68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
"net/http"
|
|
"xcpq_editor/internal/handlers"
|
|
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type HttpServer struct {
|
|
config *viper.Viper
|
|
router *gin.Engine
|
|
dbHandler *sql.DB
|
|
}
|
|
|
|
func InitHttpServer(config *viper.Viper, dbHandler *sql.DB) HttpServer {
|
|
|
|
// repository initialisation:
|
|
|
|
// service initialisation:
|
|
|
|
// controller initialisation:
|
|
|
|
// Service API
|
|
router := gin.Default()
|
|
router.Use(cors.Default())
|
|
|
|
// static content
|
|
router.Static("/static", "../static")
|
|
router.GET("/", handlers.Home)
|
|
router.GET("/properties", handlers.PropertiesHandler)
|
|
/*
|
|
router.GET("/products", handlers.ProductsHandler)
|
|
router.GET("/conditions", handlers.ConditionsHandler)
|
|
router.GET("/translations", handlers.TranslationsHandler)
|
|
*/
|
|
|
|
// import
|
|
router.POST("/import", handlers.ImportHandler_PropertyTypesGroup)
|
|
|
|
// test endpoint
|
|
router.GET("/test", Test)
|
|
|
|
// server
|
|
return HttpServer{
|
|
config: config,
|
|
router: router,
|
|
dbHandler: dbHandler,
|
|
}
|
|
}
|
|
|
|
func (hs HttpServer) Start() {
|
|
err := hs.router.Run(hs.config.GetString("http.server_address"))
|
|
if err != nil {
|
|
log.Fatalf("Error while starting HTTP server: %v", err)
|
|
}
|
|
}
|
|
|
|
func Test(ctx *gin.Context) {
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"message": "Test",
|
|
})
|
|
}
|