feat: Add regitry and adapter level code for user/admin
This commit is contained in:
parent
9094d12c3a
commit
28bc869c64
52
internal/howmuch/adapter/controller/admin.go
Normal file
52
internal/howmuch/adapter/controller/admin.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
// 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 controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Admin interface {
|
||||||
|
CreateUser(core.Context)
|
||||||
|
GetUserById(core.Context)
|
||||||
|
UpdateUser(core.Context)
|
||||||
|
DeleteUser(core.Context)
|
||||||
|
ListUsers(core.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type AdminController struct{}
|
||||||
|
|
||||||
|
func (ac *AdminController) CreateUser(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac *AdminController) GetUserById(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac *AdminController) UpdateUser(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac *AdminController) DeleteUser(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ac *AdminController) ListUsers(core.Context) {
|
||||||
|
}
|
31
internal/howmuch/adapter/controller/app.go
Normal file
31
internal/howmuch/adapter/controller/app.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// 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 controller
|
||||||
|
|
||||||
|
// AppController is the controller structure that holds service controllers.
|
||||||
|
type AppController struct {
|
||||||
|
// User must implement User interface
|
||||||
|
User interface{ User }
|
||||||
|
|
||||||
|
Admin interface{ Admin }
|
||||||
|
}
|
54
internal/howmuch/adapter/controller/user.go
Normal file
54
internal/howmuch/adapter/controller/user.go
Normal 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 controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
// User is the user controller interface, it describes all the handlers
|
||||||
|
// that need to be implemented for the /user endpoint
|
||||||
|
type User interface {
|
||||||
|
Signup(core.Context)
|
||||||
|
UpdateInfo(core.Context)
|
||||||
|
Login(core.Context)
|
||||||
|
Logout(core.Context)
|
||||||
|
ChangePassword(core.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserController struct{}
|
||||||
|
|
||||||
|
func (uc *UserController) Signup(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) UpdateInfo(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) Login(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) Logout(core.Context) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *UserController) ChangePassword(core.Context) {
|
||||||
|
}
|
@ -97,6 +97,7 @@ to share their expense of an event or a trip`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
|
// Set Gin running mode
|
||||||
isDev := viper.GetBool("dev-mode")
|
isDev := viper.GetBool("dev-mode")
|
||||||
if isDev {
|
if isDev {
|
||||||
gin.SetMode(gin.DebugMode)
|
gin.SetMode(gin.DebugMode)
|
||||||
@ -107,23 +108,22 @@ func run() error {
|
|||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// Middlewares
|
// Middlewares
|
||||||
|
// Cors
|
||||||
corsCfg := cors.DefaultConfig()
|
corsCfg := cors.DefaultConfig()
|
||||||
corsCfg.AllowAllOrigins = true
|
corsCfg.AllowAllOrigins = true
|
||||||
corsCfg.AllowHeaders = append(corsCfg.AllowHeaders, "Authorization", "Accept", "X-CSRF-Token")
|
corsCfg.AllowHeaders = append(corsCfg.AllowHeaders, "Authorization", "Accept", "X-CSRF-Token")
|
||||||
r.Use(cors.New(corsCfg))
|
r.Use(cors.New(corsCfg))
|
||||||
|
|
||||||
|
// Use my request id middleware
|
||||||
|
// TODO: I might use the community version later
|
||||||
r.Use(middleware.RequestID())
|
r.Use(middleware.RequestID())
|
||||||
|
|
||||||
|
// Route for the 404 error
|
||||||
r.NoRoute(func(ctx *gin.Context) {
|
r.NoRoute(func(ctx *gin.Context) {
|
||||||
core.WriteResponse(ctx, errno.PageNotFoundErr, nil)
|
core.WriteResponse(ctx, errno.PageNotFoundErr, nil)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.GET("/", func(ctx *gin.Context) {
|
// Register the core service
|
||||||
// time.Sleep(10 * time.Second) // Test shutdown
|
|
||||||
core.WriteResponse(ctx, nil, gin.H{
|
|
||||||
"message": "how much?",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
server := http.Server{
|
server := http.Server{
|
||||||
Addr: viper.GetString("web.addr"),
|
Addr: viper.GetString("web.addr"),
|
||||||
|
30
internal/howmuch/registry/admin.go
Normal file
30
internal/howmuch/registry/admin.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// 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 registry
|
||||||
|
|
||||||
|
import "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
|
||||||
|
|
||||||
|
// NewUserController returns an admin controller's implementation
|
||||||
|
func (r *registry) NewAdminController() controller.Admin {
|
||||||
|
return &controller.AdminController{}
|
||||||
|
}
|
54
internal/howmuch/registry/registry.go
Normal file
54
internal/howmuch/registry/registry.go
Normal 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 registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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{}
|
||||||
|
|
||||||
|
// Registry returns a new app controller that will be used by main()/run()
|
||||||
|
// AppController is essentially the struct that holds all the controllers,
|
||||||
|
// classed by their domains.
|
||||||
|
type Registry interface {
|
||||||
|
NewAppController() controller.AppController
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRegistry returns a new Registry's implementation.
|
||||||
|
func NewRegistry() Registry {
|
||||||
|
return ®istry{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAppController creates a new AppController with controller struct for
|
||||||
|
// each domain.
|
||||||
|
func (r *registry) NewAppController() controller.AppController {
|
||||||
|
return controller.AppController{
|
||||||
|
User: r.NewUserController(),
|
||||||
|
Admin: r.NewAdminController(),
|
||||||
|
}
|
||||||
|
}
|
30
internal/howmuch/registry/user.go
Normal file
30
internal/howmuch/registry/user.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// 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 registry
|
||||||
|
|
||||||
|
import "git.vinchent.xyz/vinchent/howmuch/internal/howmuch/adapter/controller"
|
||||||
|
|
||||||
|
// NewUserController returns a user controller's implementation
|
||||||
|
func (r *registry) NewUserController() controller.User {
|
||||||
|
return &controller.UserController{}
|
||||||
|
}
|
@ -1,3 +1,25 @@
|
|||||||
|
// 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 core
|
package core
|
||||||
|
|
||||||
type Context interface {
|
type Context interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user