2023-05-25 02:21:09 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2023-10-17 16:35:51 -04:00
|
|
|
"codeberg.org/vlbeaudoin/serpents"
|
2024-01-05 14:38:48 -05:00
|
|
|
"git.agecem.com/agecem/bottin/v6/data"
|
|
|
|
"git.agecem.com/agecem/bottin/v6/handlers"
|
2023-05-25 02:21:09 -04:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
apiPort int
|
|
|
|
apiKey string
|
|
|
|
)
|
|
|
|
|
2023-05-25 19:22:46 -04:00
|
|
|
// apiCmd represents the api command
|
|
|
|
var apiCmd = &cobra.Command{
|
|
|
|
Use: "api",
|
2023-05-25 02:21:09 -04:00
|
|
|
Short: "Démarrer le serveur API",
|
2023-05-25 19:22:46 -04:00
|
|
|
Args: cobra.ExactArgs(0),
|
2023-05-25 02:21:09 -04:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-05-25 19:22:46 -04:00
|
|
|
apiKey = viper.GetString("api.key")
|
|
|
|
apiPort = viper.GetInt("api.port")
|
|
|
|
|
2023-05-25 02:21:09 -04:00
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middlewares
|
|
|
|
|
|
|
|
e.Pre(middleware.AddTrailingSlash())
|
|
|
|
|
|
|
|
if apiKey != "" {
|
|
|
|
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
|
|
|
|
return subtle.ConstantTimeCompare([]byte(key), []byte(apiKey)) == 1, nil
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2023-09-18 22:55:40 -04:00
|
|
|
// DataClient
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-06-02 16:16:18 -04:00
|
|
|
client, err := data.NewDataClientFromViper()
|
2023-05-25 02:21:09 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
|
|
|
}
|
2023-06-08 19:47:25 -04:00
|
|
|
defer client.DB.Close()
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
err = client.DB.Ping()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Database was supposed to be ready but Ping() failed.\n Error: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:36:13 -04:00
|
|
|
_, err = client.Seed()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error during client.Seed(): %s", err)
|
|
|
|
}
|
|
|
|
|
2023-09-18 22:55:40 -04:00
|
|
|
h := handlers.New(client)
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.GET("/v6/health/", h.GetHealth)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.POST("/v6/membres/", h.PostMembres)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.GET("/v6/membres/", h.ListMembres)
|
2023-09-19 19:09:51 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.GET("/v6/membres/:membre_id/", h.ReadMembre)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.PUT("/v6/membres/:membre_id/prefered_name/", h.PutMembrePreferedName)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.POST("/v6/programmes/", h.PostProgrammes)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
2024-01-05 14:38:48 -05:00
|
|
|
e.POST("/v6/seed/", h.PostSeed)
|
2023-09-18 22:55:40 -04:00
|
|
|
|
|
|
|
// Execution
|
|
|
|
|
2023-05-25 02:21:09 -04:00
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-05-25 19:22:46 -04:00
|
|
|
rootCmd.AddCommand(apiCmd)
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// api.key
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(apiCmd.Flags(),
|
|
|
|
"api.key", "api-key", "bottin",
|
|
|
|
"API server key. Leave empty for no key auth")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// api.port
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.Int(apiCmd.Flags(),
|
|
|
|
"api.port", "api-port", 1312,
|
|
|
|
"API server port")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.database
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(apiCmd.Flags(),
|
|
|
|
"db.database", "db-database", "bottin",
|
|
|
|
"Postgres database")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.host
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(apiCmd.Flags(),
|
|
|
|
"db.host", "db-host", "db",
|
|
|
|
"Postgres host")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.password
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(apiCmd.Flags(),
|
|
|
|
"db.password", "db-password", "bottin",
|
|
|
|
"Postgres password")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.port
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.Int(apiCmd.Flags(),
|
|
|
|
"db.port", "db-port", 5432,
|
|
|
|
"Postgres port")
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.user
|
2023-10-17 16:35:51 -04:00
|
|
|
serpents.String(apiCmd.Flags(),
|
|
|
|
"db.user", "db-user", "bottin",
|
|
|
|
"Postgres user")
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|