|
|
|
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/server"
|
|
|
|
"git.phc.dm.unipi.it/phc/storage/serverinfo"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
//
|
|
|
|
// Setup main services
|
|
|
|
//
|
|
|
|
|
|
|
|
db := database.NewJSON("database.local.json")
|
|
|
|
|
|
|
|
serverinfoService := serverinfo.NewService(&serverinfo.Config{
|
|
|
|
ScriptsDir: "./scripts",
|
|
|
|
CacheTimeout: config.MonitorCacheTimeout,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := serverinfoService.LoadScripts(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Setup server
|
|
|
|
//
|
|
|
|
|
|
|
|
// The server wraps all application dependencies and provides "routing" methods
|
|
|
|
server := server.New(db, serverinfoService)
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
"/",
|
|
|
|
"/login",
|
|
|
|
"/admin",
|
|
|
|
"/api-keys",
|
|
|
|
"/buckets",
|
|
|
|
"/buckets/:bucket",
|
|
|
|
} {
|
|
|
|
app.Get(route, func(c *fiber.Ctx) error {
|
|
|
|
return c.SendFile("./_frontend/dist/index.html")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup server routes
|
|
|
|
|
|
|
|
app.Route("/api", server.Api)
|
|
|
|
|
|
|
|
//
|
|
|
|
// ViteJS development server
|
|
|
|
//
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start backend the server
|
|
|
|
log.Fatal(app.Listen(config.Host))
|
|
|
|
}
|