diff --git a/Dockerfile b/Dockerfile index 63ddde7..3a66043 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,6 +25,7 @@ EXPOSE 3000 HEALTHCHECK --timeout=10s CMD ["/snice", "healthcheck"] COPY --from=builder /etc/passwd /etc/passwd +COPY README.md /srv/README.md COPY --from=builder /snice / VOLUME /srv diff --git a/README.md b/README.md index e033fb8..94010b3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,27 @@ # snice -Simple Static Server \ No newline at end of file +Simple Static Server designed to be run in Docker and served behind [Traefik](https://doc.traefik.io/traefik/) + +## Usage + +``` +NAME: + snice - Serve Static Files + +USAGE: + snice [global options] command [command options] [arguments...] + +VERSION: + v0.2.0 + +COMMANDS: + serve, s Serve directory + healthcheck, hc Call healthcheck endpoint + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --quiet, -q (default: false) + --port value, -p value Port to serve on (default: "3000") [$PORT] + --help, -h show help + --version, -v print the version +``` \ No newline at end of file diff --git a/main.go b/main.go index 6a407e9..1c6d4c2 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "os" + "strings" "time" "github.com/lmittmann/tint" @@ -41,6 +42,16 @@ func WithLogging(h http.Handler) http.Handler { loggingFn := func(w http.ResponseWriter, r *http.Request) { start := time.Now() + remoteAddr := r.RemoteAddr + fwdAddress := r.Header.Get("X-Forwarded-For") + if fwdAddress != "" { + remoteAddr = fwdAddress + ips := strings.Split(fwdAddress, ", ") + if len(ips) > 1 { + remoteAddr = ips[0] + } + } + responseData := &responseData{ status: 0, size: 0, @@ -53,11 +64,14 @@ func WithLogging(h http.Handler) http.Handler { duration := time.Since(start) - slog.Info("Request:", + // t=2023-10-27T18:08:47.231895532+13:00 remote_addr=100.114.208.117 time_ms=4 duration=4.914291ms + slog.Info("Request Completed:", slog.String("method", r.Method), slog.String("path", r.RequestURI), slog.String("url", r.URL.Path), slog.String("host", r.Host), + slog.String("referer", r.Referer()), + slog.String("remote_addr", remoteAddr), slog.Int("status", responseData.status), slog.Int64("duration", duration.Microseconds()), slog.Int("size", responseData.size),