diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..bb9a96b --- /dev/null +++ b/Containerfile @@ -0,0 +1,38 @@ +# Build stage +FROM golang:alpine AS builder + +# Install build dependencies and UPX +RUN apk add --no-cache \ + musl-dev \ + gcc \ + wget \ + xz \ + git + +RUN wget https://github.com/upx/upx/releases/download/v5.0.2/upx-5.0.2-amd64_linux.tar.xz && \ + tar -xf upx-5.0.2-amd64_linux.tar.xz && \ + mv upx-5.0.2-amd64_linux/upx /usr/local/bin/upx && \ + rm -r upx-5.0.2-amd64_linux upx-5.0.2-amd64_linux.tar.xz + +WORKDIR /app + +# Copy go mod files first for better layer caching +COPY go.mod ./ +RUN go mod download + +# Copy source code +COPY . . + +# Create necessary directories, build, and compress with UPX +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags="-s -w -extldflags '-static' -X main.GOMEMLIMIT=50MiB -X runtime.defaultGOGC=50" -trimpath -gcflags="-l=4" -asmflags=-trimpath -o bin/main app/gofudge/main.go +RUN upx --best --ultra-brute bin/main +RUN chmod +x bin/main + +FROM scratch +WORKDIR /app + +# Copy only the built binary and necessary directories +COPY --from=builder /app/bin/main ./bin/main + +EXPOSE 8080 +ENTRYPOINT ["/app/bin/main"] diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index fba9f9e..0000000 --- a/Dockerfile +++ /dev/null @@ -1,36 +0,0 @@ -# Build stage -FROM golang:bookworm AS builder - -# Install UPX -RUN apt-get update && apt-get install -y wget xz-utils && rm -rf /var/lib/apt/lists/* - -# Download the latest UPX binary directly from GitHub -RUN wget https://github.com/upx/upx/releases/download/v5.0.2/upx-5.0.2-amd64_linux.tar.xz -RUN tar -xf upx-5.0.2-amd64_linux.tar.xz && mv upx-5.0.2-amd64_linux/upx /usr/local/bin/upx && rm -r upx-5.0.2-amd64_linux upx-5.0.2-amd64_linux.tar.xz - -# Create a simple Go web server -WORKDIR /app - -# Copy go mod files first for better layer caching -COPY go.mod ./ -RUN go mod download - -# Copy source code -COPY . . - -# Create necessary directories, build, and compress with UPX -RUN mkdir -p /var/sockets -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags="-s -w -extldflags '-static' -X main.GOMEMLIMIT=50MiB -X runtime.defaultGOGC=150" -trimpath -gcflags="-l=4" -asmflags=-trimpath -o bin/main app/gofudge/main.go -RUN upx --best --ultra-brute bin/main -RUN chmod +x bin/main - -# Final stage with Chainguard static -FROM cgr.dev/chainguard/static:latest -WORKDIR /app - -# Copy only the built binary and necessary directories -COPY --from=builder /app/bin/main ./bin/main - -EXPOSE 8080 -USER nonroot:nonroot -CMD ["./bin/main"] diff --git a/README.md b/README.md index a4d3417..0119cce 100644 --- a/README.md +++ b/README.md @@ -9,39 +9,97 @@ A Fudge Dice rolling room programmed in Go ## Prerequisites [![Go](https://img.shields.io/badge/go-%2300ADD8.svg?style=for-the-badge&logo=go&logoColor=white)](https://golang.org/dl/) -- The version of **Go** used to test the code in this repository is **1.25.3**. +- Go 1.25.3+ (only needed to build) +- Or just download a pre-built binary from Releases (when available) -## Get started - -- run the following commands: `go mod tidy; go build -o bin/main ./app/gofudge/main.go` then `./bin/main` to start the server on port 8080. +### Quick start (from source) +```bash +git clone https://codeberg.org/riomoo/gofudge.git +cd gofudge +go build -o gofudge app/gofudge/main.go +./gofudge +``` - Visit http://localhost:8080 in your browser. - - Upon visiting the URL you will be created with a username entry and Create room button. After that you will be in the room. - (If you are hosting this publicly) You can copy the room link in the top right hand corner and share it to anyone. They will be prompted to also pick a username. - From there you may increase/decrease the modifier as needed for the skill you are rolling for. -### Podman/Docker -- If you want to use this with Docker, replace all `podman` commands in `run.sh` instances with `docker` -- Use `run.sh` script which will start the site on port `12007` and to make it public change the `8080` port in the NGINX config to `12007` or change the port in the `run.sh` script how you like. -- To use it locally, same as above but visit http://localhost:12007 instead. +## If you want to use this with podman: +```bash +git clone https://codeberg.org/riomoo/gofudge.git +cd gofudge +./scripts-bash/run.sh +``` + +Then open http://localhost:12007 in your browser. ## Config for NGINX to use as a website: ``` upstream gofudge { server 127.0.0.1:8080; + #server 127.0.0.1:12007; #For Podman instead server [::1]:8080; + #server [::1]:12007; #For Podman instead } server { listen 80; listen [::1]:80; server_name fudge.example.com; + location /ws { + proxy_pass http://gofudge; + proxy_http_version 1.1; + + # WebSocket upgrade headers + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + # Standard proxy headers + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # WebSocket timeout settings (increase for long-lived connections) + proxy_connect_timeout 7d; + proxy_send_timeout 7d; + proxy_read_timeout 7d; + + # Disable buffering for WebSocket + proxy_buffering off; + + # Security headers + add_header X-Content-Type-Options nosniff; + add_header X-Frame-Options DENY; + add_header X-XSS-Protection "1; mode=block"; + add_header Referrer-Policy "strict-origin-when-cross-origin"; + } location / { proxy_pass http://gofudge; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; + + # Connection keep-alive for better performance + proxy_http_version 1.1; + proxy_set_header Connection ""; + + # Timeouts optimized for your simple site + proxy_connect_timeout 5s; + proxy_send_timeout 10s; + proxy_read_timeout 10s; + + # Enable buffering for better compression + proxy_buffering on; + proxy_buffer_size 4k; + proxy_buffers 8 4k; + + # Security headers + add_header X-Content-Type-Options nosniff; + add_header X-Frame-Options DENY; + add_header X-XSS-Protection "1; mode=block"; + add_header Referrer-Policy "strict-origin-when-cross-origin"; } } ``` @@ -51,6 +109,7 @@ server { ## Software Used but not included ![Arch](https://img.shields.io/badge/Arch%20Linux-1793D1?logo=arch-linux&logoColor=fff&style=for-the-badge) +![Podman](https://img.shields.io/badge/-Podman-892CA0?style=flat-square&logo=podman&logoColor=white) ![Vim](https://img.shields.io/badge/VIM-%2311AB00.svg?style=for-the-badge&logo=vim&logoColor=white) ![Git](https://img.shields.io/badge/git-%23F05033.svg?style=for-the-badge&logo=git&logoColor=white) ![Forgejo](https://img.shields.io/badge/forgejo-%23FB923C.svg?style=for-the-badge&logo=forgejo&logoColor=white) diff --git a/app/gofudge/main.go b/app/gofudge/main.go index 11b7336..2e933b3 100644 --- a/app/gofudge/main.go +++ b/app/gofudge/main.go @@ -506,10 +506,14 @@ func handleRoom(w http.ResponseWriter, r *http.Request) {

Roll Dice

-
- - -
+
+
+ + + +
+ +
@@ -525,7 +529,7 @@ func handleRoom(w http.ResponseWriter, r *http.Request) {