Skip to content

Commit

Permalink
fix: add build based on env
Browse files Browse the repository at this point in the history
  • Loading branch information
soockee committed Mar 21, 2024
1 parent cdf7024 commit 96e08e6
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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
RUN CGO_ENABLED=0 go build -o myapp -tags prod


# Stage 2: Create a minimal production image
Expand Down
34 changes: 34 additions & 0 deletions Dockerfile.dev
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"]
55 changes: 0 additions & 55 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@ package main
import (
"encoding/json"
"errors"
"log/slog"
"net/http"
"os"
"time"

"github.com/a-h/templ"
"github.com/gorilla/mux"
"golang.org/x/crypto/acme/autocert"

"github.com/soockee/ssr-go/components"
)
Expand Down Expand Up @@ -53,57 +49,6 @@ func makeHTTPHandleFunc(f apiFunc) http.HandlerFunc {
})
}

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)

if s.isProd {
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)
}
} else {
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)
}
}
}

func (s *ApiServer) handleHome(w http.ResponseWriter, r *http.Request) error {
switch r.Method {
case "GET":
Expand Down
40 changes: 40 additions & 0 deletions server_dev.go
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)
}
}
49 changes: 49 additions & 0 deletions server_prod.go
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)
}
}

0 comments on commit 96e08e6

Please sign in to comment.