Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/refactor 2 #14

Merged
merged 28 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e408ce7
init -1
Lukmanern Jan 23, 2024
84c5b77
add main, remove v19x
Lukmanern Jan 23, 2024
b4158a5
update internal pkg
Lukmanern Jan 23, 2024
8bcdb99
update middleware
Lukmanern Jan 23, 2024
313d2fc
add repository
Lukmanern Jan 24, 2024
dfca9b1
add repositories
Lukmanern Jan 24, 2024
191895b
add email and user service
Lukmanern Jan 24, 2024
ae468ff
update user service
Lukmanern Jan 24, 2024
caf0cc3
add role service
Lukmanern Jan 24, 2024
a62151d
update service and test
Lukmanern Jan 24, 2024
489e0b5
add role controller
Lukmanern Jan 26, 2024
ba8d7eb
add user controller
Lukmanern Jan 26, 2024
a9998c1
update user controller
Lukmanern Jan 27, 2024
e29ecfc
update role controller
Lukmanern Jan 27, 2024
c1949ab
update user controller
Lukmanern Jan 27, 2024
385a0a2
add user controller test
Lukmanern Jan 27, 2024
7956509
add app dir
Lukmanern Jan 28, 2024
d4400a9
add base role test case
Lukmanern Jan 29, 2024
f972d75
add test case for role controller
Lukmanern Jan 29, 2024
429edb0
add app
Lukmanern Jan 29, 2024
299caa4
add sleep to test
Lukmanern Jan 29, 2024
7a7552b
update controller and service -1
Lukmanern Jan 30, 2024
d725b9f
update controller and service -2
Lukmanern Jan 30, 2024
af53a71
update controller and service -3
Lukmanern Jan 30, 2024
3073b9f
finishing features -1
Lukmanern Jan 31, 2024
5c3c0c3
finishing features -2: testing
Lukmanern Jan 31, 2024
ae9a55a
finishing features -3: testing
Lukmanern Jan 31, 2024
503629b
finishing features -4: sorting
Lukmanern Jan 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ["1.19.x", "1.20.x", "1.21.x"]
go-version: ["1.20.x", "1.21.x"]

steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
- name: Run migration
run: |
go run database/migration/main.go
sleep 3s

- name: Run Test
run: go test -race ./...
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ Techs and tools were used in this project:
This project is under license from MIT. For more details, see the [LICENSE](LICENSE) file.

 

## Todo

- Add password and password confirm at DeleteProfile (permanent) by User
4 changes: 0 additions & 4 deletions application/.gitignore

This file was deleted.

24 changes: 13 additions & 11 deletions application/app.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// 📌 Origin Github Repository: https://github.com/Lukmanern<slash>gost

// 🔍 README
// Application package configures middleware, error management, and
// handles OS signals for gracefully stopping the server when receiving
// an interrupt signal. This package provides routes related to user
// management and role-based access control (RBAC). And so on.
// 📌 Origin Github Repository: https://github.com/Lukmanern

package application

import (
"errors"
"fmt"
"log"
"net"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -73,8 +68,17 @@ func setup() {
connector.LoadRedisCache()
}

func checkLocalPort(port int) {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal("port is being used by other process")
}
defer listener.Close()
}

