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.
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
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"
|
|
|
|
"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
|
|
router := &routes.Router{
|
|
Database: db,
|
|
Monitor: monitorService,
|
|
}
|
|
|
|
// We use go-fiber as the HTTP framework
|
|
app := fiber.New()
|
|
|
|
// Middlewares
|
|
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{
|
|
"/",
|
|
"/api-keys",
|
|
"/admin",
|
|
"/buckets",
|
|
"/buckets/:bucket",
|
|
} {
|
|
app.Get(route, func(c *fiber.Ctx) error {
|
|
return c.SendFile("./_frontend/dist/index.html")
|
|
})
|
|
}
|
|
|
|
app.Route("/api", router.Api)
|
|
|
|
if strings.HasPrefix(config.Mode, "dev") {
|
|
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))
|
|
}
|