From f3d689b618f4369d88a83b3d7ffe57ae60002e56 Mon Sep 17 00:00:00 2001 From: Andy <4ndygeiss@gmail.com> Date: Tue, 31 Dec 2024 06:16:30 +0100 Subject: [PATCH] feat: add static file embedding #9 --- cmd/service/assets/keepalive.txt | 1 + cmd/service/main.go | 7 ++++++- go.mod | 2 +- go.sum | 4 ++-- internal/app/adapters/inbound/api/router.go | 14 +++++++++----- internal/app/config/config.go | 5 ++++- 6 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 cmd/service/assets/keepalive.txt diff --git a/cmd/service/assets/keepalive.txt b/cmd/service/assets/keepalive.txt new file mode 100644 index 0000000..d86bac9 --- /dev/null +++ b/cmd/service/assets/keepalive.txt @@ -0,0 +1 @@ +OK diff --git a/cmd/service/main.go b/cmd/service/main.go index 0151779..88d5289 100644 --- a/cmd/service/main.go +++ b/cmd/service/main.go @@ -3,6 +3,7 @@ package main import ( "context" + "embed" "log" "net/http" "os" @@ -15,6 +16,9 @@ import ( "github.com/andygeiss/cloud-native-utils/service" ) +//go:embed assets +var efs embed.FS + func main() { // Create a new configuration object. cfg := &config.Config{ @@ -28,6 +32,7 @@ func main() { Key: security.Getenv("ENCRYPTION_KEY"), }, Server: config.Server{ + Efs: efs, Port: os.Getenv("PORT"), }, } @@ -51,7 +56,7 @@ func main() { defer svc.Teardown() // Initialize the API router using the configuration object. - mux := api.Route(svc, ctx) + mux := api.Route(svc, ctx, cfg) // Create a new secure server. srv := security.NewServer(mux) diff --git a/go.mod b/go.mod index d5d90e0..9075c7f 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.23.2 require ( cloud.google.com/go/spanner v1.73.0 - github.com/andygeiss/cloud-native-utils v0.1.20 + github.com/andygeiss/cloud-native-utils v0.1.21 ) require ( diff --git a/go.sum b/go.sum index 3dba7d1..e74f0b5 100644 --- a/go.sum +++ b/go.sum @@ -626,8 +626,8 @@ github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/andygeiss/cloud-native-utils v0.1.20 h1:4G8fl+epgm2qXtw60YpHPxrMl52rVnLDNkBdH4/kTLQ= -github.com/andygeiss/cloud-native-utils v0.1.20/go.mod h1:vfzGOtGWQSFBS3I2JptmNOK6Syx+s7hppY4dC9H+KNs= +github.com/andygeiss/cloud-native-utils v0.1.21 h1:XJ2vNE9q9LVepsalglOLqB4y7IJoHshjJwHCECP88y8= +github.com/andygeiss/cloud-native-utils v0.1.21/go.mod h1:vfzGOtGWQSFBS3I2JptmNOK6Syx+s7hppY4dC9H+KNs= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0= github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI= diff --git a/internal/app/adapters/inbound/api/router.go b/internal/app/adapters/inbound/api/router.go index 5904687..9743652 100644 --- a/internal/app/adapters/inbound/api/router.go +++ b/internal/app/adapters/inbound/api/router.go @@ -4,15 +4,19 @@ import ( "context" "net/http" + "github.com/andygeiss/cloud-native-store/internal/app/config" "github.com/andygeiss/cloud-native-store/internal/app/core/services" "github.com/andygeiss/cloud-native-utils/security" ) -// Route creates a new mux with the health check endpoint (/health) -// and the store endpoints (/api/v1/store). -func Route(service *services.ObjectService, ctx context.Context) *http.ServeMux { - // Create a new mux with health check endpoint. - mux := security.Mux(ctx) +// Route creates a new mux with the liveness and readiness probe (/liveness, /readiness), +// the static assets endpoint (/) and the store endpoints (/api/v1/store). +func Route(service *services.ObjectService, ctx context.Context, cfg *config.Config) *http.ServeMux { + // Create a new mux with liveness and readyness endpoint. + // Embed the assets into the mux. + mux := security.Mux(ctx, cfg.Server.Efs) + + // Add the store endpoints to the mux. mux.HandleFunc("DELETE /api/v1/store", Delete(service)) mux.HandleFunc("GET /api/v1/store", Get(service)) mux.HandleFunc("PUT /api/v1/store", Put(service)) diff --git a/internal/app/config/config.go b/internal/app/config/config.go index 87435f3..283108e 100644 --- a/internal/app/config/config.go +++ b/internal/app/config/config.go @@ -1,5 +1,7 @@ package config +import "embed" + type Config struct { PortCloudSpanner PortCloudSpanner `json:"port_cloud_spanner"` Server Server `json:"server"` @@ -14,7 +16,8 @@ type PortCloudSpanner struct { } type Server struct { - Port string `json:"port"` + Efs embed.FS `json:"-"` + Port string `json:"port"` } type Service struct {