From 3e444ffb2aec5f200b742c8ae34d31cf63cb0fc7 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 19 Sep 2025 14:37:31 +0200 Subject: [PATCH] Replacing more fmt.Print with log.Print --- server/auth.go | 8 ++++---- server/crypto.go | 5 +++-- server/embed_space_primitives.go | 3 ++- server/fs.go | 7 ++----- server/proxy.go | 6 +++--- server/shell_backend.go | 6 +++--- server/shell_endpoint.go | 6 +++--- 7 files changed, 20 insertions(+), 21 deletions(-) diff --git a/server/auth.go b/server/auth.go index 5b19d80f..f053dc3b 100644 --- a/server/auth.go +++ b/server/auth.go @@ -93,7 +93,7 @@ func addAuthEndpoints(r chi.Router, config *ServerConfig) { } if spaceConfig.LockoutTimer.IsLocked() { - fmt.Println("Authentication locked out, redirecting to auth page.") + log.Println("Authentication locked out, redirecting to auth page.") http.Redirect(w, r, applyURLPrefix("/.auth?error=2", spaceConfig.HostURLPrefix), http.StatusFound) return } @@ -113,7 +113,7 @@ func addAuthEndpoints(r chi.Router, config *ServerConfig) { } if err != nil { - fmt.Printf("Failed to create JWT: %v\n", err) + log.Printf("Failed to create JWT: %v\n", err) http.Error(w, "Internal server error", http.StatusInternalServerError) return } @@ -139,7 +139,7 @@ func addAuthEndpoints(r chi.Router, config *ServerConfig) { http.Redirect(w, r, applyURLPrefix(redirectPath, spaceConfig.HostURLPrefix), http.StatusFound) } else { - fmt.Println("Authentication failed, redirecting to auth page.") + log.Println("Authentication failed, redirecting to auth page.") spaceConfig.LockoutTimer.AddCount() http.Redirect(w, r, applyURLPrefix("/.auth?error=1", spaceConfig.HostURLPrefix), http.StatusFound) @@ -202,7 +202,7 @@ func authMiddleware(config *ServerConfig) func(http.Handler) http.Handler { next.ServeHTTP(w, r) return } else { - fmt.Println("Unauthorized token access") + log.Println("Unauthorized token access") http.Error(w, "Unauthorized", http.StatusUnauthorized) return } diff --git a/server/crypto.go b/server/crypto.go index e837bb80..01d085e4 100644 --- a/server/crypto.go +++ b/server/crypto.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "log" "maps" "os" "time" @@ -61,7 +62,7 @@ func (j *Authenticator) save() error { // Init initializes the JWT issuer with an auth string for validation func (j *Authenticator) init(authConfig *AuthOptions) error { if j.SecretKey == "" { - fmt.Println("Generating new JWT secret key") + log.Println("Generating new JWT secret key") if err := j.generateNewKey(); err != nil { return err } @@ -69,7 +70,7 @@ func (j *Authenticator) init(authConfig *AuthOptions) error { newAuthHash := j.hashOptions(authConfig) if j.AuthHash != newAuthHash { - fmt.Println("Authentication has changed since last run, so invalidating all existing tokens") + log.Println("Authentication has changed since last run, so invalidating all existing tokens") // Generate new key to invalidate all existing tokens if err := j.generateNewKey(); err != nil { return err diff --git a/server/embed_space_primitives.go b/server/embed_space_primitives.go index 7a34866c..1926e3f7 100644 --- a/server/embed_space_primitives.go +++ b/server/embed_space_primitives.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/fs" + "log" pathLib "path" "strings" "time" @@ -83,7 +84,7 @@ func (e *ReadOnlyFallthroughSpacePrimitives) FetchFileList() ([]FileMeta, error) }) if err != nil { - fmt.Printf("Something went wrong listing files in the FS: %v", err) + log.Printf("Something went wrong listing files in the FS: %v", err) } wrappedFiles, err := e.fallthroughSpacePrimitives.FetchFileList() diff --git a/server/fs.go b/server/fs.go index 7046a4ae..e9025167 100644 --- a/server/fs.go +++ b/server/fs.go @@ -1,7 +1,6 @@ package server import ( - "fmt" "io" "log" "net/http" @@ -115,7 +114,7 @@ func handleFsPut(w http.ResponseWriter, r *http.Request) { // Write file meta, err := spaceConfig.SpacePrimitives.WriteFile(path, body, getFileMetaFromHeaders(r.Header, path)) if err != nil { - fmt.Printf("Write failed: %v\n", err) + log.Printf("Write failed: %v\n", err) http.Error(w, "Write failed", http.StatusInternalServerError) return } @@ -136,14 +135,12 @@ func handleFsDelete(w http.ResponseWriter, r *http.Request) { return } - fmt.Printf("Deleting file: %s\n", path) - err = spaceConfig.SpacePrimitives.DeleteFile(path) if err != nil { if err == ErrNotFound { http.NotFound(w, r) } else { - fmt.Printf("Error deleting file: %v\n", err) + log.Printf("Error deleting file: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) } return diff --git a/server/proxy.go b/server/proxy.go index 045893a0..3929323c 100644 --- a/server/proxy.go +++ b/server/proxy.go @@ -1,8 +1,8 @@ package server import ( - "fmt" "io" + "log" "net/http" "regexp" "strings" @@ -44,7 +44,7 @@ func proxyHandler(config *ServerConfig) http.HandlerFunc { targetURL = "https://" + path } - fmt.Printf("Proxying to %s\n", targetURL) + log.Printf("Proxying to %s\n", targetURL) // Create the proxy request req, err := http.NewRequest(r.Method, targetURL, r.Body) @@ -76,7 +76,7 @@ func proxyHandler(config *ServerConfig) http.HandlerFunc { resp, err := client.Do(req) if err != nil { - fmt.Printf("Error fetching proxied URL: %v\n", err) + log.Printf("Error fetching proxied URL: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } diff --git a/server/shell_backend.go b/server/shell_backend.go index d988a5ca..7148dcd5 100644 --- a/server/shell_backend.go +++ b/server/shell_backend.go @@ -2,7 +2,7 @@ package server import ( "bytes" - "fmt" + "log" "os/exec" "slices" "strings" @@ -32,7 +32,7 @@ func NewLocalShell(cwd string, cmdWhiteList string) *LocalShell { func (ls *LocalShell) Handle(request ShellRequest) (ShellResponse, error) { // Check if command is whitelisted if !ls.AllowAllCmds && !slices.Contains(ls.CmdWhiteList, request.Cmd) { - fmt.Printf("Not running shell command because not in whitelist: %s\n", request.Cmd) + log.Printf("Not running shell command because not in whitelist: %s\n", request.Cmd) return ShellResponse{ Code: -1, Stdout: "", @@ -40,7 +40,7 @@ func (ls *LocalShell) Handle(request ShellRequest) (ShellResponse, error) { }, nil } - fmt.Printf("Running shell command: %s %s\n", request.Cmd, strings.Join(request.Args, " ")) + log.Printf("Running shell command: %s %s\n", request.Cmd, strings.Join(request.Args, " ")) // Create the command cmd := exec.Command(request.Cmd, request.Args...) diff --git a/server/shell_endpoint.go b/server/shell_endpoint.go index 75bd35ab..2e011e05 100644 --- a/server/shell_endpoint.go +++ b/server/shell_endpoint.go @@ -2,7 +2,7 @@ package server import ( "encoding/json" - "fmt" + "log" "net/http" ) @@ -20,7 +20,7 @@ func handleShellEndpoint(config *ServerConfig) http.HandlerFunc { // Execute the shell command shellResponse, err := spaceConfig.ShellBackend.Handle(shellRequest) if err != nil { - fmt.Printf("Shell error: %v\n", err) + log.Printf("Shell error: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -28,7 +28,7 @@ func handleShellEndpoint(config *ServerConfig) http.HandlerFunc { // Return the response as JSON w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(shellResponse); err != nil { - fmt.Printf("Error encoding shell response: %v\n", err) + log.Printf("Error encoding shell response: %v\n", err) http.Error(w, "Internal server error", http.StatusInternalServerError) return }