diff --git a/README.md b/README.md index 180ec69..7a08fd2 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,46 @@ You can build `ratchet` locally by cloning the respository, then run The `ratchetd` cmd binary uses Oauth so you will need to create a new Oauth App. The vlaue of the authorization callback must be the hostname and IP at which clients can access the `ratchetd` server. +## Additional Resources + +- [Content Range Requests](https://web.archive.org/web/20230918195519/https://benramsey.com/blog/2008/05/206-partial-content-and-range-requests/) +- [HTTP 204 and 205 Status Codes](https://web.archive.org/web/20230918193536/https://benramsey.com/blog/2008/05/http-status-204-no-content-and-205-reset-content/) +- [How to Disable FileServer Directory Listings](https://www.alexedwards.net/blog/disable-http-fileserver-directory-listings) +- [Understand Mutexs in Go](https://www.alexedwards.net/blog/understanding-mutexes) +- [Structured Logging in Go with log/slog](https://pkg.go.dev/log/slog) +- [Exit Codes with special meaning](https://tldp.org/LDP/abs/html/exitcodes.html) + +### Go http.FileServer + +Supports If-Modified-Since and Last-Modified headers +``` +curl -i -H "If-Modified-Since: +Thu, 04 May 2017 13:07:52 GMT" http://localhost:5001/static/img/logo.png +HTTP/1.1 304 Not Modified +Last-Modified: Thu, 04 May 2017 13:07:52 GMT +Date: Sun, 12 Jan 2025 14:26:06 GMT +``` + +Supports Range Requests and 206 Partial Content responses. +``` +curl -i -H "Range: bytes=100-199" --output - http://localhost:5001/static/img/logo.png +HTTP/1.1 206 Partial Content +Accept-Ranges: bytes +Content-Length: 100 +Content-Range: bytes 100-199/1075 +Content-Type: image/png +Last-Modified: Thu, 04 May 2017 13:07:52 GMT +Date: Sun, 12 Jan 2025 14:18:32 GMT +``` + +- The `Content-Type` is automatically set from the file extension using the `mime.TypeByExtension()` function. You can add your own custom extensions and content types using the `mime.AddExtensionType()` function if necessary. + +- Sometimes you might want to serve a single file from within a handler. For this there’s the `http.ServeFile()` function, which you can use like so: + +```go +func downloadHandler(w http.ResponseWriter, r *http.Request) { + http.ServeFile(w, r, "./ui/static/file.zip") +} +``` + +Warning: http.ServeFile() does not automatically sanitize the file path. If you’re constructing a file path from untrusted user input, to avoid directory traversal attacks you must sanitize the input with filepath.Clean() before using it. \ No newline at end of file diff --git a/cmd/ratchetd/main.go b/cmd/ratchetd/main.go index df49678..114b52f 100644 --- a/cmd/ratchetd/main.go +++ b/cmd/ratchetd/main.go @@ -1,8 +1,11 @@ package main import ( - "log" + "flag" + "fmt" + "log/slog" "net/http" + "os" "strings" "git.runcible.io/learning/ratchet" @@ -15,11 +18,27 @@ var ( ) func main() { + // Commandline options + addr := flag.String("addr", "0.0.0.0", "HTTP network address") + port := flag.String("port", "5001", "HTTP port") + logLevel := flag.String("logging", "INFO", "Logging Level. Valid values [INFO, DEBUG, WARN, ERROR].") + // must call parse or all values will be the defaults + flag.Parse() + + // Setup Logging + ratchethttp.InitLogging(*logLevel) + // Propagate build information to root package to share globally ratchet.Version = strings.TrimPrefix(version, "") ratchet.Commit = commit server := ratchethttp.NewRatchetServer() - log.Print("Listening on http://localhost:5001/") - log.Fatal(http.ListenAndServe(":5001", server)) + slog.Debug("Herp dirp!") + slog.Info(fmt.Sprintf("Listening on http://%s:%s", *addr, *port)) + //log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", *addr, *port), server)) + // there is no log.Fatal equivalent. This is an approximation of the behavior + err := http.ListenAndServe(fmt.Sprintf("%s:%s", *addr, *port), server) + slog.Error(err.Error()) + os.Exit(1) + } diff --git a/internal/server.go b/internal/server.go index 1b72444..d0775a2 100644 --- a/internal/server.go +++ b/internal/server.go @@ -3,23 +3,57 @@ package ratchet import ( "fmt" "html/template" - "log" + "log/slog" "net/http" + "os" "strconv" + "strings" ) type RatchetServer struct { http.Handler //Services used by HTTP routes - UserService UserService } +func parseLogLevel(levelStr string) slog.Level { + switch strings.ToUpper(levelStr) { + case "DEBUG": + return slog.LevelDebug + case "INFO": + return slog.LevelInfo + case "WARN": + return slog.LevelWarn + case "ERROR": + return slog.LevelError + default: + return slog.LevelInfo // Default level + } +} + +func InitLogging(level string) { + // Use os.Stderr + // + // Stderr is used for diagnostics and logging. Stdout is used for program + // output. Stderr also have greater likely hood of being seen if a programs + // output is being redirected. + parsedLogLevel := parseLogLevel(level) + loggerHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: parsedLogLevel, AddSource: true}) + logger := slog.New(loggerHandler) + slog.SetDefault(logger) +} + func NewRatchetServer() *RatchetServer { r := new(RatchetServer) + // TODO implement middleware that disables directory listings + fileServer := http.FileServer(http.Dir("./ui/static/")) router := http.NewServeMux() + + // Subtree pattern for static assets + router.Handle("GET /static/", http.StripPrefix("/static/", fileServer)) + // /{$} is used to prevent subtree path patterns from acting like a wildcard // resulting in this route requiring an exact match on "/" only // You can only include one HTTP method in a route pattern if you choose @@ -27,12 +61,20 @@ func NewRatchetServer() *RatchetServer { router.HandleFunc("GET /{$}", home) router.HandleFunc("GET /snippet/view/{id}", snippetView) router.HandleFunc("GET /snippet/create", snippetCreate) + + // FYI The http.HandlerFunc() adapter works by automatically adding a ServeHTTP() method to + // the passed function router.HandleFunc("POST /snippet/create", snippetCreatePost) + + // Mux Router implements the Handler interface. AKA it has a ServeHTTP receiver. r.Handler = router return r } func home(w http.ResponseWriter, r *http.Request) { + // TODO middleware should be able to print out these lines for all routes + slog.Info("request received", "method", "GET", "path", "/") + w.Header().Add("Server", "Go") // Initialize a slice containing the paths to the two files. It's important @@ -47,14 +89,14 @@ func home(w http.ResponseWriter, r *http.Request) { // read template file into template set. ts, err := template.ParseFiles(files...) if err != nil { - log.Print(err.Error()) + slog.Error(err.Error()) http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } // Write template content to response body err = ts.ExecuteTemplate(w, "base", nil) if err != nil { - log.Print(err.Error()) + slog.Error(err.Error()) http.Error(w, "Internal Server Error", http.StatusInternalServerError) } } diff --git a/ui/html/base.go.tmpl b/ui/html/base.go.tmpl index b017bf7..09d86e4 100644 --- a/ui/html/base.go.tmpl +++ b/ui/html/base.go.tmpl @@ -4,6 +4,10 @@