Skip to content

Commit

Permalink
This branch is going away because I don't like it
Browse files Browse the repository at this point in the history
  • Loading branch information
CtrlSpice committed Jan 16, 2025
1 parent 2755a74 commit d7ff9f5
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 25 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ build-go:

.PHONY: test-go
test-go:
cd desktopexporter; go test ./...
cd desktopexporter; DEV=false go test ./...

.PHONY: run-go
run-go:
cd desktopcollector; go run ./... --dev
cd desktopcollector; DEV=true STATIC_DIR="/desktopexporter/internal/server/static/" go run ./...

.PHONY: run-db-go
run-db-go:
cd desktopcollector; go run ./... --dev --db ../duck.db
cd desktopcollector; DEV=true STATIC_DIR="/desktopexporter/internal/server/static/" go run ./... --db ../duck.db

.PHONY: build-js
build-js:
Expand Down
3 changes: 0 additions & 3 deletions desktopcollector/main.go

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

4 changes: 0 additions & 4 deletions desktopexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ type Config struct {

// 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
13 changes: 10 additions & 3 deletions desktopexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import (
"github.com/CtrlSpice/otel-desktop-viewer/desktopexporter/internal/telemetry"
)

type EnvConfig struct {
IsDev bool
IsCI bool
StaticDir string
}

type desktopExporter struct {
server *server.Server
}

func newDesktopExporter(cfg *Config) *desktopExporter {
server := server.NewServer(cfg.Endpoint, cfg.DbPath, cfg.IsDev)
s := server.NewServer(cfg.Endpoint, cfg.DbPath)

return &desktopExporter{
server: server,
server: s,
}
}

Expand Down Expand Up @@ -54,7 +61,7 @@ func (exporter *desktopExporter) Start(ctx context.Context, host component.Host)
err := exporter.server.Start()

if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
fmt.Println("server closed")
} else if err != nil {
fmt.Printf("error listening for server: %s\n", err)
}
Expand Down
66 changes: 56 additions & 10 deletions desktopexporter/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/pkg/browser"
Expand All @@ -19,18 +21,25 @@ import (
//go:embed static/*
var assets embed.FS

type EnvConfig struct {
IsDev bool
IsCI bool
StaticDir string
}

type Server struct {
server http.Server
Store *store.Store
isDev bool
Env EnvConfig
}

func NewServer(endpoint string, dbPath string, isDev bool) *Server {
func NewServer(endpoint string, dbPath string) *Server {
s := Server{
server: http.Server{
Addr: endpoint,
},
Store: store.NewStore(context.Background(), dbPath),
Env: GetEnvConfig(),
}
s.server.Handler = s.Handler()
return &s
Expand All @@ -39,14 +48,12 @@ func NewServer(endpoint string, dbPath string, isDev bool) *Server {
func (s *Server) Start() error {
defer s.Store.Close()

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

if !isCI {
if !s.Env.IsCI {
go func() {
// Wait a bit for the server to come up to avoid a 404 as a first experience
time.Sleep(250 * time.Millisecond)
endpoint := s.server.Addr
browser.OpenURL("http://" + endpoint + "/")
browser.OpenURL("http://" + endpoint)
}()
}
return s.server.ListenAndServe()
Expand All @@ -64,9 +71,10 @@ func (s *Server) Handler() http.Handler {
router.HandleFunc("GET /api/clearData", s.clearTracesHandler)
router.HandleFunc("GET /traces/{id}", s.indexHandler)

if s.isDev {
if s.Env.IsDev {
log.Println(s.Env.StaticDir)
// Serve front-end from the filesystem
router.Handle("/", http.FileServer(http.Dir("./static/")))
router.Handle("/", http.FileServer(http.Dir("/Users/T998182/Workspace/otel-desktop-viewer/desktopexporter/internal/server/static/")))
} else {
// Serve front end from embedded static content
staticContent, err := fs.Sub(assets, "static")
Expand Down Expand Up @@ -120,8 +128,10 @@ func (s *Server) traceIDHandler(writer http.ResponseWriter, request *http.Reques
}

func (s *Server) indexHandler(writer http.ResponseWriter, request *http.Request) {
if s.isDev {
http.ServeFile(writer, request, "./desktopexporter/internal/server/static/index.html")
if s.Env.IsDev {
log.Println("IDX HANDLER RAN " + s.Env.StaticDir)
//http.ServeFile(writer, request, s.Env.StaticDir)
http.ServeFile(writer, request, "/Users/T998182/Workspace/otel-desktop-viewer/desktopexporter/internal/server/static/index.html")
} else {
indexBytes, err := assets.ReadFile("static/index.html")
if err != nil {
Expand All @@ -144,3 +154,39 @@ func writeJSON(writer http.ResponseWriter, data any) {
writer.Header().Set("Content-Type", "application/json")
writer.Write(jsonData)
}

func GetEnvConfig() EnvConfig {
isDev := isSet("DEV")
isCI := isSet("CI")
wd, err := os.Getwd()
if err != nil {
log.Fatalln("could not get current directory to set env config")
}

relStaticDir, sdSet := os.LookupEnv("STATIC_DIR")
if isDev && !sdSet {
log.Fatalln("the STATIC_DIR environment variable must be set when working in DEV")
}

sd := wd + filepath.Clean(relStaticDir)

return EnvConfig{
IsDev: isDev,
IsCI: isCI,
StaticDir: sd,
}
}

func isSet(key string) bool {
str, ok := os.LookupEnv(key)
if !ok {
return false
}

val, err := strconv.ParseBool(str)
if err != nil {
return false
}

return val
}
7 changes: 5 additions & 2 deletions desktopexporter/internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
)

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

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

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

func setupWithTrace(t *testing.T) (*httptest.Server, func(*testing.T)) {
server := NewServer("localhost:8000", "", false)
server := NewServer("localhost:8000", "")

testSpanData := telemetry.SpanData{
TraceID: "1234567890",
TraceState: "",
Expand Down

0 comments on commit d7ff9f5

Please sign in to comment.