func RunApp() {
setup()
checkLocalPort(port)
router.Use(cors.New(cors.Config{
AllowCredentials: true,
}))
Expand Down Expand Up @@ -109,10 +113,8 @@ func RunApp() {
close(idleConnsClosed)
}()

getUserManagementRoutes(router) // user CRUD without auth ⚠️
getDevopmentRouter(router) // experimental without auth ⚠️
getUserRoutes(router) // user with auth
getRolePermissionRoutes(router) // RBAC CRUD with auth
getUserRoutes(router)
getRolePermissionRoutes(router)

if err := router.Listen(fmt.Sprintf(":%d", port)); err != nil {
log.Printf("Oops... Server is not running! Reason: %v", err)
Expand Down
44 changes: 0 additions & 44 deletions application/development_router.go

This file was deleted.

54 changes: 0 additions & 54 deletions application/role_permission_router.go

This file was deleted.

32 changes: 32 additions & 0 deletions application/role_router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 📌 Origin Github Repository: https://github.com/Lukmanern

package application

import (
"github.com/gofiber/fiber/v2"

"github.com/Lukmanern/gost/internal/middleware"
"github.com/Lukmanern/gost/internal/role"

controller "github.com/Lukmanern/gost/controller/role"
service "github.com/Lukmanern/gost/service/role"
)

var (
roleService service.RoleService
roleController controller.RoleController
)

func getRolePermissionRoutes(router fiber.Router) {
jwtHandler := middleware.NewJWTHandler()

roleService = service.NewRoleService()
roleController = controller.NewRoleController(roleService)

roleRouter := router.Group("role").Use(jwtHandler.IsAuthenticated)
roleRouter.Post("", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), roleController.Create)
roleRouter.Get("", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), roleController.GetAll)
roleRouter.Get(":id", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), roleController.Get)
roleRouter.Put(":id", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), roleController.Update)
roleRouter.Delete(":id", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), roleController.Delete)
}
33 changes: 0 additions & 33 deletions application/user_management_router.go

This file was deleted.

39 changes: 16 additions & 23 deletions application/user_router.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
// 📌 Origin Github Repository: https://github.com/Lukmanern<slash>gost

// 🔍 README
// User Routes provides some features and action that user can use.
// User Routes provide the typical web application authentication flow,
// such as registration, sending verification codes, and verifying accounts
// with a verification code.
// 📌 Origin Github Repository: https://github.com/Lukmanern

package application

import (
"github.com/gofiber/fiber/v2"

"github.com/Lukmanern/gost/internal/middleware"
"github.com/Lukmanern/gost/internal/role"

controller "github.com/Lukmanern/gost/controller/user"
service "github.com/Lukmanern/gost/service/user"

permSvc "github.com/Lukmanern/gost/service/permission"
roleSvc "github.com/Lukmanern/gost/service/role"
)

var (
userPermService permSvc.PermissionService
userRoleService roleSvc.RoleService
userService service.UserService
userController controller.UserController
userService service.UserService
userController controller.UserController
)

func getUserRoutes(router fiber.Router) {
userPermService = permSvc.NewPermissionService()
userRoleService = roleSvc.NewRoleService(userPermService)
userService = service.NewUserService(userRoleService)
userController = controller.NewUserController(userService)
jwtHandler := middleware.NewJWTHandler()

userService = service.NewUserService()
userController = controller.NewUserController(userService)

userRoute := router.Group("user")
userRoute.Post("register", userController.Register) // send email
userRoute.Post("account-activation", userController.AccountActivation)
userRoute.Post("login", userController.Login)
userRoute.Post("register", userController.Register)
userRoute.Post("verification", userController.AccountActivation)
userRoute.Post("request-delete", userController.DeleteAccountActivation)
userRoute.Post("forget-password", userController.ForgetPassword)
userRoute.Post("forget-password", userController.ForgetPassword) // send email
userRoute.Post("reset-password", userController.ResetPassword)

userRouteAuth := userRoute.Use(jwtHandler.IsAuthenticated)
userRouteAuth.Post("logout", userController.Logout)
userRouteAuth.Get("my-profile", userController.MyProfile)
userRouteAuth.Post("logout", userController.Logout)
userRouteAuth.Put("profile-update", userController.UpdateProfile)
userRouteAuth.Post("update-password", userController.UpdatePassword)
userRouteAuth.Delete("delete-account", userController.DeleteAccount)

// for admin
userRouteAuth.Get("", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), userController.GetAll)
userRouteAuth.Put("ban-user/:id", jwtHandler.HasOneRole(role.RoleSuperAdmin, role.RoleAdmin), userController.BanAccount)
}
Loading
Loading