2024-06-06 16:28:14 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-06 17:59:58 -04:00
|
|
|
"context"
|
2024-06-06 16:28:14 -04:00
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
2024-06-20 19:36:38 -04:00
|
|
|
"net/http"
|
2024-06-06 16:28:14 -04:00
|
|
|
"os"
|
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
"codeberg.org/vlbeaudoin/voki/v3"
|
2024-06-06 17:59:58 -04:00
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2024-06-06 16:28:14 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-06-06 18:07:30 -04:00
|
|
|
// rootCmd represents the base command when called without any subcommands
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "bottin",
|
|
|
|
Short: "Bottin étudiant de l'AGECEM",
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute adds all child commands to the root command and sets flags appropriately.
|
|
|
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
|
|
func execute() {
|
|
|
|
err := rootCmd.Execute()
|
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 16:28:14 -04:00
|
|
|
// apiCmd represents the api command
|
|
|
|
var apiCmd = &cobra.Command{
|
|
|
|
Use: "api",
|
|
|
|
Short: "Démarrer le serveur API",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-06-06 17:59:58 -04:00
|
|
|
var cfg Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
|
|
log.Fatal("parse config:", err)
|
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
|
2024-06-06 17:59:58 -04:00
|
|
|
if cfg.API.Key != "" {
|
2024-06-06 16:28:14 -04:00
|
|
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
2024-06-06 17:59:58 -04:00
|
|
|
return subtle.ConstantTimeCompare([]byte(key), []byte(cfg.API.Key)) == 1, nil
|
2024-06-06 16:28:14 -04:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DataClient
|
2024-06-06 17:59:58 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
//prep
|
|
|
|
pool, err := pgxpool.New(
|
|
|
|
ctx,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"user=%s password=%s database=%s host=%s port=%d sslmode=%s ",
|
|
|
|
cfg.DB.User,
|
|
|
|
cfg.DB.Password,
|
|
|
|
cfg.DB.Database,
|
|
|
|
cfg.DB.Host,
|
|
|
|
cfg.DB.Port,
|
|
|
|
cfg.DB.SSLMode,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("init pgx pool:", err)
|
|
|
|
}
|
|
|
|
defer pool.Close()
|
2024-06-06 16:28:14 -04:00
|
|
|
|
2024-06-06 17:59:58 -04:00
|
|
|
db := &PostgresClient{
|
|
|
|
Ctx: ctx,
|
|
|
|
Pool: pool,
|
|
|
|
}
|
|
|
|
if err := db.Pool.Ping(ctx); err != nil {
|
|
|
|
log.Fatal("ping db:", err)
|
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
|
2024-06-06 17:59:58 -04:00
|
|
|
if err := db.CreateOrReplaceSchema(); err != nil {
|
|
|
|
log.Fatal("create or replace schema:", err)
|
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
|
2024-06-17 17:25:53 -04:00
|
|
|
if err := db.CreateOrReplaceViews(); err != nil {
|
|
|
|
log.Fatal("create or replace views:", err)
|
|
|
|
}
|
|
|
|
|
2024-06-06 16:28:14 -04:00
|
|
|
// Routes
|
2024-06-11 17:28:20 -04:00
|
|
|
if err := addRoutes(e, db); err != nil {
|
|
|
|
log.Fatal("add routes:", err)
|
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
/*
|
|
|
|
h := handlers.New(client)
|
|
|
|
|
|
|
|
e.GET("/v7/health/", h.GetHealth)
|
|
|
|
|
|
|
|
e.POST("/v7/membres/", h.PostMembres)
|
|
|
|
|
|
|
|
e.GET("/v7/membres/", h.ListMembres)
|
|
|
|
|
|
|
|
e.GET("/v7/membres/:membre_id/", h.ReadMembre)
|
|
|
|
|
|
|
|
e.PUT("/v7/membres/:membre_id/prefered_name/", h.PutMembrePreferedName)
|
|
|
|
|
|
|
|
e.POST("/v7/programmes/", h.PostProgrammes)
|
|
|
|
|
|
|
|
e.POST("/v7/seed/", h.PostSeed)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Execution
|
2024-06-06 17:59:58 -04:00
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.API.Port)))
|
2024-06-06 16:28:14 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// webCmd represents the web command
|
|
|
|
var webCmd = &cobra.Command{
|
|
|
|
Use: "web",
|
|
|
|
Short: "Démarrer le client web",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2024-06-20 19:36:38 -04:00
|
|
|
// Parse config
|
2024-06-06 17:59:58 -04:00
|
|
|
var cfg Config
|
|
|
|
if err := viper.Unmarshal(&cfg); err != nil {
|
|
|
|
log.Fatal("init config:", err)
|
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
// Trailing slash
|
2024-06-06 16:28:14 -04:00
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
// Auth
|
2024-06-06 16:28:14 -04:00
|
|
|
e.Use(middleware.BasicAuth(func(user, password string, c echo.Context) (bool, error) {
|
2024-06-06 17:59:58 -04:00
|
|
|
usersMatch := subtle.ConstantTimeCompare([]byte(user), []byte(cfg.Web.User)) == 1
|
|
|
|
passwordsMatch := subtle.ConstantTimeCompare([]byte(password), []byte(cfg.Web.Password)) == 1
|
2024-06-06 16:28:14 -04:00
|
|
|
return usersMatch && passwordsMatch, nil
|
|
|
|
}))
|
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
// Templating
|
|
|
|
e.Renderer = &Template{
|
2024-06-06 16:28:14 -04:00
|
|
|
templates: template.Must(template.ParseFS(templatesFS, "templates/*.html")),
|
|
|
|
}
|
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
// API Client
|
|
|
|
apiClient := APIClient{voki.New(
|
|
|
|
http.DefaultClient,
|
|
|
|
cfg.Web.API.Host,
|
|
|
|
cfg.Web.API.Key,
|
|
|
|
cfg.Web.API.Port,
|
|
|
|
cfg.Web.API.Protocol,
|
|
|
|
)}
|
|
|
|
defer apiClient.Voki.CloseIdleConnections()
|
2024-06-06 16:28:14 -04:00
|
|
|
|
|
|
|
// Routes
|
2024-06-20 19:36:38 -04:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
|
|
|
pingResult, err := apiClient.GetHealth()
|
|
|
|
if err != nil {
|
2024-06-20 19:54:41 -04:00
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
|
|
|
voki.MessageResponse{Message: fmt.Sprintf("impossible d'accéder au serveur API: %s", err)},
|
|
|
|
)
|
2024-06-20 19:36:38 -04:00
|
|
|
}
|
2024-06-06 16:28:14 -04:00
|
|
|
|
2024-06-20 19:36:38 -04:00
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
|
|
|
voki.MessageResponse{Message: pingResult},
|
|
|
|
)
|
|
|
|
})
|
2024-06-06 16:28:14 -04:00
|
|
|
|
2024-06-20 19:55:12 -04:00
|
|
|
e.GET("/membre/", func(c echo.Context) error {
|
|
|
|
membreID := c.QueryParam("membre_id")
|
|
|
|
switch {
|
|
|
|
case membreID == "":
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
2024-06-20 20:16:33 -04:00
|
|
|
voki.MessageResponse{Message: "❗Veuillez entrer un numéro étudiant à rechercher"},
|
2024-06-20 19:55:12 -04:00
|
|
|
)
|
|
|
|
case !IsMembreID(membreID):
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
2024-06-20 20:16:33 -04:00
|
|
|
voki.MessageResponse{Message: fmt.Sprintf("❗Numéro étudiant '%s' invalide", membreID)},
|
2024-06-20 19:55:12 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
membre, err := apiClient.GetMembreForDisplay(membreID)
|
|
|
|
if err != nil {
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
2024-06-20 20:16:33 -04:00
|
|
|
voki.MessageResponse{Message: fmt.Sprintf("❗erreur: %s", err)},
|
2024-06-20 19:55:12 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"index-html",
|
|
|
|
voki.MessageResponse{Message: fmt.Sprintf(`
|
|
|
|
Numéro étudiant: %s
|
|
|
|
Nom d'usage: %s
|
|
|
|
Programme: [%s] %s
|
|
|
|
`,
|
|
|
|
membre.ID,
|
|
|
|
membre.Name,
|
|
|
|
membre.ProgrammeID,
|
|
|
|
membre.ProgrammeName,
|
|
|
|
)},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2024-06-06 16:28:14 -04:00
|
|
|
// Execution
|
|
|
|
e.Logger.Fatal(e.Start(
|
2024-06-06 17:59:58 -04:00
|
|
|
fmt.Sprintf(":%d", cfg.Web.Port)))
|
2024-06-06 16:28:14 -04:00
|
|
|
},
|
|
|
|
}
|