feat: route up to the controller level

This commit is contained in:
Muyao CHEN 2024-10-06 00:15:29 +02:00
parent c00cbf35f1
commit 332871d403
5 changed files with 78 additions and 32 deletions

View File

@ -23,6 +23,8 @@
package controller
import (
"net/http"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
)
@ -38,17 +40,18 @@ type User interface {
type UserController struct{}
func (uc *UserController) Signup(core.Context) {
func (uc *UserController) Signup(ctx core.Context) {
ctx.JSON(http.StatusOK, "hello")
}
func (uc *UserController) UpdateInfo(core.Context) {
func (uc *UserController) UpdateInfo(ctx core.Context) {
}
func (uc *UserController) Login(core.Context) {
func (uc *UserController) Login(ctx core.Context) {
}
func (uc *UserController) Logout(core.Context) {
func (uc *UserController) Logout(ctx core.Context) {
}
func (uc *UserController) ChangePassword(core.Context) {
func (uc *UserController) ChangePassword(ctx core.Context) {
}

View File

@ -32,12 +32,10 @@ import (
"time"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/infra/datastore"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/infra/router"
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/registry"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/middleware"
"git.vinchent.xyz/vinchent/howmuch/pkg/version/verflag"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -107,7 +105,6 @@ func run() error {
}
// Init DB
dbPool, err := datastore.NewDB(
fmt.Sprintf(
"host=%s port=%d dbname=%s user=%s password=%s sslmode=%s",
@ -124,31 +121,20 @@ func run() error {
}
defer dbPool.Close()
r := gin.Default()
// Middlewares
// Cors
corsCfg := cors.DefaultConfig()
corsCfg.AllowAllOrigins = true
corsCfg.AllowHeaders = append(corsCfg.AllowHeaders, "Authorization", "Accept", "X-CSRF-Token")
r.Use(cors.New(corsCfg))
// Use my request id middleware
// TODO: I might use the community version later
r.Use(middleware.RequestID())
// Route for the 404 error
r.NoRoute(func(ctx *gin.Context) {
core.WriteResponse(ctx, errno.PageNotFoundErr, nil)
})
// Register the core service
r := registry.NewRegistry(dbPool)
engine := gin.Default()
engine = router.Routes(engine, r.NewAppController())
server := http.Server{
Addr: viper.GetString("web.addr"),
Handler: r,
Handler: engine,
}
log.InfoLog("Server running", "port", viper.GetString("web.addr"))
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.FatalLog(err.Error())

View File

@ -0,0 +1,54 @@
// MIT License
//
// Copyright (c) 2024 vinchent <vinchent@vinchent.xyz>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package router
import (
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/errno"
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/middleware"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Routes(engine *gin.Engine, c controller.AppController) *gin.Engine {
// Middlewares
// Cors
corsCfg := cors.DefaultConfig()
corsCfg.AllowAllOrigins = true
corsCfg.AllowHeaders = append(corsCfg.AllowHeaders, "Authorization", "Accept", "X-CSRF-Token")
engine.Use(cors.New(corsCfg))
// Use my request id middleware
// TODO: I might use the community version later
engine.Use(middleware.RequestID())
// Route for the 404 error
engine.NoRoute(func(ctx *gin.Context) {
core.WriteResponse(ctx, errno.PageNotFoundErr, nil)
})
engine.POST("/signup", func(ctx *gin.Context) { c.User.Signup(ctx) })
return engine
}

View File

@ -24,13 +24,16 @@ package registry
import (
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
"github.com/jackc/pgx/v5/pgxpool"
)
// registry is an implementation of Registry interface.
// It needs a db connection to provide the necessary support.
// It might holds other drivers when the projects grows. For example
// the object needed to connect to Redis or Kafka.
type registry struct{}
type registry struct {
db *pgxpool.Pool
}
// Registry returns a new app controller that will be used by main()/run()
// AppController is essentially the struct that holds all the controllers,
@ -40,8 +43,8 @@ type Registry interface {
}
// NewRegistry returns a new Registry's implementation.
func NewRegistry() Registry {
return &registry{}
func NewRegistry(db *pgxpool.Pool) Registry {
return &registry{db: db}
}
// NewAppController creates a new AppController with controller struct for