From 10f96562191baf36b66d1811f5d91c362e8764da Mon Sep 17 00:00:00 2001 From: Jamie Banks Date: Fri, 17 Jul 2026 01:14:19 +0100 Subject: [PATCH] fix: add main.go to git --- .gitignore | 2 +- cmd/nucleus/main.go | 69 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/nucleus/main.go diff --git a/.gitignore b/.gitignore index 897b0e3..0fa1df8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -nucleus +/nucleus cmd/nucleus/dist \ No newline at end of file diff --git a/cmd/nucleus/main.go b/cmd/nucleus/main.go new file mode 100644 index 0000000..2546eec --- /dev/null +++ b/cmd/nucleus/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "embed" + "io/fs" + "log" + "net/http" + "os" + + "nucleus/internal/api" + "nucleus/internal/db" + "nucleus/internal/scheduler" +) + +//go:embed dist/* +var staticFiles embed.FS + +type spaHandler struct { + staticPath string + indexPath string + fs http.FileSystem +} + +func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + path := r.URL.Path + + // Check if file exists + f, err := h.fs.Open(path) + if os.IsNotExist(err) { + // fallback to index.html + r.URL.Path = "/" + } else if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } else { + // If file exists, check if it's a directory + stat, err := f.Stat() + if err == nil && stat.IsDir() { + r.URL.Path = "/" + } + f.Close() + } + + http.FileServer(h.fs).ServeHTTP(w, r) +} + +func main() { + db.InitDB("app.db") + scheduler.InitScheduler() + + mux := http.NewServeMux() + + api.RegisterRoutes(mux) + + // Setup embedded SPA + distFS, err := fs.Sub(staticFiles, "dist") + if err != nil { + // If running locally without build, this might fail, fallback to a simple message + log.Println("dist/ not found in embedded filesystem. Ensure frontend is built before compiling. Serving API only.") + } else { + spa := spaHandler{staticPath: "/", indexPath: "index.html", fs: http.FS(distFS)} + mux.Handle("/", spa) + } + + log.Println("Starting server on :8080") + if err := http.ListenAndServe(":8080", mux); err != nil { + log.Fatal(err) + } +}