Adapts api package to have users handler
This commit is contained in:
parent
bdaa5aa59b
commit
ce5ee0fbd7
2 changed files with 101 additions and 0 deletions
45
api/api.go
Normal file
45
api/api.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"freed/database"
|
||||
"os"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
Repo *database.Repository
|
||||
}
|
||||
|
||||
func NewHandlers(repo *database.Repository) *Handlers {
|
||||
return &Handlers{Repo: repo}
|
||||
}
|
||||
|
||||
func Setup(app *fiber.App, repository *database.Repository) error {
|
||||
apiKey := os.Getenv("API_KEY")
|
||||
|
||||
if apiKey == "" {
|
||||
return errors.New("Could not read API_KEY from ENV file.")
|
||||
}
|
||||
|
||||
api := app.Group("/api", keyauth.New(keyauth.Config{
|
||||
SuccessHandler: successHandler,
|
||||
ErrorHandler: errHandler,
|
||||
KeyLookup: "header:x-api-key",
|
||||
ContextKey: "apiKey",
|
||||
Validator: apiKeyValidator(apiKey),
|
||||
}))
|
||||
|
||||
handlers := NewHandlers(repository)
|
||||
|
||||
v1 := api.Group("/v1")
|
||||
v1.Get("/users", handlers.FetchAllUsersHandler)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Handlers) FetchAllUsersHandler(ctx *fiber.Ctx) error {
|
||||
return ctx.JSON(&fiber.Map{"users": "none"})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue