Ajouter API keyauth

Defer dbclient.DB.Close() dans apiCmd
This commit is contained in:
Victor Lacasse-Beaudoin 2023-09-19 16:47:57 -04:00
parent 9ac6bf7940
commit 3b9a51c767

View file

@ -4,6 +4,7 @@ Copyright © 2023 AGECEM & Victor Lacasse-Beaudoin
package cmd
import (
"crypto/subtle"
"fmt"
"log"
@ -30,6 +31,13 @@ var apiCmd = &cobra.Command{
e.Pre(middleware.AddTrailingSlash())
if cfg.API.Key != "" {
e.Use(middleware.KeyAuth(func(key string, c echo.Context) (bool, error) {
return subtle.ConstantTimeCompare([]byte(key), []byte(cfg.API.Key)) == 1, nil
}))
log.Println("API server is using an API key")
}
v0 := e.Group("/v0")
bottinApiClient := bottindata.NewApiClient(cfg.Bottin.API.Key, cfg.Bottin.API.Host, cfg.Bottin.API.Protocol, cfg.Bottin.API.Port)
@ -38,6 +46,7 @@ var apiCmd = &cobra.Command{
if err != nil {
log.Fatal(err)
}
defer dbClient.DB.Close()
handler := apihandler.New(bottinApiClient, dbClient)