From 28bc869c645ddf2f189629ed794ffc2fae4b7db3 Mon Sep 17 00:00:00 2001 From: Muyao CHEN Date: Sat, 5 Oct 2024 16:41:49 +0200 Subject: [PATCH] feat: Add regitry and adapter level code for user/admin --- internal/howmuch/adapter/controller/.keep | 0 internal/howmuch/adapter/controller/admin.go | 52 +++++++++++++++++++ internal/howmuch/adapter/controller/app.go | 31 +++++++++++ internal/howmuch/adapter/controller/user.go | 54 ++++++++++++++++++++ internal/howmuch/howmuch.go | 12 ++--- internal/howmuch/registry/.keep | 0 internal/howmuch/registry/admin.go | 30 +++++++++++ internal/howmuch/registry/registry.go | 54 ++++++++++++++++++++ internal/howmuch/registry/user.go | 30 +++++++++++ internal/pkg/core/context.go | 22 ++++++++ 10 files changed, 279 insertions(+), 6 deletions(-) delete mode 100644 internal/howmuch/adapter/controller/.keep create mode 100644 internal/howmuch/adapter/controller/admin.go create mode 100644 internal/howmuch/adapter/controller/app.go create mode 100644 internal/howmuch/adapter/controller/user.go delete mode 100644 internal/howmuch/registry/.keep create mode 100644 internal/howmuch/registry/admin.go create mode 100644 internal/howmuch/registry/registry.go create mode 100644 internal/howmuch/registry/user.go diff --git a/internal/howmuch/adapter/controller/.keep b/internal/howmuch/adapter/controller/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/howmuch/adapter/controller/admin.go b/internal/howmuch/adapter/controller/admin.go new file mode 100644 index 0000000..ba3eda8 --- /dev/null +++ b/internal/howmuch/adapter/controller/admin.go @@ -0,0 +1,52 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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) { +} diff --git a/internal/howmuch/adapter/controller/app.go b/internal/howmuch/adapter/controller/app.go new file mode 100644 index 0000000..ad7c948 --- /dev/null +++ b/internal/howmuch/adapter/controller/app.go @@ -0,0 +1,31 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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 } +} diff --git a/internal/howmuch/adapter/controller/user.go b/internal/howmuch/adapter/controller/user.go new file mode 100644 index 0000000..744824d --- /dev/null +++ b/internal/howmuch/adapter/controller/user.go @@ -0,0 +1,54 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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) { +} diff --git a/internal/howmuch/howmuch.go b/internal/howmuch/howmuch.go index 4db3c02..55e192f 100644 --- a/internal/howmuch/howmuch.go +++ b/internal/howmuch/howmuch.go @@ -97,6 +97,7 @@ to share their expense of an event or a trip`, } func run() error { + // Set Gin running mode isDev := viper.GetBool("dev-mode") if isDev { gin.SetMode(gin.DebugMode) @@ -107,23 +108,22 @@ func run() error { 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) }) - r.GET("/", func(ctx *gin.Context) { - // time.Sleep(10 * time.Second) // Test shutdown - core.WriteResponse(ctx, nil, gin.H{ - "message": "how much?", - }) - }) + // Register the core service server := http.Server{ Addr: viper.GetString("web.addr"), diff --git a/internal/howmuch/registry/.keep b/internal/howmuch/registry/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/internal/howmuch/registry/admin.go b/internal/howmuch/registry/admin.go new file mode 100644 index 0000000..b6506f0 --- /dev/null +++ b/internal/howmuch/registry/admin.go @@ -0,0 +1,30 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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{} +} diff --git a/internal/howmuch/registry/registry.go b/internal/howmuch/registry/registry.go new file mode 100644 index 0000000..30a2e8f --- /dev/null +++ b/internal/howmuch/registry/registry.go @@ -0,0 +1,54 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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(), + } +} diff --git a/internal/howmuch/registry/user.go b/internal/howmuch/registry/user.go new file mode 100644 index 0000000..036e1d6 --- /dev/null +++ b/internal/howmuch/registry/user.go @@ -0,0 +1,30 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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{} +} diff --git a/internal/pkg/core/context.go b/internal/pkg/core/context.go index a914f6b..832d46e 100644 --- a/internal/pkg/core/context.go +++ b/internal/pkg/core/context.go @@ -1,3 +1,25 @@ +// MIT License +// +// Copyright (c) 2024 vinchent +// +// 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 type Context interface {