Replacing more fmt.Print with log.Print

This commit is contained in:
Zef Hemel
2025-09-19 14:37:31 +02:00
parent 1541d13834
commit 3e444ffb2a
7 changed files with 20 additions and 21 deletions
+4 -4
View File
@@ -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
}
+3 -2
View File
@@ -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
+2 -1
View File
@@ -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()
+2 -5
View File
@@ -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
+3 -3
View File
@@ -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
}
+3 -3
View File
@@ -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...)
+3 -3
View File
@@ -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
}