bottin/cmd/api.go

117 lines
2.8 KiB
Go
Raw Permalink Normal View History

2023-05-25 02:21:09 -04:00
package cmd
import (
"crypto/subtle"
"fmt"
"log"
"git.agecem.com/agecem/bottin/v5/data"
"git.agecem.com/agecem/bottin/v5/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
)
// 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",
Args: cobra.ExactArgs(0),
2023-05-25 02:21:09 -04:00
Run: func(cmd *cobra.Command, args []string) {
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
}))
}
// DataClient
2023-05-25 02:21:09 -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)
}
_, err = client.Seed()
if err != nil {
log.Fatalf("Error during client.Seed(): %s", err)
}
h := handlers.New(client)
// Routes
e.GET("/v5/health/", h.GetHealth)
e.POST("/v5/membres/", h.PostMembres)
e.GET("/v5/membres/:membre_id/", h.ReadMembre)
e.PUT("/v5/membres/:membre_id/prefered_name/", h.PutMembrePreferedName)
e.POST("/v5/programmes/", h.PostProgrammes)
e.POST("/v5/seed/", h.PostSeed)
// Execution
2023-05-25 02:21:09 -04:00
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
},
}
func init() {
rootCmd.AddCommand(apiCmd)
2023-05-25 02:21:09 -04:00
// api.key
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')")
viper.BindPFlag("api.key", apiCmd.Flags().Lookup("api-key"))
2023-05-25 02:21:09 -04:00
// api.port
apiCmd.Flags().Int(
"api-port", 1312,
2023-05-25 02:21:09 -04:00
"API server port (config:'api.port')")
viper.BindPFlag("api.port", apiCmd.Flags().Lookup("api-port"))
2023-05-25 02:21:09 -04:00
// db.database
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
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
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
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
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
}