61 Commits

Author SHA1 Message Date
6caee54f4d Merge pull request 'chore(deps): update actions/checkout action to v6' (#28) from renovate/actions-checkout-6.x into main
All checks were successful
Go / build (push) Successful in 3m16s
Reviewed-on: #28
2025-12-10 23:49:29 +13:00
Renovate Bot
fb075a8820 chore(deps): update actions/checkout action to v6
Some checks failed
Go / build (pull_request) Failing after 0s
2025-11-22 18:14:43 +00:00
5af162aff2 Merge pull request 'chore(deps): update actions/checkout action to v5' (#27) from renovate/actions-checkout-5.x into main
Some checks failed
Go / build (push) Failing after 3s
Reviewed-on: #27
2025-08-19 16:25:22 +12:00
Renovate Bot
e20373f556 chore(deps): update actions/checkout action to v5
Some checks failed
Go / build (pull_request) Failing after 2s
2025-08-11 18:11:58 +00:00
9a0a7b8d6c Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.7' (#26) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 33s
Reviewed-on: #26
2025-07-16 09:13:31 +12:00
Renovate Bot
8a1ef68a01 fix(deps): update module github.com/urfave/cli/v2 to v2.27.7
All checks were successful
Go / build (pull_request) Successful in 32s
2025-06-14 06:06:47 +00:00
50fdbd1c30 Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.1.2' (#25) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 33s
Reviewed-on: #25
2025-06-09 09:53:49 +12:00
Renovate Bot
a042e01d3e fix(deps): update module github.com/lmittmann/tint to v1.1.2
All checks were successful
Go / build (pull_request) Successful in 52s
2025-06-08 00:08:50 +00:00
208afccaf5 Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.1.1' (#24) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 33s
Reviewed-on: #24
2025-06-05 10:02:43 +12:00
Renovate Bot
ea74f04f3c fix(deps): update module github.com/lmittmann/tint to v1.1.1
All checks were successful
Go / build (pull_request) Successful in 31s
2025-06-02 12:09:01 +00:00
149f347510 Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.6' (#21) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 33s
Reviewed-on: #21
2025-05-23 13:57:40 +12:00
Renovate Bot
ae18004048 fix(deps): update module github.com/urfave/cli/v2 to v2.27.6
All checks were successful
Go / build (pull_request) Successful in 31s
2025-05-23 00:07:17 +00:00
31f5a7649f Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.1.0' (#23) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 31s
Reviewed-on: #23
2025-05-23 08:20:57 +12:00
Renovate Bot
f1830bed2f fix(deps): update module github.com/lmittmann/tint to v1.1.0
All checks were successful
Go / build (pull_request) Successful in 50s
2025-05-19 18:10:31 +00:00
b72a8e751c Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.0.7' (#20) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 29s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/20
2025-01-30 08:56:04 +13:00
Renovate Bot
8c8b64dddd fix(deps): update module github.com/lmittmann/tint to v1.0.7
All checks were successful
Go / build (pull_request) Successful in 45s
2025-01-29 19:48:42 +00:00
be3661ae1c Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.0.6' (#19) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 35s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/19
2024-12-17 09:57:32 +13:00
Renovate Bot
c924f7e360 fix(deps): update module github.com/lmittmann/tint to v1.0.6
All checks were successful
Go / build (pull_request) Successful in 52s
2024-12-16 17:04:17 +00:00
9d4af9e7c5 Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.5' (#18) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 1m10s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/18
2024-10-14 10:52:16 +13:00
Renovate Bot
25cf893cb7 fix(deps): update module github.com/urfave/cli/v2 to v2.27.5
All checks were successful
Go / build (pull_request) Successful in 1m21s
2024-10-13 17:04:22 +00:00
2c84486945 Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.4' (#17) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 44s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/17
2024-08-12 09:03:47 +12:00
Renovate Bot
6aa2d6d2c8 fix(deps): update module github.com/urfave/cli/v2 to v2.27.4
All checks were successful
Go / build (pull_request) Successful in 49s
2024-08-11 18:02:22 +00:00
cfe6bbaef3 Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.3' (#16) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 1m41s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/16
2024-08-07 18:18:09 +12:00
Renovate Bot
37bb111127 fix(deps): update module github.com/urfave/cli/v2 to v2.27.3
All checks were successful
Go / build (pull_request) Successful in 52s
2024-07-25 00:02:42 +00:00
55e0a6bd5c Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.0.5' (#15) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 1m5s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/15
2024-07-18 10:49:44 +12:00
Renovate Bot
1e43763d8c fix(deps): update module github.com/lmittmann/tint to v1.0.5
All checks were successful
Go / build (pull_request) Successful in 1m37s
2024-07-12 01:52:44 +00:00
d9a029b3d1 Merge pull request 'chore(deps): update docker/build-push-action action to v6' (#14) from renovate/docker-build-push-action-6.x into main
All checks were successful
Go / build (push) Successful in 53s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/14
2024-06-19 08:55:26 +12:00
Renovate Bot
b9746793f4 chore(deps): update docker/build-push-action action to v6
All checks were successful
Go / build (pull_request) Successful in 59s
2024-06-17 12:05:15 +00:00
6c3d34143b Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.2' (#13) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 58s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/13
2024-04-28 20:12:09 +12:00
Renovate Bot
fee35e60af fix(deps): update module github.com/urfave/cli/v2 to v2.27.2
All checks were successful
Go / build (pull_request) Successful in 58s
2024-04-27 18:02:30 +00:00
d68005b17c Merge pull request 'chore(deps): update dependency go to v1.22.2' (#12) from renovate/go-1.x into main
All checks were successful
Go / build (push) Successful in 55s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/12
2024-04-07 08:56:40 +12:00
Renovate Bot
d5f7b40e24 chore(deps): update dependency go to v1.22.2
Some checks failed
Go / build (pull_request) Failing after 34s
2024-04-03 17:01:58 +00:00
f50e12bdef Merge pull request 'chore(deps): update dependency go to v1.22.1' (#11) from renovate/go-1.x into main
All checks were successful
Go / build (push) Successful in 55s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/11
2024-03-11 09:06:46 +13:00
Renovate Bot
2bc5373924 chore(deps): update dependency go to v1.22.1
All checks were successful
Go / build (pull_request) Successful in 1m26s
2024-03-10 17:05:44 +00:00
6d5cc4045c Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.0.4' (#9) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 59s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/9
2024-01-27 21:14:12 +13:00
Renovate Bot
a1cc5cd483 fix(deps): update module github.com/lmittmann/tint to v1.0.4
All checks were successful
Go / build (pull_request) Successful in 56s
2024-01-19 11:03:12 +00:00
688d0c9ca8 Merge pull request 'chore(deps): update dependency go to v1.21.6' (#8) from renovate/go-1.x into main
All checks were successful
Go / build (push) Successful in 54s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/8
2024-01-19 11:55:50 +13:00
ef067ec0d0 Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.27.1' (#7) from renovate/github.com-urfave-cli-v2-2.x into main
Some checks failed
Go / build (push) Has been cancelled
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/7
2024-01-19 11:55:28 +13:00
Renovate Bot
9adc35e806 chore(deps): update dependency go to v1.21.6
All checks were successful
Go / build (pull_request) Successful in 1m26s
2024-01-10 11:01:18 +00:00
Renovate Bot
79a907e618 fix(deps): update module github.com/urfave/cli/v2 to v2.27.1
All checks were successful
Go / build (pull_request) Successful in 55s
2023-12-31 11:03:05 +00:00
74939c8456 Merge pull request 'chore(deps): update dependency go to v1.21.5' (#6) from renovate/go-1.x into main
All checks were successful
Go / build (push) Successful in 55s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/6
2023-12-11 10:05:49 +13:00
Renovate Bot
3ddbe87d97 chore(deps): update dependency go to v1.21.5
All checks were successful
Go / build (pull_request) Successful in 56s
2023-12-06 11:03:36 +00:00
4b37041dff Merge pull request 'fix(deps): update module github.com/urfave/cli/v2 to v2.26.0' (#5) from renovate/github.com-urfave-cli-v2-2.x into main
All checks were successful
Go / build (push) Successful in 1m22s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/5
2023-12-04 11:15:32 +13:00
Renovate Bot
4ed91e01a9 fix(deps): update module github.com/urfave/cli/v2 to v2.26.0
Some checks failed
Go / build (pull_request) Failing after 1s
2023-12-03 11:03:42 +00:00
ea0cf4622f Merge pull request 'chore(deps): update dependency go to v1.21.4' (#4) from renovate/go-1.x into main
All checks were successful
Go / build (push) Successful in 56s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/4
2023-11-09 12:41:19 +13:00
Renovate Bot
6a2a4d217f chore(deps): update dependency go to v1.21.4
All checks were successful
Go / build (pull_request) Successful in 56s
2023-11-08 11:03:17 +00:00
7cfbe3cd34 Merge pull request 'fix(deps): update module github.com/lmittmann/tint to v1.0.3' (#2) from renovate/github.com-lmittmann-tint-1.x into main
All checks were successful
Go / build (push) Successful in 1m16s
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/2
2023-10-30 16:18:37 +13:00
Renovate Bot
73ee86eb9f fix(deps): update module github.com/lmittmann/tint to v1.0.3
Some checks reported warnings
Go / build (pull_request) Has been cancelled
2023-10-30 03:12:38 +00:00
a7368c182a Merge pull request 'Configure Renovate' (#1) from renovate/configure into main
Some checks reported warnings
Go / build (push) Has been cancelled
Reviewed-on: https://git.nice.net.nz/hads/snice/pulls/1
2023-10-30 16:07:12 +13:00
Renovate Bot
88cd7a4ce9 chore(deps): add renovate.json
All checks were successful
Go / build (pull_request) Successful in 1m16s
2023-10-30 03:05:04 +00:00
68d7d1b481 Order
All checks were successful
Go / build (push) Successful in 1m22s
2023-10-29 13:59:04 +13:00
d703edb282 Tweak logging
All checks were successful
Go / build (push) Successful in 1m20s
2023-10-28 09:33:18 +13:00
a45deac7c8 go mod tidy
All checks were successful
Go / build (push) Successful in 1m21s
2023-10-27 21:37:54 +13:00
5521a326cf Add README. Process X-Forwarded-For
All checks were successful
Go / build (push) Successful in 1m22s
2023-10-27 21:30:42 +13:00
373013a74d Color logging. Timeouts
All checks were successful
Go / build (push) Successful in 1m20s
2023-10-27 17:21:21 +13:00
2064a593d1 Fix healthcheck. Use /srv
All checks were successful
Go / build (push) Successful in 1m22s
2023-10-27 16:33:31 +13:00
6f13e4021e Fix package URL
All checks were successful
Go / build (push) Successful in 1m21s
2023-10-27 12:18:49 +13:00
b91b9b2ed0 Checkout
Some checks failed
Go / build (push) Failing after 1m20s
2023-10-27 12:04:24 +13:00
038ef99183 Use ubuntu container runner
Some checks failed
Go / build (push) Failing after 50s
2023-10-27 12:01:06 +13:00
15a7690e36 Docker
Some checks failed
Go / build (push) Failing after 25s
2023-10-27 11:40:19 +13:00
e56a541a40 New logging middleware that captures response 2023-10-27 11:39:48 +13:00
7 changed files with 265 additions and 68 deletions

View File

@@ -9,20 +9,32 @@ on:
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.21' ]
name: Go ${{ matrix.go }} test
container: catthehacker/ubuntu:act-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v6
- name: Set up Go
uses: actions/setup-go@v4
# - name: Set up Go
# uses: actions/setup-go@v4
# with:
# go-version: 1.21
# - name: Build
# run: go build -v ./...
- name: Login to Docker registry
uses: docker/login-action@v3
with:
go-version: ${{ matrix.go }}
registry: git.nice.net.nz
username: hads
password: ${{ secrets.PAT }}
- name: Build
run: go build -v ./...
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# - name: Test
# run: go test -v ./...
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
push: true
tags: git.nice.net.nz/hads/snice:latest

34
Dockerfile Normal file
View File

@@ -0,0 +1,34 @@
# syntax=docker/dockerfile:1
FROM golang:alpine AS builder
ENV VERSION 0.1.0
ENV CGO_ENABLED 0
ENV GOOS linux
ENV BUILD_DIR /build
RUN mkdir -p ${BUILD_DIR}
WORKDIR ${BUILD_DIR}
RUN adduser --system --no-create-home --uid 1000 --shell /sbin/nologin app
COPY go.* ./
COPY *.go ./
RUN go build -v -o / ./...
FROM scratch
USER app
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
ENTRYPOINT ["/snice"]
CMD ["serve"]

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

10
go.mod
View File

@@ -1,14 +1,14 @@
module git.nice.net.nz/hads/snice
go 1.21.3
go 1.22.2
require (
github.com/urfave/cli/v2 v2.25.7
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
github.com/lmittmann/tint v1.1.2
github.com/urfave/cli/v2 v2.27.7
)
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
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-20201216005158-039620a65673 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
)

46
go.sum
View File

@@ -1,10 +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=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
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=

169
main.go
View File

@@ -2,34 +2,103 @@ package main
import (
"fmt"
"log"
"log/slog"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/lmittmann/tint"
"github.com/urfave/cli/v2"
"golang.org/x/exp/slog"
)
func serveLogger(logger *log.Logger, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remoteHost, _, _ := strings.Cut(r.RemoteAddr, ":")
logger.Printf("%v %v %v\n", remoteHost, r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
type (
// struct for holding response details
responseData struct {
status int
size int
}
// our http.ResponseWriter implementation
loggingResponseWriter struct {
http.ResponseWriter // compose original http.ResponseWriter
responseData *responseData
}
)
func (r *loggingResponseWriter) Write(b []byte) (int, error) {
size, err := r.ResponseWriter.Write(b) // write response using original http.ResponseWriter
r.responseData.size += size // capture size
return size, err
}
func timeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, time.Now().Format("02 Jan 2006 15:04:05 MST"))
func (r *loggingResponseWriter) WriteHeader(statusCode int) {
r.ResponseWriter.WriteHeader(statusCode) // write status code using original http.ResponseWriter
r.responseData.status = statusCode // capture status code
}
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,
}
lrw := loggingResponseWriter{
ResponseWriter: w, // compose original http.ResponseWriter
responseData: responseData,
}
h.ServeHTTP(&lrw, r) // inject our implementation of http.ResponseWriter
duration := time.Since(start)
// 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.String("duration", duration.String()),
slog.Int("size", responseData.size),
)
}
return http.HandlerFunc(loggingFn)
}
func pingHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}
func main() {
// handler := slog.NewJSONHandler(os.Stdout, nil)
handler := slog.NewTextHandler(os.Stdout, nil)
slog.SetDefault(slog.New(handler))
logger := slog.NewLogLogger(handler, slog.LevelError)
logLevel := &slog.LevelVar{} // INFO
opts := tint.Options{
Level: logLevel,
}
logLevel.Set(slog.LevelDebug)
handler := tint.NewHandler(os.Stdout, &opts)
logger := slog.New(handler)
slog.SetDefault(logger)
var host string
var port string
@@ -38,8 +107,15 @@ func main() {
app := &cli.App{
Name: "snice",
Usage: "Serve Static Files",
Version: "v0.1.0",
DefaultCommand: "",
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",
@@ -48,24 +124,30 @@ func main() {
Action: func(cCtx *cli.Context) error {
var addr string = host + ":" + port
mux := http.NewServeMux()
mux.Handle("/ping", http.HandlerFunc(pingHandler))
fileHandler := http.FileServer(http.Dir(directory))
mux.Handle("/", fileHandler)
srv := &http.Server{
Addr: addr,
ErrorLog: logger,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
mux := http.NewServeMux()
mux.HandleFunc("/time", timeHandler)
fileHandler := serveLogger(logger, http.FileServer(http.Dir(directory)))
mux.Handle("/", fileHandler)
srv.Handler = mux
srv.Handler = WithLogging(mux)
listener, err := net.Listen("tcp", addr)
if err != nil {
logger.Println(err)
slog.Error("Listen error", err)
}
scheme := "http://"
logger.Printf("Serving directory %q on %v%v", directory, scheme, 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.Println(err)
slog.Error("Serve error", err)
}
return nil
},
@@ -74,14 +156,14 @@ func main() {
Name: "directory",
Aliases: []string{"dir", "d"},
EnvVars: []string{"DIRECTORY"},
Value: ".",
Value: "/srv",
Usage: "Directory to serve",
Destination: &directory,
},
&cli.StringFlag{
Name: "host",
EnvVars: []string{"HOST"},
Value: "127.0.0.1",
Value: "0.0.0.0",
Usage: "Host to listen on",
Destination: &host,
}},
@@ -91,11 +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/health", 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,
}
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"}},
@@ -108,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
View File

@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>hads/renovate-config"
]
}