Switch to chi and improve logs
This commit is contained in:
parent
acab4bc68b
commit
c3334faa9e
8 changed files with 23 additions and 17 deletions
|
@ -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/)
|
|
@ -7,11 +7,6 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
findMethod string
|
||||
findQuery string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(findUserCmd)
|
||||
}
|
||||
|
|
|
@ -33,7 +33,6 @@ func initConfig() {
|
|||
|
||||
auth.MustLoadKey()
|
||||
storage.Data = sqlite.MustLoadSQLite(config.Cfg.SQLiteFile)
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
24
cmd/serve.go
24
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
1
go.mod
1
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
|
||||
)
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue