pye/internal/app/serve.go

49 lines
1 KiB
Go
Raw Normal View History

2024-10-13 21:03:44 +03:00
package app
2024-10-13 16:16:19 +03:00
import (
"log/slog"
"net/http"
"strconv"
2024-10-13 21:03:44 +03:00
"git.a71.su/Andrew71/pye/internal/auth"
"git.a71.su/Andrew71/pye/internal/config"
2024-10-13 16:51:29 +03:00
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
2024-10-13 16:16:19 +03:00
"github.com/spf13/cobra"
)
var port int
func init() {
2024-10-13 16:51:29 +03:00
serveCmd.Flags().IntVarP(&port, "port", "p", 0, "port to use")
2024-10-13 16:16:19 +03:00
rootCmd.AddCommand(serveCmd)
}
var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start JWT service",
Long: `Start a simple authentication service`,
Run: serveAuth,
}
func serveAuth(cmd *cobra.Command, args []string) {
2024-10-13 16:51:29 +03:00
if port == 0 {
port = config.Cfg.Port
}
2024-10-13 16:16:19 +03:00
2024-10-13 16:51:29 +03:00
r := chi.NewRouter()
r.Use(middleware.RealIP)
r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes)
2024-10-13 16:16:19 +03:00
2024-10-13 17:18:53 +03:00
r.Get("/pem", auth.ServePublicKey)
2024-10-13 16:51:29 +03:00
r.Post("/register", auth.Register)
r.Post("/login", auth.Login)
2024-10-13 16:16:19 +03:00
// Note: likely temporary, possibly to be replaced by a fake "frontend"
2024-10-13 16:51:29 +03:00
r.Get("/register", auth.Register)
r.Get("/login", auth.Login)
2024-10-13 16:16:19 +03:00
slog.Info("🪐 pye started", "port", port)
2024-10-13 16:51:29 +03:00
http.ListenAndServe(":"+strconv.Itoa(port), r)
2024-10-13 16:16:19 +03:00
}