2023-05-29 17:58:23 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/subtle"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2023-06-03 20:16:41 -04:00
|
|
|
"git.agecem.com/agecem/bottin-agenda/data"
|
2023-05-29 17:58:23 -04:00
|
|
|
"git.agecem.com/agecem/bottin-agenda/handlers"
|
2023-06-03 19:48:37 -04:00
|
|
|
bottindata "git.agecem.com/agecem/bottin/v5/data"
|
2023-05-29 17:58:23 -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",
|
|
|
|
Short: "Démarrer le serveur API",
|
|
|
|
Args: cobra.ExactArgs(0),
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
apiKey = viper.GetString("api.key")
|
|
|
|
apiPort = viper.GetInt("api.port")
|
|
|
|
|
|
|
|
bottinApiKey := viper.GetString("bottin.api.key")
|
|
|
|
bottinApiHost := viper.GetString("bottin.api.host")
|
|
|
|
bottinApiProtocol := viper.GetString("bottin.api.protocol")
|
|
|
|
bottinApiPort := viper.GetInt("bottin.api.port")
|
|
|
|
|
2023-05-30 19:03:55 -04:00
|
|
|
// Using bottin's API client
|
|
|
|
bottinConnection := bottindata.NewApiClient(
|
2023-05-29 17:58:23 -04:00
|
|
|
bottinApiKey,
|
|
|
|
bottinApiHost,
|
|
|
|
bottinApiProtocol,
|
|
|
|
bottinApiPort,
|
|
|
|
)
|
|
|
|
|
|
|
|
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-06 02:16:54 -04:00
|
|
|
e.POST("/v2/seed/", handlers.PostSeed)
|
|
|
|
|
2023-06-03 19:48:37 -04:00
|
|
|
e.GET("/v2/health/", handlers.GetHealth)
|
2023-05-29 17:58:23 -04:00
|
|
|
|
2023-06-03 19:48:37 -04:00
|
|
|
e.GET("/v2/membres/:membre_id/", handlers.GetMembre)
|
2023-05-30 19:03:55 -04:00
|
|
|
|
2023-06-08 00:53:05 -04:00
|
|
|
e.GET("/v2/transactions/", handlers.GetTransactions)
|
2023-06-06 02:22:57 -04:00
|
|
|
e.POST("/v2/transactions/", handlers.PostTransactions)
|
|
|
|
|
2023-05-29 17:58:23 -04:00
|
|
|
// Check bottin is ready
|
|
|
|
|
2023-06-03 19:48:37 -04:00
|
|
|
bottinHealthResponse, err := bottinConnection.GetHealth()
|
2023-05-29 17:58:23 -04:00
|
|
|
if err != nil {
|
2023-06-03 20:16:41 -04:00
|
|
|
log.Fatalf("[bottin] bottinConnection.GetHealth(): %s", err)
|
2023-05-29 17:58:23 -04:00
|
|
|
}
|
|
|
|
|
2023-06-03 20:16:41 -04:00
|
|
|
log.Println("[bottin] ok: ", bottinHealthResponse)
|
|
|
|
|
|
|
|
// Check database is ready
|
|
|
|
|
|
|
|
dataClient, err := data.NewDataClientFromViper()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("[bottin-agenda db] data.NewDataclientFromViper(): %s", err)
|
|
|
|
}
|
|
|
|
defer dataClient.DB.Close()
|
|
|
|
|
|
|
|
if err := dataClient.DB.Ping(); err != nil {
|
|
|
|
log.Fatalf("[bottin-agenda db] dataClient.DB.Ping(): %s", err)
|
|
|
|
} else {
|
|
|
|
log.Println("[bottin-agenda db] ok")
|
|
|
|
}
|
2023-05-29 17:58:23 -04:00
|
|
|
|
|
|
|
// Execution
|
|
|
|
|
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(apiCmd)
|
|
|
|
|
|
|
|
// api.key
|
|
|
|
apiCmd.Flags().String(
|
|
|
|
"api-key", "bottin-agenda",
|
|
|
|
"API server key. Leave empty for no key auth. (config: 'api.key')")
|
|
|
|
viper.BindPFlag("api.key", apiCmd.Flags().Lookup("api-key"))
|
|
|
|
|
|
|
|
// api.port
|
|
|
|
apiCmd.Flags().Int(
|
|
|
|
"api-port", 1313,
|
|
|
|
"API server port (config:'api.port')")
|
|
|
|
viper.BindPFlag("api.port", apiCmd.Flags().Lookup("api-port"))
|
|
|
|
|
|
|
|
// bottin.api.host
|
|
|
|
apiCmd.Flags().String(
|
|
|
|
"bottin-api-host", "api",
|
|
|
|
"Remote bottin API server host (config:'bottin.api.host')")
|
|
|
|
viper.BindPFlag("bottin.api.host", apiCmd.Flags().Lookup("bottin-api-host"))
|
|
|
|
|
|
|
|
// bottin.api.key
|
|
|
|
apiCmd.Flags().String(
|
|
|
|
"bottin-api-key", "bottin",
|
|
|
|
"Remote bottin API server key (config:'bottin.api.key')")
|
|
|
|
viper.BindPFlag("bottin.api.key", apiCmd.Flags().Lookup("bottin-api-key"))
|
|
|
|
|
|
|
|
// bottin.api.protocol
|
|
|
|
apiCmd.Flags().String(
|
|
|
|
"bottin-api-protocol", "http",
|
|
|
|
"Remote bottin API server protocol (config:'bottin.api.protocol')")
|
|
|
|
viper.BindPFlag("bottin.api.protocol", apiCmd.Flags().Lookup("bottin-api-protocol"))
|
|
|
|
|
|
|
|
// bottin.api.port
|
|
|
|
apiCmd.Flags().Int(
|
|
|
|
"bottin-api-port", 1312,
|
|
|
|
"Remote bottin API server port (config:'bottin.api.port')")
|
|
|
|
viper.BindPFlag("bottin.api.port", apiCmd.Flags().Lookup("bottin-api-port"))
|
|
|
|
|
|
|
|
// db.database
|
|
|
|
apiCmd.Flags().String("db-database", "bottin-agenda", "Postgres database (config:'db.database')")
|
|
|
|
viper.BindPFlag("db.database", apiCmd.Flags().Lookup("db-database"))
|
|
|
|
|
|
|
|
// db.host
|
|
|
|
apiCmd.Flags().String("db-host", "db", "Postgres host (config:'db.host')")
|
|
|
|
viper.BindPFlag("db.host", apiCmd.Flags().Lookup("db-host"))
|
|
|
|
|
|
|
|
// db.password
|
|
|
|
apiCmd.Flags().String("db-password", "bottin-agenda", "Postgres password (config:'db.password')")
|
|
|
|
viper.BindPFlag("db.password", apiCmd.Flags().Lookup("db-password"))
|
|
|
|
|
|
|
|
// db.port
|
|
|
|
apiCmd.Flags().Int("db-port", 5432, "Postgres port (config:'db.port')")
|
|
|
|
viper.BindPFlag("db.port", apiCmd.Flags().Lookup("db-port"))
|
|
|
|
|
|
|
|
// db.user
|
|
|
|
apiCmd.Flags().String("db-user", "bottin-agenda", "Postgres user (config:'db.user')")
|
|
|
|
viper.BindPFlag("db.user", apiCmd.Flags().Lookup("db-user"))
|
|
|
|
}
|