2024-05-11 09:56:19 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/labstack/echo/v4/middleware"
|
|
|
|
|
2024-05-12 09:22:14 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/apiclient"
|
2024-05-11 09:56:19 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-portal/internal/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
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"
|
|
|
|
ErrFailedToUpdateRecord = "The requested record was not updated due to a database problem"
|
|
|
|
|
|
|
|
ErrUserUnknown = "User is unknown"
|
|
|
|
ErrYouDontOwnTheRecord = "The record requested does not belong to you"
|
|
|
|
|
|
|
|
ResponseMessageSuccess = "Success"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2024-05-12 09:22:14 -07:00
|
|
|
type Handler struct {
|
|
|
|
Router *echo.Echo
|
|
|
|
config config.Configs
|
|
|
|
api apiclient.ApiClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.ApiClient) *Handler {
|
2024-05-11 09:56:19 -07:00
|
|
|
s := &Handler{
|
|
|
|
config: configs,
|
2024-05-12 09:22:14 -07:00
|
|
|
api: apiClient,
|
2024-05-11 09:56:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
router := echo.New()
|
|
|
|
router.Pre(middleware.RemoveTrailingSlash())
|
|
|
|
router.Pre(middleware.Logger())
|
|
|
|
router.Pre(middleware.Recover())
|
2024-05-26 19:30:04 -07:00
|
|
|
router.Use(middleware.Static("/internal/static"))
|
2024-07-07 07:58:26 -07:00
|
|
|
//router.Use(RefreshJwtMiddleware(apiClient))
|
2024-05-11 09:56:19 -07:00
|
|
|
|
|
|
|
router.GET("/", s.HomeIndex)
|
2024-05-12 09:22:14 -07:00
|
|
|
router.GET("/about", s.HomeAbout)
|
|
|
|
|
2024-05-12 10:45:16 -07:00
|
|
|
debug := router.Group("/debug")
|
|
|
|
debug.GET("/cookies", s.DebugCookies)
|
|
|
|
|
|
|
|
articles := router.Group("/articles")
|
2024-07-07 07:58:26 -07:00
|
|
|
//articles.Use(ValidateJwtMiddleware(configs.JwtSecret))
|
2024-05-12 10:45:16 -07:00
|
|
|
articles.GET("", s.ArticlesList)
|
|
|
|
|
2024-06-02 19:55:05 -07:00
|
|
|
sources := router.Group("/sources")
|
2024-07-07 07:58:26 -07:00
|
|
|
//sources.Use(ValidateJwtMiddleware(configs.JwtSecret))
|
2024-06-02 19:55:05 -07:00
|
|
|
sources.GET("", s.ListAllSources)
|
2024-07-07 07:58:26 -07:00
|
|
|
sources.GET("/add", s.AddSource)
|
2024-06-02 19:55:05 -07:00
|
|
|
|
2024-05-12 09:22:14 -07:00
|
|
|
users := router.Group("/users")
|
|
|
|
users.GET("/login", s.UserLogin)
|
|
|
|
users.POST("/login", s.UserAfterLogin)
|
2024-05-12 09:35:13 -07:00
|
|
|
users.GET("/signup", s.UserSignUp)
|
|
|
|
users.POST("/signup", s.UserAfterSignUp)
|
2024-07-07 07:58:26 -07:00
|
|
|
users.Use(ValidateJwtMiddleware(configs.JwtSecret))
|
2024-06-02 19:55:05 -07:00
|
|
|
users.GET("/logout", s.UsersLogout)
|
2024-05-26 19:30:04 -07:00
|
|
|
users.GET("/profile", s.UserProfile)
|
|
|
|
|
2024-05-11 09:56:19 -07:00
|
|
|
s.Router = router
|
|
|
|
return s
|
|
|
|
}
|