refactor!: Use gin router instead chi router

- Removed chi dependencies
- Updated router registration logic
This commit is contained in:
2025-11-04 13:44:18 +03:00
parent 7e9653606e
commit 7bc4ce8c96
13 changed files with 499 additions and 294 deletions

View File

@@ -1,63 +0,0 @@
package auth
import (
"net/http"
"git.ostiwe.com/ostiwe-com/status/dto"
"git.ostiwe.com/ostiwe-com/status/modules/auth"
httpApp "git.ostiwe.com/ostiwe-com/status/pkg/http"
"git.ostiwe.com/ostiwe-com/status/router/controller"
"github.com/go-andiamo/chioas"
"github.com/go-chi/chi/v5"
"github.com/go-chi/render"
)
type Controller struct {
authModule auth.Module
}
func (*Controller) New() controller.Controller {
return &Controller{
authModule: auth.New(),
}
}
func (c *Controller) plainLogin(w http.ResponseWriter, r *http.Request) {
var payload dto.LoginRequest
if err := render.Bind(r, &payload); err != nil {
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
if sendErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
jwtString, err := c.authModule.Proceed(r.Context(), payload.Login, payload.Password)
if err != nil {
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(w, r)
if sendErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
return
}
response := dto.LoginResponse{
AccessToken: *jwtString,
}
render.JSON(w, r, response)
w.WriteHeader(http.StatusOK)
}
func (c *Controller) Group(r chi.Router) {
r.Post("/auth/plain", c.plainLogin)
}
func (c *Controller) Documentate() (*chioas.Paths, *chioas.Components) {
return nil, nil
}

View File

@@ -0,0 +1,62 @@
package auth
import (
"net/http"
"git.ostiwe.com/ostiwe-com/status/dto"
"git.ostiwe.com/ostiwe-com/status/modules/auth"
httpApp "git.ostiwe.com/ostiwe-com/status/pkg/http"
"git.ostiwe.com/ostiwe-com/status/router/controller"
"github.com/gin-gonic/gin"
)
type PlainAuthController struct {
authModule auth.Module
}
func (c *PlainAuthController) Handler() gin.HandlerFunc {
return func(ginCtx *gin.Context) {
var payload dto.LoginRequest
if err := ginCtx.ShouldBindJSON(&payload); err != nil {
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(ginCtx)
if sendErr != nil {
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
return
}
return
}
jwtString, err := c.authModule.Proceed(ginCtx.Request.Context(), payload.Login, payload.Password)
if err != nil {
sendErr := httpApp.NewResponseErrBuilder().WithStatusCode(http.StatusBadRequest).WithMessage(err.Error()).Send(ginCtx)
if sendErr != nil {
ginCtx.Writer.WriteHeader(http.StatusInternalServerError)
return
}
return
}
response := dto.LoginResponse{
AccessToken: *jwtString,
}
ginCtx.JSON(http.StatusOK, response)
}
}
func (c *PlainAuthController) Method() string {
return http.MethodPost
}
func (c *PlainAuthController) Path() string {
return "/api/v1/auth"
}
func (*PlainAuthController) New() controller.Controller {
return &PlainAuthController{
authModule: auth.New(),
}
}