-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Stage 1: Build the Go binary | ||
FROM golang:latest AS builder | ||
|
||
WORKDIR /app | ||
|
||
# Copy the Go project files | ||
COPY . . | ||
|
||
|
||
RUN go install github.com/a-h/templ/cmd/templ@latest && templ generate | ||
|
||
# Build the Go binary for the desired architecture (amd64 in this case) | ||
RUN CGO_ENABLED=0 go build -o myapp -tags dev | ||
|
||
|
||
# Stage 2: Create a minimal production image | ||
FROM arm64v8/ubuntu:22.04 | ||
|
||
RUN apt update && apt install -y bash ca-certificates && rm -rf /var/cache/apt/* | ||
WORKDIR /app | ||
|
||
# Copy only the binary from the previous stage | ||
COPY --from=builder /app/myapp . | ||
COPY ./assets ./assets | ||
|
||
# Expose the port that your application listens on | ||
EXPOSE 443 | ||
EXPOSE 80 | ||
|
||
VOLUME ["/certs"] | ||
|
||
|
||
# Command to run the executable | ||
ENTRYPOINT ["./myapp"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//go:build !prod | ||
// +build !prod | ||
|
||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func (s *ApiServer) Run() { | ||
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) | ||
serverLogger := slog.NewLogLogger(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}), slog.LevelDebug) | ||
|
||
router := mux.NewRouter() | ||
router.HandleFunc("/", makeHTTPHandleFunc(s.handleHome)) | ||
router.HandleFunc("/games/{id}", makeHTTPHandleFunc(s.handleGames)) | ||
router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", s.fs)) | ||
|
||
loggingMiddleware := LoggingMiddleware(logger) | ||
loggedRouter := loggingMiddleware(router) | ||
|
||
httpServer := &http.Server{ | ||
ReadTimeout: 5 * time.Second, | ||
WriteTimeout: 5 * time.Second, | ||
IdleTimeout: 120 * time.Second, | ||
Addr: ":http", | ||
Handler: loggedRouter, | ||
ErrorLog: serverLogger, | ||
} | ||
|
||
if err := httpServer.ListenAndServe(); err != nil { | ||
logger.Error("Failed to start HTTP server", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
//go:build prod | ||
// +build prod | ||
|
||
package main | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"golang.org/x/crypto/acme/autocert" | ||
) | ||
|
||
func (s *ApiServer) Run() { | ||
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug})) | ||
serverLogger := slog.NewLogLogger(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}), slog.LevelDebug) | ||
|
||
router := mux.NewRouter() | ||
router.HandleFunc("/", makeHTTPHandleFunc(s.handleHome)) | ||
router.HandleFunc("/games/{id}", makeHTTPHandleFunc(s.handleGames)) | ||
router.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/", s.fs)) | ||
|
||
loggingMiddleware := LoggingMiddleware(logger) | ||
loggedRouter := loggingMiddleware(router) | ||
|
||
certManager := &autocert.Manager{ | ||
Prompt: autocert.AcceptTOS, | ||
HostPolicy: autocert.HostWhitelist(s.domainName), | ||
Cache: autocert.DirCache("/certs"), | ||
} | ||
|
||
httpsServer := &http.Server{ | ||
ReadTimeout: 5 * time.Second, | ||
WriteTimeout: 5 * time.Second, | ||
IdleTimeout: 120 * time.Second, | ||
Addr: ":https", | ||
TLSConfig: certManager.TLSConfig(), | ||
Handler: loggedRouter, | ||
ErrorLog: serverLogger, | ||
} | ||
|
||
logger.Info("Starting HTTPS sever") | ||
if err := httpsServer.ListenAndServeTLS("", ""); err != nil { | ||
logger.Error("Failed to start HTTPS server", err) | ||
os.Exit(1) | ||
} | ||
} |