106 lines
2.4 KiB
Go
106 lines
2.4 KiB
Go
package cmd
|
||
|
||
import (
|
||
"crypto/subtle"
|
||
"fmt"
|
||
"log"
|
||
"net/http"
|
||
|
||
"git.agecem.com/agecem/bottin-agenda/data"
|
||
"git.agecem.com/agecem/bottin-agenda/handlers"
|
||
bottindata "git.agecem.com/agecem/bottin/v6/data"
|
||
"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) {
|
||
// TODO migrer à viper.Unmarshal(&models.Config)
|
||
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")
|
||
|
||
bottinClient := http.DefaultClient
|
||
defer bottinClient.CloseIdleConnections()
|
||
|
||
// Using bottin's API client
|
||
bottinConnection := bottindata.NewApiClient(
|
||
bottinClient,
|
||
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
|
||
|
||
e.POST("/v3/seed/", handlers.PostSeed)
|
||
|
||
e.GET("/v3/health/", handlers.GetHealth)
|
||
|
||
e.GET("/v3/transactions/", handlers.GetTransactions)
|
||
e.POST("/v3/transactions/", handlers.PostTransactions)
|
||
|
||
// Check bottin is ready
|
||
|
||
bottinHealthResponse, err := bottinConnection.GetHealth()
|
||
if err != nil {
|
||
log.Fatalf("[bottin] bottinConnection.GetHealth(): %s", err)
|
||
}
|
||
|
||
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() failed: %s", err)
|
||
} else {
|
||
log.Println("[bottin-agenda db] ok")
|
||
}
|
||
|
||
if _, err = dataClient.Seed(); err != nil {
|
||
log.Fatalf("[bottin-agenda db] dataClient.Seed() failed: %s", err)
|
||
}
|
||
|
||
// Execution
|
||
|
||
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", apiPort)))
|
||
},
|
||
}
|
||
|
||
func init() {
|
||
rootCmd.AddCommand(apiCmd)
|
||
}
|