Files
gun-manager-backend/main.go

93 lines
2.1 KiB
Go

package main
import (
"flag"
"log"
"os"
"strings"
"git.siteworxpro.com/gun-manager/Handlers/Guns"
"git.siteworxpro.com/gun-manager/sql"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
log2 "github.com/labstack/gommon/log"
)
func main() {
var dbFile string
flag.StringVar(&dbFile, "database", "", "the database file to load")
flag.Parse()
if dbFile == "" {
flag.Usage()
os.Exit(1)
}
e := echo.New()
e.HideBanner = true
e.HidePort = true
e.Logger.SetOutput(os.Stdout)
e.Logger.SetLevel(log2.INFO)
db, err := sql.NewDb(dbFile)
if err != nil {
log.Fatal(err)
}
e.Logger.Infof("Using database file: %s", dbFile)
e.Logger.Infof("Running migrations for database file: %s", dbFile)
err = db.Migrate()
if err != nil {
log.Fatal(err)
}
e.Logger.Infof("Database file %s is ready", dbFile)
var corsOriginsList []string
corsOrigins := os.Getenv("CORS_ORIGINS")
if corsOrigins != "" {
corsOriginsList = strings.Split(corsOrigins, ",")
} else {
corsOriginsList = []string{
"https://localhost",
"https://127.0.0.1",
"http://127.0.0.1:5173",
"http://localhost:5173",
"http://127.0.0.1:4173",
"http://127.0.0.1:8000",
}
}
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: corsOriginsList,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
}))
// Serve static files from the "dist" directory
e.Static("/", "dist")
// Gun management routes
e.GET("/gun", Guns.Get)
e.PUT("/gun/:id", Guns.UpdateGun)
e.GET("/gun/:id", Guns.GetById)
e.DELETE("/gun/:id", Guns.DeleteById)
e.POST("/gun", Guns.Post)
// Photo download routes
e.GET("/gun/photo/:id/:filename", Guns.GetPhoto)
e.GET("/gun/photo/:id/:size/:filename", Guns.GetPhotoResize)
// Photo management routes
e.POST("/gun/photo/:id", Guns.PostPhoto)
e.DELETE("/gun/photo/:id", Guns.DeletePhoto)
e.Logger.Info("Starting server on port 8000")
e.Logger.Fatal(e.Start(":8000"))
}