diff --git a/api/api.go b/api/api.go index fd9e68f..d44f19b 100644 --- a/api/api.go +++ b/api/api.go @@ -3,7 +3,7 @@ package api import ( "database/sql" "fmt" - "log" + "log/slog" "net/http" "net/url" "path" @@ -58,7 +58,7 @@ func NewSecretHandler(config HandlerConfig) SecretHandler { func (s SecretHandler) Post(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" && r.URL.Path != "" { http.Error(w, "Not found", http.StatusNotFound) - log.Println("Not found", r.URL.Path) + slog.Info("Not found", "url", r.URL.Path) return } @@ -168,6 +168,7 @@ func (s SecretHandler) RegisterHandlers(mux *http.ServeMux, apiRoot string) { http.StripPrefix( apiRoot, middlewares.SetupLogging( + false, middlewares.SetupHeaders(http.HandlerFunc(s.Get)), ), ), @@ -177,6 +178,7 @@ func (s SecretHandler) RegisterHandlers(mux *http.ServeMux, apiRoot string) { http.StripPrefix( path.Join("/", apiRoot), middlewares.SetupLogging( + true, middlewares.SetupHeaders(http.HandlerFunc(s.Post)), ), ), @@ -187,6 +189,7 @@ func (s SecretHandler) RegisterHandlers(mux *http.ServeMux, apiRoot string) { http.StripPrefix( apiRoot, middlewares.SetupLogging( + false, middlewares.SetupHeaders(http.HandlerFunc(s.Delete)), ), ), @@ -197,6 +200,7 @@ func (s SecretHandler) RegisterHandlers(mux *http.ServeMux, apiRoot string) { http.StripPrefix( apiRoot, middlewares.SetupLogging( + false, middlewares.SetupHeaders(http.HandlerFunc(s.Options)), ), ), @@ -208,11 +212,12 @@ func (s SecretHandler) RegisterHandlers(mux *http.ServeMux, apiRoot string) { http.StripPrefix( apiRoot, middlewares.SetupLogging( + false, middlewares.SetupHeaders(http.HandlerFunc(s.GenerateEncryptionKey)), ), ), ) - mux.Handle("/", middlewares.SetupLogging(middlewares.SetupHeaders(http.HandlerFunc(s.NotFound)))) + mux.Handle("/", middlewares.SetupLogging(true, middlewares.SetupHeaders(http.HandlerFunc(s.NotFound)))) } diff --git a/api/middlewares/middlewares.go b/api/middlewares/middlewares.go index 867b2f1..06b7721 100644 --- a/api/middlewares/middlewares.go +++ b/api/middlewares/middlewares.go @@ -1,17 +1,16 @@ package middlewares import ( - "fmt" - "log" + "log/slog" "net/http" ) -func SetupLogging(h http.Handler) http.Handler { +func SetupLogging(withPath bool, h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodGet || r.Method == http.MethodDelete || r.Method == http.MethodOptions { - log.Println(fmt.Sprintf("%s: %s", r.Method, "/***")) + if withPath { + slog.Info("handle", "method", r.Method, "path", r.URL.Path) } else { - log.Println(fmt.Sprintf("%s: %s", r.Method, r.URL.Path)) + slog.Info("handle", "method", r.Method) } h.ServeHTTP(w, r) }) diff --git a/cmd/prepare/main.go b/cmd/prepare/main.go index 7ecc507..c711b74 100644 --- a/cmd/prepare/main.go +++ b/cmd/prepare/main.go @@ -3,7 +3,7 @@ package main import ( "context" "flag" - "fmt" + "log/slog" _ "github.com/lib/pq" @@ -32,6 +32,6 @@ func prepareDatabase(ctx context.Context) error { func main() { if err := prepareDatabase(context.Background()); err != nil { - fmt.Println(err) + slog.Error("Failed to prepare database", "error", err) } } diff --git a/cmd/sekret.link/main.go b/cmd/sekret.link/main.go index 08846e7..1b20159 100644 --- a/cmd/sekret.link/main.go +++ b/cmd/sekret.link/main.go @@ -6,6 +6,7 @@ import ( "database/sql" "flag" "fmt" + "log/slog" "net/http" "net/url" "os" @@ -58,7 +59,7 @@ func scheduleDeleteExpired(ctx context.Context, db *sql.DB) error { for { select { case <-ctx.Done(): - fmt.Println("Stop deleting expired entries") + slog.Info("Stop deleting expired entries") return nil case <-ticker.C: if err := manager.DeleteExpired(ctx); err != nil { @@ -84,7 +85,7 @@ func listen(handlerConfig api.HandlerConfig) *http.Server { } go func() { - fmt.Println("Handle Path: ", apiRoot) + slog.Info("Start listening", "address", httpServer.Addr, "path", apiRoot) if err := httpServer.ListenAndServe(); err != nil { if err.Error() != "http: Server closed" { fmt.Fprintf(os.Stderr, "error: %s", err) diff --git a/internal/api/createentry.go b/internal/api/createentry.go index d9391aa..791e207 100644 --- a/internal/api/createentry.go +++ b/internal/api/createentry.go @@ -3,7 +3,7 @@ package api import ( "context" "errors" - "log" + "log/slog" "net/http" "time" @@ -78,7 +78,7 @@ func (c CreateHandler) handle(w http.ResponseWriter, r *http.Request) error { // Handle handles http request to create secret func (c CreateHandler) Handle(w http.ResponseWriter, r *http.Request) { if err := c.handle(w, r); err != nil { - log.Println("create error", err) + slog.Error("create error", "error", err) c.view.RenderError(w, r, err) } } diff --git a/internal/api/createentry_test.go b/internal/api/createentry_test.go index 438bc8d..6f8486f 100644 --- a/internal/api/createentry_test.go +++ b/internal/api/createentry_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "fmt" "net/http" "net/http/httptest" "testing" diff --git a/internal/views/entrycreate.go b/internal/views/entrycreate.go index 1956140..c60cb6f 100644 --- a/internal/views/entrycreate.go +++ b/internal/views/entrycreate.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "log" + "log/slog" "net/http" "net/url" "os" @@ -54,12 +54,12 @@ func (e EntryCreateView) Render(w http.ResponseWriter, r *http.Request, entry En w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(entry); err != nil { - log.Println("JSON encode failed", err) + slog.Error("JSON encode failed", "error", err) } } else { newURL, err := uuid.GetUUIDUrlWithSecret(e.webExternalURL, entry.UUID, entry.Key) if err != nil { - log.Println("Get UUID URL with secret failed", err) + slog.Error("Get UUID URL with secret failed", "error", err) http.Error(w, "Internal error", http.StatusInternalServerError) return } diff --git a/internal/views/entrykeycreate.go b/internal/views/entrykeycreate.go index ff78cea..5b12492 100644 --- a/internal/views/entrykeycreate.go +++ b/internal/views/entrykeycreate.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "log" + "log/slog" "net/http" "net/url" "os" @@ -44,7 +44,7 @@ func (g GenerateEntryKeyView) Render(w http.ResponseWriter, r *http.Request, res w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(response); err != nil { - log.Println("JSON encode failed", err) + slog.Error("JSON encode failed", "error", err) } } else { newURL, err := uuid.GetUUIDUrlWithSecret(g.webExternalURL, response.UUID, response.Key.String()) diff --git a/internal/views/entryread.go b/internal/views/entryread.go index f5ce1ed..5a728dc 100644 --- a/internal/views/entryread.go +++ b/internal/views/entryread.go @@ -5,7 +5,7 @@ import ( "encoding/hex" "encoding/json" "errors" - "log" + "log/slog" "net/http" "time" @@ -47,7 +47,7 @@ func (e EntryReadView) Render(w http.ResponseWriter, r *http.Request, response E if r.Header.Get("Accept") == "application/json" { w.Header().Add("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(response); err != nil { - log.Println("JSON encode failed", err) + slog.Error("JSON encode failed", "error", err) } } else { if response.ContentType != "" {