users/cmd/main.go
GnomeZworc c78316301f
v1.0.0: health: add a simple healthcheck
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2024-03-16 12:59:09 +01:00

82 lines
1.4 KiB
Go

package main
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"gitlab.g3e.fr/h6n/users/lib"
"database/sql"
_ "github.com/lib/pq"
)
var db *sql.DB = nil
func logout(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
func isLoggedIn(c echo.Context) error {
return c.NoContent(http.StatusNoContent)
}
func health(c echo.Context) error {
return c.NoContent(http.StatusOK)
}
func init_database() {
var err error
connStr := "postgres://acc:totor@postgres:5432/accounts?sslmode=disable"
db, err = sql.Open("postgres", connStr)
if err != nil {
fmt.Println(err)
}
}
func skip_auth(c echo.Context) bool {
if c.Request().Method == "POST" && c.Path() == "/" {
return true
}
if c.Request().Method == "GET" && c.Path() == "/health" {
return true
}
return false
}
func skip_log(c echo.Context) bool {
if c.Request().Method == "GET" && c.Path() == "/health" {
return true
}
return false
}
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)
// Start server
e.Logger.Fatal(e.Start(":1222"))
}