Officiellement migrer à /v5 #15
14 changed files with 47 additions and 52 deletions
|
@ -10,6 +10,7 @@ ADD cmd/ cmd/
|
||||||
ADD data/ data/
|
ADD data/ data/
|
||||||
ADD handlers/ handlers/
|
ADD handlers/ handlers/
|
||||||
ADD models/ models/
|
ADD models/ models/
|
||||||
|
ADD responses/ responses/
|
||||||
ADD web/ web/
|
ADD web/ web/
|
||||||
|
|
||||||
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o bottin .
|
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o bottin .
|
||||||
|
|
26
cmd/api.go
26
cmd/api.go
|
@ -5,8 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"git.agecem.com/agecem/bottin/v4/handlers"
|
"git.agecem.com/agecem/bottin/handlers"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -27,14 +27,6 @@ var apiCmd = &cobra.Command{
|
||||||
apiKey = viper.GetString("api.key")
|
apiKey = viper.GetString("api.key")
|
||||||
apiPort = viper.GetInt("api.port")
|
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"),
|
|
||||||
}
|
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Middlewares
|
// Middlewares
|
||||||
|
@ -49,21 +41,21 @@ var apiCmd = &cobra.Command{
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
|
|
||||||
e.GET("/v4/health/", handlers.GetHealth)
|
e.GET("/v5/health/", handlers.GetHealth)
|
||||||
|
|
||||||
e.POST("/v4/membres/", handlers.PostMembres)
|
e.POST("/v5/membres/", handlers.PostMembres)
|
||||||
|
|
||||||
e.GET("/v4/membres/:membre_id/", handlers.ReadMembre)
|
e.GET("/v5/membres/:membre_id/", handlers.ReadMembre)
|
||||||
|
|
||||||
e.PUT("/v4/membres/:membre_id/prefered_name/", handlers.PutMembrePreferedName)
|
e.PUT("/v5/membres/:membre_id/prefered_name/", handlers.PutMembrePreferedName)
|
||||||
|
|
||||||
e.POST("/v4/programmes/", handlers.PostProgrammes)
|
e.POST("/v5/programmes/", handlers.PostProgrammes)
|
||||||
|
|
||||||
e.POST("/v4/seed/", handlers.PostSeed)
|
e.POST("/v5/seed/", handlers.PostSeed)
|
||||||
|
|
||||||
// Execution
|
// Execution
|
||||||
|
|
||||||
client, err := data.NewDataClient(connection)
|
client, err := data.NewDataClientFromViper()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
log.Fatalf("Could not establish database connection.\n Error: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"git.agecem.com/agecem/bottin/v4/web"
|
"git.agecem.com/agecem/bottin/web"
|
||||||
"git.agecem.com/agecem/bottin/v4/web/webhandlers"
|
"git.agecem.com/agecem/bottin/web/webhandlers"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -55,7 +55,7 @@ var webCmd = &cobra.Command{
|
||||||
|
|
||||||
apiClient := data.NewApiClient(webApiKey, webApiHost, webApiProtocol, webApiPort)
|
apiClient := data.NewApiClient(webApiKey, webApiHost, webApiProtocol, webApiPort)
|
||||||
|
|
||||||
pingResult, err := apiClient.GetV4()
|
pingResult, err := apiClient.GetHealth()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,8 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/models"
|
"git.agecem.com/agecem/bottin/models"
|
||||||
|
"git.agecem.com/agecem/bottin/responses"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -79,33 +80,31 @@ func (a *ApiClient) Call(method, route string, requestBody io.Reader, useKey boo
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetV4 allows checking for API v4 server health
|
// GetHealth allows checking for API server health
|
||||||
func (a *ApiClient) GetV4() (string, error) {
|
func (a *ApiClient) GetHealth() (string, error) {
|
||||||
var getV4Response struct {
|
var getHealthResponse responses.GetHealth
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
response, err := a.Call(http.MethodGet, "/v4", nil, true)
|
response, err := a.Call(http.MethodGet, "/v5/health", nil, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return getV4Response.Message, err
|
return getHealthResponse.Message, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(response.Body)
|
body, err := ioutil.ReadAll(response.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return getV4Response.Message, err
|
return getHealthResponse.Message, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(body, &getV4Response); err != nil {
|
if err := json.Unmarshal(body, &getHealthResponse); err != nil {
|
||||||
return getV4Response.Message, err
|
return getHealthResponse.Message, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if getV4Response.Message == "" {
|
if getHealthResponse.Message == "" {
|
||||||
return getV4Response.Message, errors.New("Could not confirm that API server is up, no response message")
|
return getHealthResponse.Message, errors.New("Could not confirm that API server is up, no response message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return getV4Response.Message, nil
|
return getHealthResponse.Message, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) {
|
func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) {
|
||||||
|
@ -125,7 +124,7 @@ func (a *ApiClient) GetMembre(membreID string) (models.Membre, error) {
|
||||||
log.Println("ApiClient.GetMembre received membreID: ", membreID)
|
log.Println("ApiClient.GetMembre received membreID: ", membreID)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
response, err := a.Call(http.MethodGet, fmt.Sprintf("/v4/membres/%s", membreID), nil, true)
|
response, err := a.Call(http.MethodGet, fmt.Sprintf("/v5/membres/%s", membreID), nil, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return getMembreResponse.Data.Membre, err
|
return getMembreResponse.Data.Membre, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/models"
|
"git.agecem.com/agecem/bottin/models"
|
||||||
_ "github.com/jackc/pgx/stdlib"
|
_ "github.com/jackc/pgx/stdlib"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module git.agecem.com/agecem/bottin/v4
|
module git.agecem.com/agecem/bottin
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
|
|
|
@ -4,17 +4,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
|
"git.agecem.com/agecem/bottin/responses"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetHealthResponse struct {
|
|
||||||
Message string `json:"message"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetHealth(c echo.Context) error {
|
func GetHealth(c echo.Context) error {
|
||||||
response := GetHealthResponse{
|
response := responses.GetHealth{
|
||||||
Message: "Bottin API v4 is ready",
|
Message: "Bottin API v5 is ready",
|
||||||
}
|
}
|
||||||
|
|
||||||
dataClient, err := data.NewDataClientFromViper()
|
dataClient, err := data.NewDataClientFromViper()
|
||||||
|
|
|
@ -3,8 +3,8 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"git.agecem.com/agecem/bottin/v4/models"
|
"git.agecem.com/agecem/bottin/models"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,7 +3,7 @@ package handlers
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
2
main.go
2
main.go
|
@ -1,6 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "git.agecem.com/agecem/bottin/v4/cmd"
|
import "git.agecem.com/agecem/bottin/cmd"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
|
|
6
responses/health.go
Normal file
6
responses/health.go
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package responses
|
||||||
|
|
||||||
|
// GetHealth is the response type for handlers.GetHealth
|
||||||
|
type GetHealth struct {
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.agecem.com/agecem/bottin/v4/data"
|
"git.agecem.com/agecem/bottin/data"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue