diff --git a/README.md b/README.md index 8e8ee9b..414146a 100644 --- a/README.md +++ b/README.md @@ -34,4 +34,5 @@ Use "pye [command] --help" for more information about a command. ## Technologies used * **Storage** - [SQLite](https://github.com/mattn/go-sqlite3) and a PEM file +* **HTTP routing** - [Chi](https://go-chi.io), just for logging... * **CLI management** - [Cobra](https://cobra.dev/) \ No newline at end of file diff --git a/cmd/find_user.go b/cmd/find_user.go index 945705d..1dfba10 100644 --- a/cmd/find_user.go +++ b/cmd/find_user.go @@ -7,11 +7,6 @@ import ( "github.com/spf13/cobra" ) -var ( - findMethod string - findQuery string -) - func init() { rootCmd.AddCommand(findUserCmd) } diff --git a/cmd/root.go b/cmd/root.go index 62b3242..8f76cdf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,6 @@ func initConfig() { auth.MustLoadKey() storage.Data = sqlite.MustLoadSQLite(config.Cfg.SQLiteFile) - } func init() { diff --git a/cmd/serve.go b/cmd/serve.go index 7706c0d..5e572d4 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -7,13 +7,15 @@ import ( "git.a71.su/Andrew71/pye/auth" "git.a71.su/Andrew71/pye/config" + "github.com/go-chi/chi" + "github.com/go-chi/chi/middleware" "github.com/spf13/cobra" ) var port int func init() { - serveCmd.Flags().IntVarP(&port, "port", "p", config.Cfg.Port, "port to use") + serveCmd.Flags().IntVarP(&port, "port", "p", 0, "port to use") rootCmd.AddCommand(serveCmd) } @@ -25,18 +27,22 @@ var serveCmd = &cobra.Command{ } func serveAuth(cmd *cobra.Command, args []string) { - router := http.NewServeMux() + if port == 0 { + port = config.Cfg.Port + } - router.HandleFunc("GET /pem", auth.PublicKey) + r := chi.NewRouter() + r.Use(middleware.RealIP) + r.Use(middleware.Logger, middleware.CleanPath, middleware.StripSlashes) - router.HandleFunc("POST /register", auth.Register) - router.HandleFunc("POST /login", auth.Login) + r.Get("/pem", auth.PublicKey) + r.Post("/register", auth.Register) + r.Post("/login", auth.Login) // Note: likely temporary, possibly to be replaced by a fake "frontend" - router.HandleFunc("GET /register", auth.Register) - router.HandleFunc("GET /login", auth.Login) + r.Get("/register", auth.Register) + r.Get("/login", auth.Login) slog.Info("🪐 pye started", "port", port) - slog.Debug("debug mode active") - http.ListenAndServe(":"+strconv.Itoa(port), router) + http.ListenAndServe(":"+strconv.Itoa(port), r) } diff --git a/config/config.go b/config/config.go index 6a7dae7..77b1806 100644 --- a/config/config.go +++ b/config/config.go @@ -38,7 +38,7 @@ func LoadConfig(filename string) error { return err } Cfg = temp_config - slog.Debug("Loaded config", "file", filename) + slog.Debug("Loaded config", "file", filename, "config", Cfg) return nil } diff --git a/go.mod b/go.mod index 002bb97..b858fde 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( ) require ( + github.com/go-chi/chi v1.5.5 github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect ) diff --git a/go.sum b/go.sum index f8cf678..ada24e3 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= +github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= diff --git a/logging/log.go b/logging/log.go index 47bf473..b7255b7 100644 --- a/logging/log.go +++ b/logging/log.go @@ -7,6 +7,7 @@ import ( "os" "git.a71.su/Andrew71/pye/config" + "github.com/go-chi/chi/middleware" ) // LogInit makes slog output to both os.Stdout and a file if needed, and sets slog.LevelDebug if enabled. @@ -30,5 +31,6 @@ func LogInit(debugMode bool) { opts = &slog.HandlerOptions{Level: slog.LevelDebug} } slog.SetDefault(slog.New(slog.NewTextHandler(w, opts))) - log.SetFlags(log.LstdFlags | log.Lshortfile) + middleware.DefaultLogger = middleware.RequestLogger(&middleware.DefaultLogFormatter{Logger: log.Default(), NoColor: true}) + slog.Debug("debug mode active") }