Skip to content

Commit

Permalink
Implement --dev flag
Browse files Browse the repository at this point in the history
  • Loading branch information
CtrlSpice committed Nov 28, 2024
1 parent 5a3ed57 commit 470b8fb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 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:
SERVE_FROM_FS=true cd desktopcollector; go run ./...
cd desktopcollector; go run ./... --dev

.PHONY: run-db-go
run-db-go:
SERVE_FROM_FS=true cd desktopcollector; go run ./... --db ../duck.db
cd desktopcollector; go run ./... --dev --db ../duck.db

.PHONY: build-js
build-js:
Expand Down
7 changes: 5 additions & 2 deletions desktopcollector/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion desktopexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ type Config struct {
// Endpoint defines the host and port where we serve our frontend app
Endpoint string `mapstructure:"endpoint"`

// Endpoint defines the path of your database file. Setting an enpty string opens DuckDB in in-memory mode
// DbPath defines the path of your database file. Setting an enpty string opens DuckDB in in-memory mode
DbPath string `mapstructure:"db"`

// IsDev launches the app in dev mode, which Avoids recompiling the back-end during
// front-end development by serving the latter from the filesystem instead of embeddeding it.
IsDev bool `mapstructure:"dev"`
}

// Validate checks if the exporter configuration is valid
Expand Down
2 changes: 1 addition & 1 deletion desktopexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type desktopExporter struct {
}

func newDesktopExporter(cfg *Config) *desktopExporter {
server := server.NewServer(cfg.Endpoint, cfg.DbPath)
server := server.NewServer(cfg.Endpoint, cfg.DbPath, cfg.IsDev)
return &desktopExporter{
server: server,
}
Expand Down
25 changes: 11 additions & 14 deletions desktopexporter/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log"
"net/http"
"os"
"strconv"
"time"

"github.com/pkg/browser"
Expand All @@ -23,29 +22,25 @@ var assets embed.FS
type Server struct {
server http.Server
Store *store.Store
isDev bool
}

func NewServer(endpoint string, dbPath string) *Server {
func NewServer(endpoint string, dbPath string, isDev bool) *Server {
s := Server{
server: http.Server{
Addr: endpoint,
},
Store: store.NewStore(context.Background(), dbPath),
}

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

s.server.Handler = s.Handler(serveFromFS)
s.server.Handler = s.Handler()
return &s
}

func (s *Server) Start() error {
defer s.Store.Close()

_, isCI := os.LookupEnv("CI")

if !isCI {
go func() {
// Wait a bit for the server to come up to avoid a 404 as a first experience
Expand All @@ -61,17 +56,19 @@ func (s *Server) Close() error {
return s.server.Close()
}

func (s *Server) Handler(serveFromFS bool) http.Handler {
func (s *Server) Handler() http.Handler {
router := http.NewServeMux()
router.HandleFunc("GET /api/traces", s.tracesHandler)
router.HandleFunc("GET /api/traces/{id}", s.traceIDHandler)
router.HandleFunc("GET /api/sampleData", s.sampleDataHandler)
router.HandleFunc("GET /api/clearData", s.clearTracesHandler)
router.HandleFunc("GET /traces/{id}", indexHandler)
router.HandleFunc("GET /traces/{id}", s.indexHandler)

if serveFromFS {
if s.isDev {
// Serve front-end from the filesystem
router.Handle("/", http.FileServer(http.Dir("./static/")))
} else {
// Serve front end from embedded static content
staticContent, err := fs.Sub(assets, "static")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -122,8 +119,8 @@ func (s *Server) traceIDHandler(writer http.ResponseWriter, request *http.Reques
}
}

func indexHandler(writer http.ResponseWriter, request *http.Request) {
if os.Getenv("SERVE_FROM_FS") == "true" {
func (s *Server) indexHandler(writer http.ResponseWriter, request *http.Request) {
if s.isDev {
http.ServeFile(writer, request, "./desktopexporter/internal/server/static/index.html")
} else {
indexBytes, err := assets.ReadFile("static/index.html")
Expand Down
8 changes: 4 additions & 4 deletions desktopexporter/internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

func setupEmpty() (*httptest.Server, func()) {
server := NewServer("localhost:8000", "")
testServer := httptest.NewServer(server.Handler(false))
server := NewServer("localhost:8000", "", false)
testServer := httptest.NewServer(server.Handler())

return testServer, func() {
testServer.Close()
Expand All @@ -26,7 +26,7 @@ func setupEmpty() (*httptest.Server, func()) {
}

func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) {
server := NewServer("localhost:8000", "")
server := NewServer("localhost:8000", "", false)
testSpanData := telemetry.SpanData{
TraceID: "1234567890",
TraceState: "",
Expand Down Expand Up @@ -56,7 +56,7 @@ func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) {
err := server.Store.AddSpans(context.Background(), []telemetry.SpanData{testSpanData})
assert.Nilf(t, err, "could not create test span: %v", err)

testServer := httptest.NewServer(server.Handler(false))
testServer := httptest.NewServer(server.Handler())

return testServer, func(t *testing.T) {
testServer.Close()
Expand Down

0 comments on commit 470b8fb

Please sign in to comment.