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.
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"github.com/aziis98/lupus-lite/auth"
|
|
"github.com/aziis98/lupus-lite/database"
|
|
"github.com/aziis98/lupus-lite/handlers"
|
|
"github.com/aziis98/lupus-lite/routes"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
"github.com/gofiber/websocket/v2"
|
|
)
|
|
|
|
func main() {
|
|
mode := os.Getenv("MODE")
|
|
|
|
app := fiber.New()
|
|
|
|
db, err := database.NewInMemoryDB()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
h := handlers.NewServer(db, auth.NewInMemoryAuthService)
|
|
|
|
app.Use(logger.New())
|
|
app.Use(recover.New())
|
|
|
|
// Static files
|
|
app.Static("/", "./_frontend/dist")
|
|
|
|
// Enable WebSocket on routes ending with "/ws"
|
|
app.Use("/*/ws", func(c *fiber.Ctx) error {
|
|
log.Printf("Upgrading to websocket connection")
|
|
if websocket.IsWebSocketUpgrade(c) {
|
|
c.Locals("allowed", true)
|
|
return c.Next()
|
|
}
|
|
return fiber.ErrUpgradeRequired
|
|
})
|
|
|
|
//
|
|
// Routes
|
|
//
|
|
|
|
app.Route("/", func(r fiber.Router) {
|
|
routes.PageRoutes(r, h)
|
|
})
|
|
|
|
app.Route("/api", func(r fiber.Router) {
|
|
routes.ApiRoutes(r, h)
|
|
})
|
|
|
|
//
|
|
// Starting the server
|
|
//
|
|
|
|
if strings.HasPrefix(mode, "dev") {
|
|
log.Printf(`Running dev server for frontend: "npm run dev"`)
|
|
|
|
err := exec.Command("sh", "-c", "cd _frontend/ && npm run dev").Start()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
log.Fatal(app.Listen(":4000"))
|
|
}
|