Skip to content

Commit

Permalink
feat: add static file embedding #9
Browse files Browse the repository at this point in the history
  • Loading branch information
andygeiss committed Dec 31, 2024
1 parent a909416 commit f3d689b
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/service/assets/keepalive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK
7 changes: 6 additions & 1 deletion cmd/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main

import (
"context"
"embed"
"log"
"net/http"
"os"
Expand All @@ -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{
Expand All @@ -28,6 +32,7 @@ func main() {
Key: security.Getenv("ENCRYPTION_KEY"),
},
Server: config.Server{
Efs: efs,
Port: os.Getenv("PORT"),
},
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 9 additions & 5 deletions internal/app/adapters/inbound/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion internal/app/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import "embed"

type Config struct {
PortCloudSpanner PortCloudSpanner `json:"port_cloud_spanner"`
Server Server `json:"server"`
Expand All @@ -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 {
Expand Down

0 comments on commit f3d689b

Please sign in to comment.