Added /version endpoint #37
@@ -138,6 +138,9 @@ func makeServer(db *sql.DB, cfg *config.Config) *http.Server {
|
|||||||
routerConfig := router.Config{
|
routerConfig := router.Config{
|
||||||
Database: db,
|
Database: db,
|
||||||
JWTSecret: cfg.JWTSecret,
|
JWTSecret: cfg.JWTSecret,
|
||||||
|
ServiceVersion: Version,
|
||||||
|
ServiceCommit: Commit,
|
||||||
|
ServiceBuildTime: BuildTime,
|
||||||
}
|
}
|
||||||
mux := router.NewMux(routerConfig)
|
mux := router.NewMux(routerConfig)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/robindittmar/dttmr-api/internal/api/response"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VersionResponse struct {
|
||||||
|
Version string `json:"version"`
|
||||||
|
Commit string `json:"commit"`
|
||||||
|
BuildTime string `json:"buildTime"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VersionHandler handles the version route
|
||||||
|
//
|
||||||
|
// @Summary Service version
|
||||||
|
// @Description Reports the version of the API
|
||||||
|
// @Tags Version
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} VersionResponse
|
||||||
|
// @Router /version [get]
|
||||||
|
func VersionHandler(version string, commit string, buildTime string) func(http.ResponseWriter, *http.Request) {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
response.JSON(r.Context(), w, http.StatusOK, VersionResponse{
|
||||||
|
Version: version,
|
||||||
|
Commit: commit,
|
||||||
|
BuildTime: buildTime,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,9 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Database *sql.DB
|
Database *sql.DB
|
||||||
JWTSecret string
|
JWTSecret string
|
||||||
|
ServiceVersion string
|
||||||
|
ServiceCommit string
|
||||||
|
ServiceBuildTime string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMux(cfg Config) http.Handler {
|
func NewMux(cfg Config) http.Handler {
|
||||||
@@ -22,7 +25,7 @@ func NewMux(cfg Config) http.Handler {
|
|||||||
inviteService := domain.NewInviteService(store.Invite)
|
inviteService := domain.NewInviteService(store.Invite)
|
||||||
userService := domain.NewUserService(store.User)
|
userService := domain.NewUserService(store.User)
|
||||||
registrationService := domain.NewRegistrationService(store, userService, inviteService)
|
registrationService := domain.NewRegistrationService(store, userService, inviteService)
|
||||||
listService := domain.NewListService(store.List)
|
listService := domain.NewListService(store, store.List)
|
||||||
|
|
||||||
authHandler := handler.NewAuthHandler(authService)
|
authHandler := handler.NewAuthHandler(authService)
|
||||||
inviteHandler := handler.NewInviteHandler(inviteService)
|
inviteHandler := handler.NewInviteHandler(inviteService)
|
||||||
@@ -32,6 +35,8 @@ func NewMux(cfg Config) http.Handler {
|
|||||||
protected := middleware.WithJWT(authService)
|
protected := middleware.WithJWT(authService)
|
||||||
|
|
||||||
apiMux := http.NewServeMux()
|
apiMux := http.NewServeMux()
|
||||||
|
apiMux.HandleFunc("GET /version", handler.VersionHandler(
|
||||||
|
cfg.ServiceVersion, cfg.ServiceCommit, cfg.ServiceBuildTime))
|
||||||
apiMux.HandleFunc("GET /health", handler.HealthHandler)
|
apiMux.HandleFunc("GET /health", handler.HealthHandler)
|
||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
|
|||||||
+15
-5
@@ -49,11 +49,12 @@ type ListRepository interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ListService struct {
|
type ListService struct {
|
||||||
|
tx Transactor
|
||||||
repo ListRepository
|
repo ListRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListService(r ListRepository) *ListService {
|
func NewListService(tx Transactor, r ListRepository) *ListService {
|
||||||
return &ListService{repo: r}
|
return &ListService{tx: tx, repo: r}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ListService) CreateList(ctx context.Context, authUserID string, name string) (*List, error) {
|
func (s *ListService) CreateList(ctx context.Context, authUserID string, name string) (*List, error) {
|
||||||
@@ -61,12 +62,21 @@ func (s *ListService) CreateList(ctx context.Context, authUserID string, name st
|
|||||||
return nil, ErrListNameMissing
|
return nil, ErrListNameMissing
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := s.repo.CreateList(ctx, name)
|
var list *List
|
||||||
|
err := s.tx.WithinTx(ctx, func(ctx context.Context) error {
|
||||||
|
l, err := s.repo.CreateList(ctx, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = s.repo.AddUserToList(ctx, list.ID, authUserID)
|
err = s.repo.AddUserToList(ctx, l.ID, authUserID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
list = l
|
||||||
|
return nil
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user