Compare commits
59 Commits
15a7690e36
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6caee54f4d | |||
|
|
fb075a8820 | ||
| 5af162aff2 | |||
|
|
e20373f556 | ||
| 9a0a7b8d6c | |||
|
|
8a1ef68a01 | ||
| 50fdbd1c30 | |||
|
|
a042e01d3e | ||
| 208afccaf5 | |||
|
|
ea74f04f3c | ||
| 149f347510 | |||
|
|
ae18004048 | ||
| 31f5a7649f | |||
|
|
f1830bed2f | ||
| b72a8e751c | |||
|
|
8c8b64dddd | ||
| be3661ae1c | |||
|
|
c924f7e360 | ||
| 9d4af9e7c5 | |||
|
|
25cf893cb7 | ||
| 2c84486945 | |||
|
|
6aa2d6d2c8 | ||
| cfe6bbaef3 | |||
|
|
37bb111127 | ||
| 55e0a6bd5c | |||
|
|
1e43763d8c | ||
| d9a029b3d1 | |||
|
|
b9746793f4 | ||
| 6c3d34143b | |||
|
|
fee35e60af | ||
| d68005b17c | |||
|
|
d5f7b40e24 | ||
| f50e12bdef | |||
|
|
2bc5373924 | ||
| 6d5cc4045c | |||
|
|
a1cc5cd483 | ||
| 688d0c9ca8 | |||
| ef067ec0d0 | |||
|
|
9adc35e806 | ||
|
|
79a907e618 | ||
| 74939c8456 | |||
|
|
3ddbe87d97 | ||
| 4b37041dff | |||
|
|
4ed91e01a9 | ||
| ea0cf4622f | |||
|
|
6a2a4d217f | ||
| 7cfbe3cd34 | |||
|
|
73ee86eb9f | ||
| a7368c182a | |||
|
|
88cd7a4ce9 | ||
|
68d7d1b481
|
|||
|
d703edb282
|
|||
|
a45deac7c8
|
|||
|
5521a326cf
|
|||
|
373013a74d
|
|||
|
2064a593d1
|
|||
|
6f13e4021e
|
|||
|
b91b9b2ed0
|
|||
|
038ef99183
|
@@ -9,8 +9,9 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
container: catthehacker/ubuntu:act-latest
|
||||
steps:
|
||||
# - uses: actions/checkout@v4
|
||||
- uses: actions/checkout@v6
|
||||
|
||||
# - name: Set up Go
|
||||
# uses: actions/setup-go@v4
|
||||
@@ -31,9 +32,9 @@ jobs:
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: ./Dockerfile
|
||||
push: true
|
||||
tags: git.nice.net.nz/snice:latest
|
||||
tags: git.nice.net.nz/hads/snice:latest
|
||||
|
||||
@@ -22,10 +22,13 @@ USER app
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
HEALTHCHECK --timeout=3s CMD /snice healthcheck
|
||||
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
|
||||
|
||||
ENTRYPOINT ["/snice"]
|
||||
CMD ["serve"]
|
||||
|
||||
26
README.md
26
README.md
@@ -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
|
||||
```
|
||||
15
go.mod
15
go.mod
@@ -1,11 +1,14 @@
|
||||
module git.nice.net.nz/hads/snice
|
||||
|
||||
go 1.21.3
|
||||
|
||||
require github.com/urfave/cli/v2 v2.25.7
|
||||
go 1.22.2
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
|
||||
github.com/lmittmann/tint v1.1.2
|
||||
github.com/urfave/cli/v2 v2.27.7
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
|
||||
)
|
||||
|
||||
44
go.sum
44
go.sum
@@ -1,8 +1,52 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/lmittmann/tint v1.0.2 h1:9XZ+JvEzjvd3VNVugYqo3j+dl0NRju8k9FquAusJExM=
|
||||
github.com/lmittmann/tint v1.0.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.0.3 h1:W5PHeA2D8bBJVvabNfQD/XW9HPLZK1XoPZH0cq8NouQ=
|
||||
github.com/lmittmann/tint v1.0.3/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.0.4 h1:LeYihpJ9hyGvE0w+K2okPTGUdVLfng1+nDNVR4vWISc=
|
||||
github.com/lmittmann/tint v1.0.4/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
|
||||
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.0.6 h1:vkkuDAZXc0EFGNzYjWcV0h7eEX+uujH48f/ifSkJWgc=
|
||||
github.com/lmittmann/tint v1.0.6/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.0.7 h1:D/0OqWZ0YOGZ6AyC+5Y2kD8PBEzBk6rFHVSfOqCkF9Y=
|
||||
github.com/lmittmann/tint v1.0.7/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.1.0 h1:0hDmvuGv3U+Cep/jHpPxwjrCFjT6syam7iY7nTmA7ug=
|
||||
github.com/lmittmann/tint v1.1.0/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.1.1 h1:xmmGuinUsCSxWdwH1OqMUQ4tzQsq3BdjJLAAmVKJ9Dw=
|
||||
github.com/lmittmann/tint v1.1.1/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/lmittmann/tint v1.1.2 h1:2CQzrL6rslrsyjqLDwD11bZ5OpLBPU+g3G/r5LSfS8w=
|
||||
github.com/lmittmann/tint v1.1.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
|
||||
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/urfave/cli/v2 v2.26.0 h1:3f3AMg3HpThFNT4I++TKOejZO8yU55t3JnnSr4S4QEI=
|
||||
github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho=
|
||||
github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
|
||||
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
|
||||
github.com/urfave/cli/v2 v2.27.3 h1:/POWahRmdh7uztQ3CYnaDddk0Rm90PyOgIxgW2rr41M=
|
||||
github.com/urfave/cli/v2 v2.27.3/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
|
||||
github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
|
||||
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
|
||||
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
|
||||
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g=
|
||||
github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
|
||||
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
|
||||
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
|
||||
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
|
||||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
|
||||
|
||||
110
main.go
110
main.go
@@ -2,13 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lmittmann/tint"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@@ -41,6 +42,20 @@ 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]
|
||||
}
|
||||
}
|
||||
remoteAddr, _, err := net.SplitHostPort(remoteAddr)
|
||||
if err != nil {
|
||||
slog.Error("Failed to format remoteAddr", slog.Any("err", err))
|
||||
}
|
||||
|
||||
responseData := &responseData{
|
||||
status: 0,
|
||||
size: 0,
|
||||
@@ -53,13 +68,16 @@ func WithLogging(h http.Handler) http.Handler {
|
||||
|
||||
duration := time.Since(start)
|
||||
|
||||
slog.Info("Request:",
|
||||
// t=2023-10-27T18:08:47.231895532+13:00
|
||||
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.String("duration", duration.String()),
|
||||
slog.Int("size", responseData.size),
|
||||
)
|
||||
|
||||
@@ -67,19 +85,6 @@ func WithLogging(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(loggingFn)
|
||||
}
|
||||
|
||||
func LoggingMiddleware(logger *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
next.ServeHTTP(w, r)
|
||||
logger.Info("Request:",
|
||||
slog.String("method", r.Method),
|
||||
slog.String("path", r.RequestURI),
|
||||
slog.String("url", r.URL.Path),
|
||||
slog.String("host", r.Host),
|
||||
)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func pingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("OK"))
|
||||
}
|
||||
@@ -87,11 +92,11 @@ func pingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func main() {
|
||||
logLevel := &slog.LevelVar{} // INFO
|
||||
|
||||
opts := slog.HandlerOptions{
|
||||
opts := tint.Options{
|
||||
Level: logLevel,
|
||||
}
|
||||
logLevel.Set(slog.LevelDebug)
|
||||
handler := slog.NewTextHandler(os.Stdout, &opts)
|
||||
handler := tint.NewHandler(os.Stdout, &opts)
|
||||
logger := slog.New(handler)
|
||||
slog.SetDefault(logger)
|
||||
|
||||
@@ -100,10 +105,17 @@ func main() {
|
||||
var directory string
|
||||
|
||||
app := &cli.App{
|
||||
Name: "snice",
|
||||
Usage: "Serve Static Files",
|
||||
Version: "v0.1.0",
|
||||
DefaultCommand: "",
|
||||
Name: "snice",
|
||||
Usage: "Serve Static Files",
|
||||
Version: "v0.2.0",
|
||||
EnableBashCompletion: true,
|
||||
Compiled: time.Time{},
|
||||
Authors: []*cli.Author{
|
||||
{Name: "Hadley Rich", Email: "hads@nice.net.nz"},
|
||||
},
|
||||
SliceFlagSeparator: "",
|
||||
UseShortOptionHandling: true,
|
||||
Suggest: true,
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "serve",
|
||||
@@ -112,22 +124,30 @@ func main() {
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
var addr string = host + ":" + port
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/ping", http.HandlerFunc(pingHandler))
|
||||
fileHandler := http.FileServer(http.Dir(directory))
|
||||
mux.Handle("/", fileHandler)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
srv.Handler = WithLogging(mux)
|
||||
|
||||
listener, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
logger.Error("Listen error", err)
|
||||
slog.Error("Listen error", err)
|
||||
}
|
||||
logger.Info(fmt.Sprintf("Serving directory %q on http://%v", directory, listener.Addr()))
|
||||
slog.Info("Starting server",
|
||||
slog.String("dir", directory),
|
||||
slog.String("addr", fmt.Sprintf(":%s", listener.Addr())),
|
||||
)
|
||||
err = srv.Serve(listener)
|
||||
if err != nil {
|
||||
logger.Error("Serve error", err)
|
||||
slog.Error("Serve error", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
@@ -136,7 +156,7 @@ func main() {
|
||||
Name: "directory",
|
||||
Aliases: []string{"dir", "d"},
|
||||
EnvVars: []string{"DIRECTORY"},
|
||||
Value: ".",
|
||||
Value: "/srv",
|
||||
Usage: "Directory to serve",
|
||||
Destination: &directory,
|
||||
},
|
||||
@@ -153,12 +173,20 @@ func main() {
|
||||
Aliases: []string{"hc"},
|
||||
Usage: "Call healthcheck endpoint",
|
||||
Action: func(cCtx *cli.Context) error {
|
||||
_, err := http.Get(fmt.Sprintf("http://127.0.0.1:%s/ping", port))
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
url := fmt.Sprintf("http://127.0.0.1:%s/ping", port)
|
||||
slog.Debug("Healthcheck: ", slog.String("url", url))
|
||||
client := http.Client{
|
||||
Timeout: 1 * time.Second,
|
||||
}
|
||||
os.Exit(0)
|
||||
return nil
|
||||
res, err := client.Get(url)
|
||||
if err != nil {
|
||||
return cli.Exit("FAIL", 1)
|
||||
}
|
||||
if res.StatusCode == 200 {
|
||||
return cli.Exit("OK", 0)
|
||||
}
|
||||
slog.Debug(fmt.Sprintf("Status: %d\n", res.StatusCode))
|
||||
return cli.Exit("FAIL", 1)
|
||||
}}},
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{Name: "quiet", Aliases: []string{"q"}},
|
||||
@@ -171,21 +199,9 @@ func main() {
|
||||
Destination: &port,
|
||||
},
|
||||
},
|
||||
EnableBashCompletion: true,
|
||||
Compiled: time.Time{},
|
||||
Authors: []*cli.Author{},
|
||||
Reader: nil,
|
||||
Writer: nil,
|
||||
ErrWriter: nil,
|
||||
SliceFlagSeparator: "",
|
||||
DisableSliceFlagSeparator: false,
|
||||
UseShortOptionHandling: true,
|
||||
Suggest: true,
|
||||
AllowExtFlags: false,
|
||||
SkipFlagParsing: false,
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
log.Fatal(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
6
renovate.json
Normal file
6
renovate.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
|
||||
"extends": [
|
||||
"local>hads/renovate-config"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user