Add README. Process X-Forwarded-For
All checks were successful
Go / build (push) Successful in 1m22s

This commit is contained in:
2023-10-27 21:30:42 +13:00
parent 373013a74d
commit 5521a326cf
3 changed files with 41 additions and 2 deletions

View File

@@ -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

View File

@@ -1,3 +1,27 @@
# snice
Simple Static Server
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
```

16
main.go
View File

@@ -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),