initial work for service layer wrong
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m37s
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m37s
This commit is contained in:
parent
730f411494
commit
0af44340d5
@ -1,11 +1,11 @@
|
|||||||
info:
|
info:
|
||||||
name: register user
|
name: login user
|
||||||
type: http
|
type: http
|
||||||
seq: 1
|
seq: 1
|
||||||
|
|
||||||
http:
|
http:
|
||||||
method: POST
|
method: POST
|
||||||
url: "{{host}}/api/auth/register"
|
url: "{{host}}/api/v1/auth/login"
|
||||||
body:
|
body:
|
||||||
type: json
|
type: json
|
||||||
data: |-
|
data: |-
|
||||||
@ -3,9 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/db"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/db"
|
||||||
|
usersdb "git.kanopo.dev/rhythm/rhythm-backend/internal/db/users"
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/http"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/http"
|
||||||
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/auth"
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/health"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/health"
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/logger"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/logger"
|
||||||
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/service/users"
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
"go.uber.org/fx/fxevent"
|
"go.uber.org/fx/fxevent"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@ -14,11 +17,14 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
fx.New(
|
fx.New(
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
config.Provide,
|
config.Provide, //config
|
||||||
logger.ProvideLogger,
|
logger.ProvideLogger, //logger
|
||||||
db.ProvidePool,
|
db.ProvidePool, // pool provider
|
||||||
http.NewServer,
|
usersdb.New, // generated code for sqlc
|
||||||
health.NewHandler,
|
users.NewService, // service
|
||||||
|
http.NewServer, // http server
|
||||||
|
health.NewHandler, // http handler
|
||||||
|
auth.NewHandler, //http handler
|
||||||
),
|
),
|
||||||
fx.Invoke(
|
fx.Invoke(
|
||||||
http.GlueRoutes,
|
http.GlueRoutes,
|
||||||
|
|||||||
48
internal/http/api/auth/handler.go
Normal file
48
internal/http/api/auth/handler.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Handler struct {
|
||||||
|
log *zap.SugaredLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHandler(log *zap.SugaredLogger) *Handler {
|
||||||
|
return &Handler{
|
||||||
|
log: log,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
|
rg.POST("/login", h.Login)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setRefreshTokenCookie(c *gin.Context, token string) {
|
||||||
|
maxAge := time.Hour * 24 * 7
|
||||||
|
c.SetCookie(
|
||||||
|
"refresh_token", // name
|
||||||
|
token, // value
|
||||||
|
int(maxAge), // maxAge (seconds, 7 days)
|
||||||
|
"/", // path
|
||||||
|
"", // domain
|
||||||
|
false, // secure (true in production)
|
||||||
|
true, // httpOnly
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) Login(c *gin.Context) {
|
||||||
|
// var req users.LoginReq
|
||||||
|
// if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// res := h.service.Login(req)
|
||||||
|
// setRefreshTokenCookie(c, res.RefreshToken)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"msg": "ok"})
|
||||||
|
}
|
||||||
@ -1,14 +1,16 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/auth"
|
||||||
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/health"
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/http/api/health"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GlueRoutes(r *gin.Engine, healthHandler *health.Handler) {
|
func GlueRoutes(r *gin.Engine, healthHandler *health.Handler, authHandler *auth.Handler) {
|
||||||
api := r.Group("/api")
|
api := r.Group("/api")
|
||||||
healthHandler.RegisterRoutes(api.Group("/health"))
|
healthHandler.RegisterRoutes(api.Group("/health"))
|
||||||
|
|
||||||
// v1 := api.Group("/v1")
|
v1 := api.Group("/v1")
|
||||||
|
authHandler.RegisterRoutes(v1.Group("/auth"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
34
internal/service/users/user_service.go
Normal file
34
internal/service/users/user_service.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package users
|
||||||
|
|
||||||
|
import (
|
||||||
|
usersdb "git.kanopo.dev/rhythm/rhythm-backend/internal/db/users"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {
|
||||||
|
repo usersdb.Querier
|
||||||
|
log *zap.SugaredLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewService(repo usersdb.Querier, log *zap.SugaredLogger) *Service {
|
||||||
|
return &Service{
|
||||||
|
repo: repo,
|
||||||
|
log: log,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// type LoginReq struct {
|
||||||
|
// Email string `json:"email" binding:"required"`
|
||||||
|
// Password string `json:"password" binding:"required"`
|
||||||
|
// }
|
||||||
|
// type AuthRes struct {
|
||||||
|
// AccessToken string `json:"accessToken"`
|
||||||
|
// RefreshToken string // not parset to json, set with cookies
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *Service) Login(req LoginReq) AuthRes {
|
||||||
|
// return AuthRes{
|
||||||
|
// AccessToken: "ciao",
|
||||||
|
// RefreshToken: "ciao",
|
||||||
|
// }
|
||||||
|
// }
|
||||||
Loading…
x
Reference in New Issue
Block a user