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"
|
|
|
|
"os"
|
|
|
|
|
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-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
|
|
|
|
|
|
|
// Ping API server
|
|
|
|
/*
|
|
|
|
client := http.DefaultClient
|
|
|
|
defer client.CloseIdleConnections()
|
|
|
|
|
|
|
|
apiClient := data.NewApiClient(client, webApiKey, webApiHost, webApiProtocol, webApiPort)
|
|
|
|
|
|
|
|
pingResult, err := apiClient.GetHealth()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(pingResult)
|
|
|
|
*/
|
|
|
|
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
|
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Template
|
|
|
|
|
|
|
|
t := &Template{
|
|
|
|
templates: template.Must(template.ParseFS(templatesFS, "templates/*.html")),
|
|
|
|
}
|
|
|
|
|
|
|
|
e.Renderer = t
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
/*
|
|
|
|
handler := webhandlers.Handler{APIClient: apiClient}
|
|
|
|
|
|
|
|
e.GET("/", handler.GetIndex)
|
|
|
|
e.GET("/membre/", handler.GetMembre)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 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
|
|
|
},
|
|
|
|
}
|