package main import ( "bufio" "io" "log" "os" "os/exec" "path" "strings" "github.com/fatih/color" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/config" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/database" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/executor" "git.phc.dm.unipi.it/phc/cluster-dashboard/backend/routes" ) const FrontendOutDir = "./out/frontend" func main() { ex := &executor.Mock{} // to try out locally this project in a reasonable way db := database.NewSimpleDatabase(ex) router := &routes.Router{ Database: db, } app := fiber.New() app.Use(logger.New()) app.Use(recover.New()) app.Static("/", FrontendOutDir) for _, entrypoint := range routes.HtmlEntrypoints { app.Get(entrypoint.Route, func(c *fiber.Ctx) error { return c.SendFile(path.Join(FrontendOutDir, entrypoint.Filename)) }) } app.Route("/api", router.Api) if strings.HasPrefix(config.Mode, "dev") { setupDevServer() } log.Fatal(app.Listen(config.Host)) } func setupDevServer() { log.Printf(`Running dev server for frontend: "npm run dev"`) cmd := exec.Command("sh", "-c", "npm run dev") cmdStdout, _ := cmd.StdoutPipe() cmdStderr, _ := cmd.StderrPipe() viteLogger := log.New(os.Stderr, color.HiGreenString("[ViteJS]")+" ", log.Ltime|log.Lmsgprefix) go func() { s := bufio.NewScanner(io.MultiReader(cmdStdout, cmdStderr)) for s.Scan() { viteLogger.Print(s.Text()) } if err := s.Err(); err != nil { viteLogger.Fatal(err) } }() err := cmd.Start() if err != nil { log.Fatal(err) } }