Add README. Process X-Forwarded-For
All checks were successful
Go / build (push) Successful in 1m22s
All checks were successful
Go / build (push) Successful in 1m22s
This commit is contained in:
@@ -25,6 +25,7 @@ EXPOSE 3000
|
|||||||
HEALTHCHECK --timeout=10s CMD ["/snice", "healthcheck"]
|
HEALTHCHECK --timeout=10s CMD ["/snice", "healthcheck"]
|
||||||
|
|
||||||
COPY --from=builder /etc/passwd /etc/passwd
|
COPY --from=builder /etc/passwd /etc/passwd
|
||||||
|
COPY README.md /srv/README.md
|
||||||
COPY --from=builder /snice /
|
COPY --from=builder /snice /
|
||||||
|
|
||||||
VOLUME /srv
|
VOLUME /srv
|
||||||
|
|||||||
26
README.md
26
README.md
@@ -1,3 +1,27 @@
|
|||||||
# snice
|
# 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
16
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/lmittmann/tint"
|
"github.com/lmittmann/tint"
|
||||||
@@ -41,6 +42,16 @@ func WithLogging(h http.Handler) http.Handler {
|
|||||||
loggingFn := func(w http.ResponseWriter, r *http.Request) {
|
loggingFn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
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{
|
responseData := &responseData{
|
||||||
status: 0,
|
status: 0,
|
||||||
size: 0,
|
size: 0,
|
||||||
@@ -53,11 +64,14 @@ func WithLogging(h http.Handler) http.Handler {
|
|||||||
|
|
||||||
duration := time.Since(start)
|
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("method", r.Method),
|
||||||
slog.String("path", r.RequestURI),
|
slog.String("path", r.RequestURI),
|
||||||
slog.String("url", r.URL.Path),
|
slog.String("url", r.URL.Path),
|
||||||
slog.String("host", r.Host),
|
slog.String("host", r.Host),
|
||||||
|
slog.String("referer", r.Referer()),
|
||||||
|
slog.String("remote_addr", remoteAddr),
|
||||||
slog.Int("status", responseData.status),
|
slog.Int("status", responseData.status),
|
||||||
slog.Int64("duration", duration.Microseconds()),
|
slog.Int64("duration", duration.Microseconds()),
|
||||||
slog.Int("size", responseData.size),
|
slog.Int("size", responseData.size),
|
||||||
|
|||||||
Reference in New Issue
Block a user