Skip to content

Commit

Permalink
a single envar is fine
Browse files Browse the repository at this point in the history
  • Loading branch information
CtrlSpice committed Jan 29, 2025
1 parent bad2c6e commit 42454dc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ test-go:

.PHONY: run-go
run-go:
cd desktopcollector; SERVE_FROM_FS=true STATIC_DIR=$(abspath ./desktopexporter/internal/server/static/) go run ./...
cd desktopcollector; STATIC_ASSETS_DIR=$(abspath ./desktopexporter/internal/server/static/) go run ./...

.PHONY: run-db-go
run-db-go:
cd desktopcollector; SERVE_FROM_FS=true STATIC_DIR=$(abspath ./desktopexporter/internal/server/static/) go run ./... --db ../duck.db
cd desktopcollector; STATIC_ASSETS_DIR=$(abspath ./desktopexporter/internal/server/static/) go run ./... --db ../duck.db

.PHONY: build-js
build-js:
Expand Down
44 changes: 15 additions & 29 deletions desktopexporter/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"path/filepath"

"github.com/CtrlSpice/otel-desktop-viewer/desktopexporter/internal/store"
"github.com/CtrlSpice/otel-desktop-viewer/desktopexporter/internal/telemetry"
Expand All @@ -18,24 +18,18 @@ import (
var assets embed.FS

type Server struct {
server http.Server
Store *store.Store
Env ServerEnv
}

type ServerEnv struct {
ServeFromFS bool
StaticDir string
server http.Server
Store *store.Store
staticDir string
}

func NewServer(endpoint string, dbPath string) *Server {
env := getServerEnv()
s := Server{
server: http.Server{
Addr: endpoint,
},
Store: store.NewStore(context.Background(), dbPath),
Env: env,
Store: store.NewStore(context.Background(), dbPath),
staticDir: getStaticDir(),
}

s.server.Handler = s.Handler()
Expand All @@ -59,8 +53,8 @@ func (s *Server) Handler() http.Handler {
router.HandleFunc("GET /api/clearData", s.clearTracesHandler)
router.HandleFunc("GET /traces/{id}", s.indexHandler)

if s.Env.ServeFromFS {
router.Handle("/", http.FileServer(http.Dir(s.Env.StaticDir)))
if s.staticDir != "" {
router.Handle("/", http.FileServer(http.Dir(s.staticDir)))
} else {
staticContent, err := fs.Sub(assets, "static")
if err != nil {
Expand Down Expand Up @@ -113,8 +107,8 @@ func (s *Server) traceIDHandler(writer http.ResponseWriter, request *http.Reques
}

func (s *Server) indexHandler(writer http.ResponseWriter, request *http.Request) {
if s.Env.ServeFromFS {
http.ServeFile(writer, request, s.Env.StaticDir+"/index.html")
if s.staticDir != "" {
http.ServeFile(writer, request, s.staticDir+"/index.html")
} else {
indexBytes, err := assets.ReadFile("static/index.html")
if err != nil {
Expand All @@ -138,19 +132,11 @@ func writeJSON(writer http.ResponseWriter, data any) {
writer.Write(jsonData)
}

func getServerEnv() ServerEnv {
serveFromFS, err := strconv.ParseBool(os.Getenv("SERVE_FROM_FS"))
if err != nil {
serveFromFS = false
}

staticDir := os.Getenv("STATIC_DIR")
if staticDir == "" {
serveFromFS = false
func getStaticDir() string {
staticDir, ok := os.LookupEnv("STATIC_ASSETS_DIR")
if ok {
return filepath.Clean(staticDir)
}

return ServerEnv{
ServeFromFS: serveFromFS,
StaticDir: staticDir,
}
return ""
}

0 comments on commit 42454dc

Please sign in to comment.