diff --git a/.gitignore b/.gitignore index 7eb9e05..afbea79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp public/ resources/ +app diff --git a/.woodpecker.yml b/.woodpecker.yml index 54b91d0..ede0502 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -1,11 +1,3 @@ -# Build and publish Docker images for multible architectures. -# -# Pushing an image to codeberg as container registry, -# package owner will be the repo owner. -# -# this config also shows usage of yaml aliases and -# was taken from https://codeberg.org/6543/docker-images/src/commit/37e29c227717c1c07d2776cddcf14725bf952875/.woodpecker/hello.yml - when: branch: main @@ -14,13 +6,21 @@ variables: - &repo git.jmbit.de/${CI_REPO_OWNER}/${CI_REPO_NAME} steps: - dryrun: + hugo: image: git.jmbit.de/jmb/docker-hugo commands: - hugo --minify when: event: [ pull-request, push] + webserver: + image: golang:alpine + commands: + - go mod download && go mod verify + - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -buildvcs=true -o app . + when: + event: [ pull-request, push] + publish: image: woodpeckerci/plugin-docker-buildx settings: diff --git a/Dockerfile b/Dockerfile index 859ed95..8a5b523 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,11 @@ -FROM nginx:latest -COPY public /usr/share/nginx/html +FROM golang:alpine AS builder +RUN apk update && apk add --no-cache git + +WORKDIR $GOPATH/src/www-jmbit-de +COPY . . +RUN go get -d -v +RUN go build -o /go/bin/www + +FROM scratch +COPY --from=builder /go/bin/www /go/bin/www +ENTRYPOINT ["/go/bin/www"] diff --git a/Makefile b/Makefile index a685e2c..fd5010d 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,26 @@ HEAD=$(shell git rev-parse --short HEAD) +CTNAME:=git.jmbit.de/jmb/www-jmbit-de + +all: hugo container + dev: hugo server -D + hugo: - hugo + hugo --minify + +webserver: + CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o app . + container: - podman build -t docker.io/jmbitci/www-jmbit-de:latest -t docker.io/jmbitci/www-jmbit-de:$(HEAD) . -publish: - podman push docker.io/jmbitci/www-jmbit-de:latest --all-tags + podman build -t $(CTNAME):latest -t $(CTNAME):$(HEAD) . -nopub: hugo container - podman run --rm -p8080:80 docker.io/jmbitci/www-jmbit-de +nopub: hugo webserver container + podman run --rm -p8080:80 $(CTNAME) -#rollout: -# kubectl --context=jmbit-prod rollout restart deployment www-jmbit-de -n jmbit-web +run: + podman run --rm -p8080:80 $(CTNAME) clean: rm -rf public -all: hugo container publish - - diff --git a/server.go b/server.go index 0e0b582..33119db 100644 --- a/server.go +++ b/server.go @@ -1,23 +1,31 @@ package main import ( - "embed" "log" "net/http" - "github.com/gin-gonic/gin" + "git.jmbit.de/jmb/www-jmbit-de/public" ) -//go:embed public/* -var HtmlFS embed.FS - func main() { - router := gin.New() - gin.SetMode(gin.ReleaseMode) - router.Use(gin.Recovery()) + // Register a custom handler + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Specify the file path you want to block + blockFilePath := "/public.go" - router.StaticFS("/", http.FS(HtmlFS)) - err := router.Run("0.0.0.0:80") + // Check if the requested path matches the blocked file path + if r.URL.Path == blockFilePath { + // Return a 404 Not Found error + http.NotFound(w, r) + return + } + + // For other paths, serve the files using the file server + http.FileServer(http.FS(public.HtmlFS)).ServeHTTP(w, r) + }) + + // Start the HTTP server on port 80 + err := http.ListenAndServe(":80", nil) if err != nil { log.Fatal(err) } diff --git a/static/public.go b/static/public.go new file mode 100644 index 0000000..1283fe3 --- /dev/null +++ b/static/public.go @@ -0,0 +1,8 @@ +package public + +import ( + "embed" +) + +//go:embed * +var HtmlFS embed.FS