2023-05-25 02:21:09 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"git.agecem.com/agecem/bottin/v4/data"
|
|
|
|
"git.agecem.com/agecem/bottin/v4/handlers"
|
|
|
|
"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")
|
|
|
|
|
|
|
|
connection := data.PostgresConnection{
|
|
|
|
User: viper.GetString("db.user"),
|
|
|
|
Password: viper.GetString("db.password"),
|
|
|
|
Host: viper.GetString("db.host"),
|
|
|
|
Database: viper.GetString("db.database"),
|
|
|
|
Port: viper.GetInt("db.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
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Routes
|
|
|
|
|
2023-06-02 02:35:15 -04:00
|
|
|
e.GET("/v4/health", handlers.GetHealth)
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-05-25 04:40:13 -04:00
|
|
|
e.POST("/v4/membres/", handlers.PostMembres)
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
e.GET("/v4/membres/:membre_id/", handlers.ReadMembre)
|
2023-05-25 04:40:13 -04:00
|
|
|
|
2023-05-25 04:31:29 -04:00
|
|
|
e.PUT("/v4/membres/:membre_id/prefered_name/", handlers.PutMembrePreferedName)
|
2023-05-25 02:21:09 -04:00
|
|
|
|
2023-05-25 03:44:41 -04:00
|
|
|
e.POST("/v4/programmes/", handlers.PostProgrammes)
|
|
|
|
|
2023-05-25 04:40:13 -04:00
|
|
|
e.POST("/v4/seed/", handlers.PostSeed)
|
|
|
|
|
2023-05-25 02:21:09 -04:00
|
|
|
// Execution
|
|
|
|
|
|
|
|
client, err := data.NewDataClient(connection)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.DB.Ping()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Database was supposed to be ready but Ping() failed.\n Error: %s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client.DB.Close()
|
|
|
|
|
|
|
|
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-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().String(
|
|
|
|
"api-key", "bottin",
|
2023-05-25 02:21:09 -04:00
|
|
|
"API server key. Leave empty for no key auth. (config: 'api.key')")
|
2023-05-25 19:22:46 -04:00
|
|
|
viper.BindPFlag("api.key", apiCmd.Flags().Lookup("api-key"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// api.port
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().Int(
|
|
|
|
"api-port", 1312,
|
2023-05-25 02:21:09 -04:00
|
|
|
"API server port (config:'api.port')")
|
2023-05-25 19:22:46 -04:00
|
|
|
viper.BindPFlag("api.port", apiCmd.Flags().Lookup("api-port"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.database
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().String("db-database", "bottin", "Postgres database (config:'db.database')")
|
|
|
|
viper.BindPFlag("db.database", apiCmd.Flags().Lookup("db-database"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.host
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().String("db-host", "db", "Postgres host (config:'db.host')")
|
|
|
|
viper.BindPFlag("db.host", apiCmd.Flags().Lookup("db-host"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.password
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().String("db-password", "bottin", "Postgres password (config:'db.password')")
|
|
|
|
viper.BindPFlag("db.password", apiCmd.Flags().Lookup("db-password"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.port
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().Int("db-port", 5432, "Postgres port (config:'db.port')")
|
|
|
|
viper.BindPFlag("db.port", apiCmd.Flags().Lookup("db-port"))
|
2023-05-25 02:21:09 -04:00
|
|
|
|
|
|
|
// db.user
|
2023-05-25 19:22:46 -04:00
|
|
|
apiCmd.Flags().String("db-user", "bottin", "Postgres user (config:'db.user')")
|
|
|
|
viper.BindPFlag("db.user", apiCmd.Flags().Lookup("db-user"))
|
2023-05-25 02:21:09 -04:00
|
|
|
}
|