You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB
Go

2 years ago
package main
import (
"bufio"
"log"
"os/exec"
"strings"
"git.phc.dm.unipi.it/phc/storage/config"
"git.phc.dm.unipi.it/phc/storage/database"
"git.phc.dm.unipi.it/phc/storage/monitor"
"git.phc.dm.unipi.it/phc/storage/routes"
2 years ago
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
)
func main() {
// Setup services
db := database.NewJSON("database.local.json")
monitorService := monitor.NewService(&monitor.Config{
ScriptsDir: "./scripts",
})
if err := monitorService.LoadScripts(); err != nil {
panic(err)
}
// The router wraps all application dependencies and provides "routing" methods
2 years ago
router := &routes.Router{
Database: db,
Monitor: monitorService,
2 years ago
}
// We use go-fiber as the HTTP framework
2 years ago
app := fiber.New()
// Middlewares
2 years ago
app.Use(logger.New())
app.Use(recover.New())
// Static files
app.Static("/", "./_frontend/dist")
// Setting up dynamic "single page application" routes (all routes these to the same html file)
for _, route := range []string{
"/",
"/login",
"/admin",
"/api-keys",
"/buckets",
"/buckets/:bucket",
} {
app.Get(route, func(c *fiber.Ctx) error {
return c.SendFile("./_frontend/dist/index.html")
})
}
2 years ago
app.Route("/api", router.Api)
if strings.HasPrefix(config.Mode, "dev") {
2 years ago
log.Printf(`Running dev server for frontend: "npm run dev"`)
cmd := exec.Command("sh", "-c", "cd _frontend/ && npm run dev")
cmdStdout, _ := cmd.StdoutPipe()
go func() {
s := bufio.NewScanner(cmdStdout)
for s.Scan() {
log.Printf("[ViteJS] %s", s.Text())
}
if err := s.Err(); err != nil {
log.Fatal(err)
}
}()
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
}
log.Fatal(app.Listen(config.Host))
2 years ago
}