newsbot-api/internal/handler/v1/handler.go

152 lines
4.6 KiB
Go
Raw Normal View History

2024-04-23 22:18:07 -07:00
package v1
import (
"context"
"database/sql"
"errors"
2024-04-23 22:18:07 -07:00
"github.com/golang-jwt/jwt/v5"
echojwt "github.com/labstack/echo-jwt/v4"
2024-04-23 22:18:07 -07:00
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
2024-04-23 22:18:07 -07:00
swagger "github.com/swaggo/echo-swagger"
_ "git.jamestombleson.com/jtom38/newsbot-api/docs"
2024-04-23 22:18:07 -07:00
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"git.jamestombleson.com/jtom38/newsbot-api/internal/services"
)
type Handler struct {
Router *echo.Echo
Db *database.Queries
config services.Configs
repo services.RepositoryService
2024-04-23 22:18:07 -07:00
}
const (
2024-04-28 19:29:49 -07:00
ErrParameterIdMissing = "The requested parameter ID was not found."
ErrParameterMissing = "The requested parameter was not found found:"
ErrUnableToParseId = "Unable to parse the requested ID"
ErrRecordMissing = "The requested record was not found"
ErrFailedToCreateRecord = "The record was not created due to a database problem"
ErrUserUnknown = "User is unknown"
ResponseMessageSuccess = "Success"
2024-04-23 22:18:07 -07:00
)
var (
ErrIdValueMissing string = "id value is missing"
ErrValueNotUuid string = "a value given was expected to be a uuid but was not correct."
ErrNoRecordFound string = "no record was found."
ErrUnableToConvertToJson string = "Unable to convert to json"
)
func NewServer(ctx context.Context, configs services.Configs, conn *sql.DB) *Handler {
2024-04-23 22:18:07 -07:00
s := &Handler{
//Db: db,
//dto: dto.NewDtoClient(db),
config: configs,
repo: services.NewRepositoryService(conn),
2024-04-23 22:18:07 -07:00
}
jwtConfig := echojwt.Config{
NewClaimsFunc: func(c echo.Context) jwt.Claims {
return new(JwtToken)
},
SigningKey: []byte(configs.JwtSecret),
}
2024-04-23 22:18:07 -07:00
router := echo.New()
router.Pre(middleware.RemoveTrailingSlash())
router.Pre(middleware.Logger())
router.Pre(middleware.Recover())
2024-04-23 22:18:07 -07:00
router.GET("/swagger/*", swagger.WrapHandler)
v1 := router.Group("/api/v1")
articles := v1.Group("/articles")
articles.Use(echojwt.WithConfig(jwtConfig))
articles.GET("", s.listArticles)
articles.GET(":id", s.getArticle)
articles.GET(":id/details", s.getArticleDetails)
articles.GET("by/source/:id", s.ListArticlesBySourceId)
2024-04-23 22:18:07 -07:00
//dwh := v1.Group("/discord/webhooks")
//dwh.GET("/", s.ListDiscordWebHooks)
//dwh.POST("/new", s.NewDiscordWebHook)
//dwh.GET("/by/serverAndChannel", s.GetDiscordWebHooksByServerAndChannel)
//dwh.GET("/:ID", s.GetDiscordWebHooksById)
//dwh.DELETE("/:ID", s.deleteDiscordWebHook)
//dwh.POST("/:ID/disable", s.disableDiscordWebHook)
//dwh.POST("/:ID/enable", s.enableDiscordWebHook)
2024-04-23 22:18:07 -07:00
//queue := v1.Group("/queue")
//queue.GET("/discord/webhooks", s.ListDiscordWebhookQueue) // TODO this needs to be reworked
2024-04-23 22:18:07 -07:00
//settings := v1.Group("/settings")
//settings.GET("/", s.getSettings)
2024-04-23 22:18:07 -07:00
sources := v1.Group("/sources")
sources.Use(echojwt.WithConfig(jwtConfig))
sources.GET("", s.listSources)
2024-04-23 22:18:07 -07:00
sources.GET("/by/source", s.listSourcesBySource)
sources.GET("/by/sourceAndName", s.GetSourceBySourceAndName)
2024-04-28 19:29:49 -07:00
//sources.POST("/new/reddit", s.newRedditSource)
//sources.POST("/new/youtube", s.newYoutubeSource)
//sources.POST("/new/twitch", s.newTwitchSource)
sources.POST("/new/rss", s.newRssSource)
sources.GET("/:ID/", s.getSource)
2024-04-23 22:18:07 -07:00
sources.DELETE("/:ID/", s.deleteSources)
sources.POST("/:ID/disable", s.disableSource)
sources.POST("/:ID/enable", s.enableSource)
subs := v1.Group("/subscriptions")
subs.Use(echojwt.WithConfig(jwtConfig))
2024-04-23 22:18:07 -07:00
subs.GET("/", s.ListSubscriptions)
subs.GET("/details", s.ListSubscriptionDetails)
subs.GET("/by/discordId", s.GetSubscriptionsByDiscordId)
subs.GET("/by/sourceId", s.GetSubscriptionsBySourceId)
subs.POST("/discord/webhook/new", s.newDiscordWebHookSubscription)
subs.DELETE("/discord/webhook/delete", s.DeleteDiscordWebHookSubscription)
s.Router = router
2024-04-23 22:18:07 -07:00
return s
}
type ApiStatusModel struct {
StatusCode int `json:"status"`
Message string `json:"message"`
}
type ApiError struct {
*ApiStatusModel
}
func (s *Handler) WriteError(c echo.Context, errMessage error, HttpStatusCode int) error {
return c.JSON(HttpStatusCode, domain.BaseResponse{
Message: errMessage.Error(),
})
}
func (s *Handler) WriteMessage(c echo.Context, msg string, HttpStatusCode int) error {
return c.JSON(HttpStatusCode, domain.BaseResponse{
Message: msg,
2024-04-23 22:18:07 -07:00
})
}
func (h *Handler) getJwtToken(c echo.Context) (JwtToken, error) {
// Make sure that the request came with a jwtToken
token, ok := c.Get("user").(*jwt.Token)
if !ok {
return JwtToken{}, errors.New(ErrJwtMissing)
}
// Generate the claims from the token
claims, ok := token.Claims.(*JwtToken)
if !ok {
return JwtToken{}, errors.New(ErrJwtClaimsMissing)
}
return *claims, nil
}