users/cmd/main.go
GnomeZworc d31f7d4e0a
v1.2.0: back: use health lib path
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2024-04-01 19:49:33 +02:00

108 lines
2.4 KiB
Go

package main
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
logins "gitlab.g3e.fr/h6n/users/internal"
"gitlab.g3e.fr/h6n/users/lib"
"gitlab.g3e.fr/h6n/users/lib/health"
"gorm.io/gorm"
_ "github.com/lib/pq"
_ "gitlab.g3e.fr/h6n/users/cmd/docs"
)
var dbGor *gorm.DB = nil
// Logout
// @Tags auth
// @Description Cette route permet de revoker sont token
// @Product json
// @Router / [delete]
// @Success 200 {object} string
// @Security ApiKeyAuth
func logout(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
// Logout
// @Tags auth
// @Description Cette route de verifier que le token est toujours valide
// @Product json
// @Router / [get]
// @Success 200 {object} string
// @Security ApiKeyAuth
func isLoggedIn(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
func init_database() {
var err error
connStr := "postgres://acc:totor@postgres:5432/accounts?sslmode=disable"
dbGor, err = logins.Init_database(connStr)
if err != nil {
fmt.Println(err)
}
}
func skip_auth(c echo.Context) bool {
if c.Request().Method == "POST" && c.Path() == "/" {
return true
}
if len(c.Path()) > 9 && c.Path()[:9] == "/swagger/" {
return true
}
return health.IsHealth(c)
}
func skip_log(c echo.Context) bool {
return health.IsHealth(c)
}
// @title Users
// @version 1.0
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @description Enter the token with the `Bearer: ` prefix, e.g. "Bearer abcde12345".
func main() {
init_database()
if err := lib.InitLoginBiscuit(); err != nil {
fmt.Println("error : ", err)
return
}
e := echo.New()
// Middleware
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Skipper: skip_log,
}))
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.Use(lib.AuthMiddleware(skip_auth))
// Routes
e.GET("/", isLoggedIn)
e.POST("/", login)
e.DELETE("/", logout)
e.GET("/health", health.Health)
e.GET("/swagger/*", echoSwagger.WrapHandler)
// Start server
e.Logger.Fatal(e.Start(":1222"))
}