Switch to chi and improve logs

This commit is contained in:
Andrew-71 2024-10-13 16:51:29 +03:00
parent acab4bc68b
commit c3334faa9e
8 changed files with 23 additions and 17 deletions

View file

@ -34,4 +34,5 @@ Use "pye [command] --help" for more information about a command.
## Technologies used ## Technologies used
* **Storage** - [SQLite](https://github.com/mattn/go-sqlite3) and a PEM file * **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/) * **CLI management** - [Cobra](https://cobra.dev/)

View file

@ -7,11 +7,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
findMethod string
findQuery string
)
func init() { func init() {
rootCmd.AddCommand(findUserCmd) rootCmd.AddCommand(findUserCmd)
} }

View file

@ -33,7 +33,6 @@ func initConfig() {
auth.MustLoadKey() auth.MustLoadKey()
storage.Data = sqlite.MustLoadSQLite(config.Cfg.SQLiteFile) storage.Data = sqlite.MustLoadSQLite(config.Cfg.SQLiteFile)
} }
func init() { func init() {

View file

@ -7,13 +7,15 @@ import (
"git.a71.su/Andrew71/pye/auth" "git.a71.su/Andrew71/pye/auth"
"git.a71.su/Andrew71/pye/config" "git.a71.su/Andrew71/pye/config"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var port int var port int
func init() { 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) rootCmd.AddCommand(serveCmd)
} }
@ -25,18 +27,22 @@ var serveCmd = &cobra.Command{
} }
func serveAuth(cmd *cobra.Command, args []string) { 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) r.Get("/pem", auth.PublicKey)
router.HandleFunc("POST /login", auth.Login) r.Post("/register", auth.Register)
r.Post("/login", auth.Login)
// Note: likely temporary, possibly to be replaced by a fake "frontend" // Note: likely temporary, possibly to be replaced by a fake "frontend"
router.HandleFunc("GET /register", auth.Register) r.Get("/register", auth.Register)
router.HandleFunc("GET /login", auth.Login) r.Get("/login", auth.Login)
slog.Info("🪐 pye started", "port", port) slog.Info("🪐 pye started", "port", port)
slog.Debug("debug mode active") http.ListenAndServe(":"+strconv.Itoa(port), r)
http.ListenAndServe(":"+strconv.Itoa(port), router)
} }

View file

@ -38,7 +38,7 @@ func LoadConfig(filename string) error {
return err return err
} }
Cfg = temp_config Cfg = temp_config
slog.Debug("Loaded config", "file", filename) slog.Debug("Loaded config", "file", filename, "config", Cfg)
return nil return nil
} }

1
go.mod
View file

@ -11,6 +11,7 @@ require (
) )
require ( require (
github.com/go-chi/chi v1.5.5
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
) )

2
go.sum
View file

@ -1,4 +1,6 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 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 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

View file

@ -7,6 +7,7 @@ import (
"os" "os"
"git.a71.su/Andrew71/pye/config" "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. // 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} opts = &slog.HandlerOptions{Level: slog.LevelDebug}
} }
slog.SetDefault(slog.New(slog.NewTextHandler(w, opts))) 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")
} }