Skip to content

Commit

Permalink
embed the frontend over carrying it along with the exe everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed May 7, 2024
1 parent a3fe146 commit 46151bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
12 changes: 9 additions & 3 deletions internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"strconv"
"strings"

metrics "github.com/canonical/gocert/internal/metrics"
"github.com/canonical/gocert/ui"
)

// NewGoCertRouter takes in an environment struct, passes it along to any handlers that will need
Expand All @@ -26,7 +28,7 @@ func NewGoCertRouter(env *Environment) http.Handler {
apiV1Router.HandleFunc("DELETE /certificate_requests/{id}/certificate", DeleteCertificate(env))

metricsHandler := metrics.NewPrometheusMetricsHandler()
frontendHandler := newFrontendFileServer(env.FrontendDir)
frontendHandler := newFrontendFileServer()

router := http.NewServeMux()
router.HandleFunc("/status", HealthCheck)
Expand All @@ -37,8 +39,12 @@ func NewGoCertRouter(env *Environment) http.Handler {
return logging(router)
}

func newFrontendFileServer(dir string) http.Handler {
return http.FileServer(http.Dir(dir))
func newFrontendFileServer() http.Handler {
frontendFS, err := fs.Sub(ui.FrontendFS, "out")
if err != nil {
log.Fatal(err)
}
return http.FileServer(http.FS(frontendFS))
}

// the health check endpoint simply returns a http.StatusOK
Expand Down
14 changes: 5 additions & 9 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ type ConfigYAML struct {
}

type Config struct {
Key []byte
Cert []byte
DBPath string
FrontendDir string
Port int
Key []byte
Cert []byte
DBPath string
Port int
}

type Environment struct {
DB *certdb.CertificateRequestsRepository
FrontendDir string
DB *certdb.CertificateRequestsRepository
}

// validateConfigFile opens and processes the given yaml file, and catches errors in the process
Expand Down Expand Up @@ -67,7 +65,6 @@ func validateConfigFile(filePath string) (Config, error) {
config.Key = key
config.DBPath = c.DBPath
config.Port = c.Port
config.FrontendDir = "ui/out"
return config, nil
}

Expand All @@ -88,7 +85,6 @@ func NewServer(configFile string) (*http.Server, error) {

env := &Environment{}
env.DB = db
env.FrontendDir = config.FrontendDir
router := NewGoCertRouter(env)

s := &http.Server{
Expand Down
6 changes: 6 additions & 0 deletions ui/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ui

import "embed"

//go:embed all:out

Check failure on line 5 in ui/embed.go

View workflow job for this annotation

GitHub Actions / go-vet

pattern all:out: no matching files found

Check failure on line 5 in ui/embed.go

View workflow job for this annotation

GitHub Actions / lint

pattern all:out: no matching files found (typecheck)

Check failure on line 5 in ui/embed.go

View workflow job for this annotation

GitHub Actions / go-build

pattern all:out: no matching files found

Check failure on line 5 in ui/embed.go

View workflow job for this annotation

GitHub Actions / go-unit-tests

pattern all:out: no matching files found
var FrontendFS embed.FS

0 comments on commit 46151bc

Please sign in to comment.