2022-11-06 15:36:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/qa/api"
|
|
|
|
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-contrib/sessions/memstore"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type App struct {
|
|
|
|
router *gin.Engine
|
|
|
|
srv *http.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewApp(baseURL string) App {
|
|
|
|
gin.ForceConsoleColor()
|
|
|
|
router := gin.Default()
|
|
|
|
|
2023-11-25 12:43:41 +00:00
|
|
|
api.InitializeGitLabOIDC(baseURL)
|
|
|
|
|
2022-11-06 15:36:31 +00:00
|
|
|
store := memstore.NewStore([]byte("secret"))
|
|
|
|
router.Use(sessions.Sessions("qa-session", store))
|
2023-11-25 12:43:41 +00:00
|
|
|
router.Use(func(c *gin.Context) {
|
|
|
|
c.Set("baseurl", baseURL)
|
|
|
|
})
|
2022-11-06 15:36:31 +00:00
|
|
|
|
|
|
|
api.DeclareRoutes(router.Group(""))
|
|
|
|
|
|
|
|
var baserouter *gin.RouterGroup
|
|
|
|
if len(baseURL) > 0 {
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
|
|
|
c.Redirect(http.StatusFound, baseURL)
|
|
|
|
})
|
|
|
|
|
|
|
|
baserouter = router.Group(baseURL)
|
|
|
|
|
|
|
|
api.DeclareRoutes(baserouter)
|
|
|
|
declareStaticRoutes(baserouter, baseURL)
|
|
|
|
} else {
|
|
|
|
declareStaticRoutes(router.Group(""), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
app := App{
|
|
|
|
router: router,
|
|
|
|
}
|
|
|
|
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Start(bind string) {
|
|
|
|
app.srv = &http.Server{
|
2023-07-14 14:37:00 +00:00
|
|
|
Addr: bind,
|
|
|
|
Handler: app.router,
|
|
|
|
ReadHeaderTimeout: 15 * time.Second,
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 30 * time.Second,
|
2022-11-06 15:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatalf("listen: %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Stop() {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
if err := app.srv.Shutdown(ctx); err != nil {
|
|
|
|
log.Fatal("Server Shutdown:", err)
|
|
|
|
}
|
|
|
|
}
|