lifedebugger commited on
Commit
a2399ed
·
1 Parent(s): ecee9f6

Deploy files from GitHub repository

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .github/workflows/main.yml +13 -37
  2. .github/workflows/main_huggingface.yml +52 -0
  3. Dockerfile +37 -30
  4. README.md +19 -3
  5. docker-compose.yml +31 -0
  6. space/.gitignore +2 -1
  7. space/config/config.go +3 -0
  8. space/config/database_connection_config.go +10 -0
  9. space/controller/controller.go +7 -0
  10. space/controller/email/email_create_verification.go +22 -0
  11. space/controller/email/email_delete_verification.go +22 -0
  12. space/controller/email/email_verify.go +22 -0
  13. space/controller/user/user_login_controller.go +20 -0
  14. space/controller/user/user_profile_controller.go +24 -0
  15. space/controller/user/user_register_controller.go +20 -0
  16. space/controller/user/user_update_profile_controller.go +24 -0
  17. space/docker-compose.yaml +10 -0
  18. space/go.mod +2 -1
  19. space/go.sum +3 -1
  20. space/logserror_log.txt +1 -0
  21. space/middleware/authentication_middleware.go +6 -40
  22. space/middleware/response_middleware.go +6 -0
  23. space/models/database_orm_model.go +104 -16
  24. space/models/request_model.go +5 -0
  25. space/repositories/account_repository.go +14 -0
  26. space/repositories/email_verification_repository.go +49 -0
  27. space/router/email_route.go +15 -0
  28. space/router/router.go +3 -6
  29. space/router/user_route.go +16 -0
  30. space/services/email_verification_service.go +57 -0
  31. space/services/jwt_service.go +45 -0
  32. space/services/login_service.go +2 -3
  33. space/services/register_service.go +1 -2
  34. space/services/service.go +9 -1
  35. space/services/user_profile_service.go +37 -0
  36. space/space/LICENSE +201 -0
  37. space/space/space/.github/workflows/main.yml +1 -1
  38. space/space/space/space/.github/workflows/main.yml +1 -1
  39. space/space/space/space/space/.gitignore +2 -1
  40. space/space/space/space/space/config/config.go +21 -0
  41. space/space/space/space/space/config/database_connection_config.go +60 -0
  42. space/space/space/space/space/controller/home_controller.go +9 -0
  43. space/space/space/space/space/controller/login_controller.go +19 -0
  44. space/space/space/space/space/controller/register_controller.go +19 -0
  45. space/space/space/space/space/middleware/authentication_middleware.go +104 -0
  46. space/space/space/space/space/middleware/response_middleware.go +39 -0
  47. space/space/space/space/space/models/authentication_payload_model.go +7 -0
  48. space/space/space/space/space/models/database_orm_model.go +31 -0
  49. space/space/space/space/space/models/exception_model.go +12 -0
  50. space/space/space/space/space/models/model.go +1 -0
.github/workflows/main.yml CHANGED
@@ -1,4 +1,4 @@
1
- name: Deploy to Development via Huggingface
2
 
3
  on:
4
  push:
@@ -6,47 +6,23 @@ on:
6
  - main
7
 
8
  jobs:
9
- deploy-to-huggingface:
10
  runs-on: ubuntu-latest
11
 
12
  steps:
13
- # Checkout repository
14
  - name: Checkout Repository
15
  uses: actions/checkout@v3
16
 
17
- # Setup Git
18
- - name: Setup Git for Huggingface
19
- run: |
20
- git config --global user.email "abdan.hafidz@gmail.com"
21
- git config --global user.name "abdanhafidz"
22
-
23
- # Clone Huggingface Space Repository
24
- - name: Clone Huggingface Space
25
- env:
26
- HF_TOKEN: ${{ secrets.HF_TOKEN }}
27
- run: |
28
- git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space
29
-
30
- # Update Git Remote URL and Pull Latest Changes
31
- - name: Update Remote and Pull Changes
32
- env:
33
- HF_TOKEN: ${{ secrets.HF_TOKEN }}
34
- run: |
35
- cd space
36
- git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev
37
- git pull origin main || echo "No changes to pull"
38
-
39
- # Copy Files to Huggingface Space
40
- - name: Copy Files to Space
41
- run: |
42
- rsync -av --exclude='.git' ./ space/
43
 
44
- # Commit and Push to Huggingface Space
45
- - name: Commit and Push to Huggingface
46
- env:
47
- HF_TOKEN: ${{ secrets.HF_TOKEN }}
48
  run: |
49
- cd space
50
- git add .
51
- git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
- git push origin main || echo "No changes to push"
 
 
 
1
+ name: Deploy Golang App
2
 
3
  on:
4
  push:
 
6
  - main
7
 
8
  jobs:
9
+ deploy:
10
  runs-on: ubuntu-latest
11
 
12
  steps:
 
13
  - name: Checkout Repository
14
  uses: actions/checkout@v3
15
 
16
+ - name: Set up SSH key
17
+ uses: webfactory/ssh-agent@v0.5.4
18
+ with:
19
+ ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ - name: Deploy to VPS
 
 
 
22
  run: |
23
+ ssh -o StrictHostKeyChecking=no qobiltu@103.23.199.136 << 'EOF'
24
+ cd /home/qobiltu/api-qobiltu
25
+ git pull origin main
26
+ docker-compose down -v
27
+ docker-compose up --build -d
28
+ EOF
.github/workflows/main_huggingface.yml ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy to Development via Huggingface
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ deploy-to-huggingface:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ # Checkout repository
14
+ - name: Checkout Repository
15
+ uses: actions/checkout@v3
16
+
17
+ # Setup Git
18
+ - name: Setup Git for Huggingface
19
+ run: |
20
+ git config --global user.email "abdan.hafidz@gmail.com"
21
+ git config --global user.name "abdanhafidz"
22
+
23
+ # Clone Huggingface Space Repository
24
+ - name: Clone Huggingface Space
25
+ env:
26
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
27
+ run: |
28
+ git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space
29
+
30
+ # Update Git Remote URL and Pull Latest Changes
31
+ - name: Update Remote and Pull Changes
32
+ env:
33
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
34
+ run: |
35
+ cd space
36
+ git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev
37
+ git pull origin main || echo "No changes to pull"
38
+
39
+ # Copy Files to Huggingface Space
40
+ - name: Copy Files to Space
41
+ run: |
42
+ rsync -av --exclude='.git' ./ space/
43
+
44
+ # Commit and Push to Huggingface Space
45
+ - name: Commit and Push to Huggingface
46
+ env:
47
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
48
+ run: |
49
+ cd space
50
+ git add .
51
+ git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
+ git push origin main || echo "No changes to push"
Dockerfile CHANGED
@@ -1,30 +1,37 @@
1
- # Gunakan image dasar Golang versi 1.24.1
2
- FROM golang:1.24.1
3
-
4
- # Set working directory
5
- WORKDIR /app
6
-
7
- # Copy go.mod dan go.sum
8
- COPY go.mod go.sum ./
9
-
10
- # Download dependencies
11
- RUN go mod download
12
-
13
- # Copy seluruh kode
14
- COPY . .
15
-
16
- # Buat file .env dengan variabel environment yang dibutuhkan
17
- RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \
18
- echo "DB_USER=postgres.rdscploxoikqsevhduii" >> .env && \
19
- echo "DB_PASSWORD=Qobiltu12233334444" >> .env && \
20
- echo "DB_PORT=5432" >> .env && \
21
- echo "DB_NAME=postgres" >> .env && \
22
- echo "HOST_ADDRESS = 0.0.0.0" >> .env && \
23
- echo "HOST_PORT = 7860" >> .env && \
24
- echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \
25
- echo "LOG_PATH = logs" >> .env
26
- # Build aplikasi
27
- RUN go build -o main .
28
-
29
- # Jalankan aplikasi
30
- CMD ["./main"]
 
 
 
 
 
 
 
 
1
+ # Gunakan image dasar Golang versi 1.24.1
2
+ FROM golang:1.24.1 AS builder
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Copy go.mod dan go.sum
8
+ COPY go.mod go.sum ./
9
+
10
+ # Download dependencies
11
+ RUN go mod download
12
+
13
+ # Copy seluruh kode
14
+ COPY . .
15
+
16
+ # Build aplikasi
17
+ # RUN go build -o /app/main .
18
+
19
+ # Build aplikasi untuk Linux
20
+ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o /app/main .
21
+
22
+ # Stage 2: Gunakan image runtime yang lebih kecil
23
+ FROM alpine:latest
24
+
25
+ # Set working directory
26
+ WORKDIR /app
27
+
28
+ # Copy hasil build dari builder ke image runtime
29
+ COPY --from=builder /app/main .
30
+
31
+ # Copy file .env untuk konfigurasi environment
32
+ COPY .env .env
33
+
34
+ RUN chmod +x /app/main
35
+
36
+ # Jalankan aplikasi
37
+ CMD ["./main"]
README.md CHANGED
@@ -62,10 +62,26 @@ The API uses the `errors` package to handle errors. You can set the `ERROR_PATH`
62
 
63
  # Command Documentation
64
 
65
- ## Update Golang Project version
 
66
  > go mod edit -go 1.24
67
  > go mod tidy
68
 
69
- ## Update all package
70
  > go get -u ./...
71
- > go mod tidy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  # Command Documentation
64
 
65
+ ## GO Command
66
+ ### Update Golang Project version
67
  > go mod edit -go 1.24
68
  > go mod tidy
69
 
70
+ ### Update all package
71
  > go get -u ./...
72
+ > go mod tidy
73
+
74
+ ## Docker Command
75
+ ### Enter docker container
76
+ > docker exec -it api-qobiltu sh
77
+
78
+ ### Docker Running Process
79
+ > docker ps
80
+
81
+ ### Rebuild Docker Image
82
+ > docker-compose down -v
83
+ > docker-compose up --build -d
84
+
85
+ ### View Log container
86
+ > docker logs --tail=50 -f api-qobiltu
87
+ > docker logs postgres-db
docker-compose.yml ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ version: '3.8'
2
+
3
+ services:
4
+ app:
5
+ container_name: api-qobiltu
6
+ build: .
7
+ depends_on:
8
+ - db
9
+ env_file: .env
10
+ ports:
11
+ - "8080:8080"
12
+ # volumes:
13
+ # - ./logs:/app/logs
14
+ # - /home/qobiltu/api-qobiltu:/app
15
+ restart: unless-stopped
16
+
17
+ db:
18
+ image: postgres:15
19
+ container_name: postgres-db
20
+ environment:
21
+ POSTGRES_USER: ${DB_USER}
22
+ POSTGRES_PASSWORD: ${DB_PASSWORD}
23
+ POSTGRES_DB: ${DB_NAME}
24
+ ports:
25
+ - "5432:5432"
26
+ volumes:
27
+ - db-data:/var/lib/postgresql/data
28
+ restart: always
29
+
30
+ volumes:
31
+ db-data:
space/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
  .env
2
  vendor/
3
  quzuu-be.exe
4
- README.md
 
 
1
  .env
2
  vendor/
3
  quzuu-be.exe
4
+ README.md
5
+ .qodo
space/config/config.go CHANGED
@@ -2,6 +2,7 @@ package config
2
 
3
  import (
4
  "os"
 
5
 
6
  "github.com/joho/godotenv"
7
  )
@@ -10,6 +11,7 @@ var TCP_ADDRESS string
10
  var LOG_PATH string
11
  var HOST_ADDRESS string
12
  var HOST_PORT string
 
13
 
14
  func init() {
15
  godotenv.Load()
@@ -17,5 +19,6 @@ func init() {
17
  HOST_PORT = os.Getenv("HOST_PORT")
18
  TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT
19
  LOG_PATH = os.Getenv("LOG_PATH")
 
20
  // Menampilkan nilai variabel lingkungan
21
  }
 
2
 
3
  import (
4
  "os"
5
+ "strconv"
6
 
7
  "github.com/joho/godotenv"
8
  )
 
11
  var LOG_PATH string
12
  var HOST_ADDRESS string
13
  var HOST_PORT string
14
+ var EMAIL_VERIFICATION_DURATION int
15
 
16
  func init() {
17
  godotenv.Load()
 
19
  HOST_PORT = os.Getenv("HOST_PORT")
20
  TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT
21
  LOG_PATH = os.Getenv("LOG_PATH")
22
+ EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION"))
23
  // Menampilkan nilai variabel lingkungan
24
  }
space/config/database_connection_config.go CHANGED
@@ -51,7 +51,17 @@ func AutoMigrateAll(db *gorm.DB) {
51
  err := db.AutoMigrate(
52
  &models.Account{},
53
  &models.AccountDetails{},
 
 
 
 
 
 
 
 
 
54
  )
 
55
  if err != nil {
56
  log.Fatal(err)
57
  }
 
51
  err := db.AutoMigrate(
52
  &models.Account{},
53
  &models.AccountDetails{},
54
+ &models.EmailVerification{},
55
+ &models.ExternalAuth{},
56
+ &models.FCM{},
57
+ &models.ForgotPassword{},
58
+ &models.Academy{},
59
+ &models.AcademyMaterial{},
60
+ &models.AcademyContent{},
61
+ &models.AcademyMaterialProgress{},
62
+ &models.AcademyContentProgress{},
63
  )
64
+
65
  if err != nil {
66
  log.Fatal(err)
67
  }
space/controller/controller.go CHANGED
@@ -19,6 +19,13 @@ type (
19
  }
20
  )
21
 
 
 
 
 
 
 
 
22
  func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context, act func()) {
23
  cParam, _ := c.Get("accountData")
24
  if cParam != nil {
 
19
  }
20
  )
21
 
22
+ func (controller *Controller[T1, T2, T3]) HeaderParse(c *gin.Context, act func()) {
23
+ cParam, _ := c.Get("accountData")
24
+ if cParam != nil {
25
+ controller.AccountData = cParam.(models.AccountData)
26
+ }
27
+ act()
28
+ }
29
  func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context, act func()) {
30
  cParam, _ := c.Get("accountData")
31
  if cParam != nil {
space/controller/email/email_create_verification.go ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "strconv"
5
+
6
+ "api.qobiltu.id/controller"
7
+ "api.qobiltu.id/models"
8
+ "api.qobiltu.id/services"
9
+ "github.com/gin-gonic/gin"
10
+ )
11
+
12
+ func CreateVerification(c *gin.Context) {
13
+ emailVerification := services.EmailVerificationService{}
14
+ emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{
15
+ Service: &emailVerification.Service,
16
+ }
17
+ query, _ := c.GetQuery("account_id")
18
+ accountId, _ := strconv.Atoi(query)
19
+ emailVerificationController.Service.Constructor.AccountID = uint(accountId)
20
+ emailVerification.Create()
21
+ emailVerificationController.Response(c)
22
+ }
space/controller/email/email_delete_verification.go ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "strconv"
5
+
6
+ "api.qobiltu.id/controller"
7
+ "api.qobiltu.id/models"
8
+ "api.qobiltu.id/services"
9
+ "github.com/gin-gonic/gin"
10
+ )
11
+
12
+ func DeleteVerification(c *gin.Context) {
13
+ emailVerification := services.EmailVerificationService{}
14
+ emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{
15
+ Service: &emailVerification.Service,
16
+ }
17
+ query, _ := c.GetQuery("account_id")
18
+ accountId, _ := strconv.Atoi(query)
19
+ emailVerificationController.Service.Constructor.AccountID = uint(accountId)
20
+ emailVerification.Delete()
21
+ emailVerificationController.Response(c)
22
+ }
space/controller/email/email_verify.go ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "strconv"
5
+
6
+ "api.qobiltu.id/controller"
7
+ "api.qobiltu.id/models"
8
+ "api.qobiltu.id/services"
9
+ "github.com/gin-gonic/gin"
10
+ )
11
+
12
+ func Verify(c *gin.Context) {
13
+ emailVerification := services.EmailVerificationService{}
14
+ emailVerificationController := controller.Controller[any, models.EmailVerification, models.EmailVerification]{
15
+ Service: &emailVerification.Service,
16
+ }
17
+ query, _ := c.GetQuery("account_id")
18
+ accountId, _ := strconv.Atoi(query)
19
+ emailVerificationController.Service.Constructor.AccountID = uint(accountId)
20
+ emailVerification.Validate()
21
+ emailVerificationController.Response(c)
22
+ }
space/controller/user/user_login_controller.go ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package user
2
+
3
+ import (
4
+ "api.qobiltu.id/controller"
5
+ "api.qobiltu.id/models"
6
+ "api.qobiltu.id/services"
7
+ "github.com/gin-gonic/gin"
8
+ )
9
+
10
+ func Login(c *gin.Context) {
11
+ authentication := services.AuthenticationService{}
12
+ loginController := controller.Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{
13
+ Service: &authentication.Service,
14
+ }
15
+ loginController.RequestJSON(c, func() {
16
+ loginController.Service.Constructor.Email = loginController.Request.Email
17
+ loginController.Service.Constructor.Password = loginController.Request.Password
18
+ authentication.Authenticate()
19
+ })
20
+ }
space/controller/user/user_profile_controller.go ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package user
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "api.qobiltu.id/controller"
7
+ "api.qobiltu.id/models"
8
+ "api.qobiltu.id/services"
9
+ "github.com/gin-gonic/gin"
10
+ )
11
+
12
+ func Profile(c *gin.Context) {
13
+ userProfile := services.UserProfileService{}
14
+ userProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{
15
+ Service: &userProfile.Service,
16
+ }
17
+ fmt.Println(userProfileController.AccountData)
18
+ userProfileController.HeaderParse(c, func() {
19
+ userProfileController.Service.Constructor.AccountId = userProfileController.AccountData.UserID
20
+ userProfile.Retrieve()
21
+ userProfileController.Response(c)
22
+ },
23
+ )
24
+ }
space/controller/user/user_register_controller.go ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package user
2
+
3
+ import (
4
+ "api.qobiltu.id/controller"
5
+ "api.qobiltu.id/models"
6
+ "api.qobiltu.id/services"
7
+ "github.com/gin-gonic/gin"
8
+ )
9
+
10
+ func Register(c *gin.Context) {
11
+ register := services.RegisterService{}
12
+ registerController := controller.Controller[models.RegisterRequest, models.Account, models.Account]{
13
+ Service: &register.Service,
14
+ }
15
+ registerController.RequestJSON(c, func() {
16
+ registerController.Service.Constructor.Password = registerController.Request.Password
17
+ registerController.Service.Constructor.Email = registerController.Request.Email
18
+ register.Create()
19
+ })
20
+ }
space/controller/user/user_update_profile_controller.go ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package user
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "api.qobiltu.id/controller"
7
+ "api.qobiltu.id/models"
8
+ "api.qobiltu.id/services"
9
+ "github.com/gin-gonic/gin"
10
+ )
11
+
12
+ func UpdateProfile(c *gin.Context) {
13
+ userProfile := services.UserProfileService{}
14
+ userUpdateProfileController := controller.Controller[any, services.UserProfileConstructor, models.Account]{
15
+ Service: &userProfile.Service,
16
+ }
17
+ fmt.Println(userUpdateProfileController.AccountData)
18
+ userUpdateProfileController.HeaderParse(c, func() {
19
+ userUpdateProfileController.Service.Constructor.AccountId = userUpdateProfileController.AccountData.UserID
20
+ userProfile.Retrieve()
21
+ userUpdateProfileController.Response(c)
22
+ },
23
+ )
24
+ }
space/docker-compose.yaml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ services:
2
+ postgres:
3
+ container_name: postgres
4
+ image: postgres
5
+ environment:
6
+ - POSTGRES_USER=postgres
7
+ - POSTGRES_PASSWORD=password
8
+ - POSTGRES_DB=qobiltu
9
+ ports:
10
+ - 5432:5432
space/go.mod CHANGED
@@ -3,6 +3,7 @@ module api.qobiltu.id
3
  go 1.24.0
4
 
5
  require (
 
6
  github.com/gin-gonic/gin v1.10.0
7
  github.com/golang-jwt/jwt/v5 v5.2.1
8
  github.com/joho/godotenv v1.5.1
@@ -46,4 +47,4 @@ require (
46
  golang.org/x/text v0.23.0 // indirect
47
  google.golang.org/protobuf v1.36.5 // indirect
48
  gopkg.in/yaml.v3 v3.0.1 // indirect
49
- )
 
3
  go 1.24.0
4
 
5
  require (
6
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible
7
  github.com/gin-gonic/gin v1.10.0
8
  github.com/golang-jwt/jwt/v5 v5.2.1
9
  github.com/joho/godotenv v1.5.1
 
47
  golang.org/x/text v0.23.0 // indirect
48
  google.golang.org/protobuf v1.36.5 // indirect
49
  gopkg.in/yaml.v3 v3.0.1 // indirect
50
+ )
space/go.sum CHANGED
@@ -10,6 +10,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
10
  github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
12
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 
 
13
  github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
14
  github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
15
  github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
@@ -115,4 +117,4 @@ gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
115
  gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
116
  gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
117
  gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
118
- nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
 
10
  github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11
  github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
12
  github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
14
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
15
  github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
16
  github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
17
  github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
 
117
  gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
118
  gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
119
  gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
120
+ nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
space/logserror_log.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ 2025/03/18 21:08:07 Error Log : ERROR: relasi « email_verifications » tidak ada (SQLSTATE 42P01)
space/middleware/authentication_middleware.go CHANGED
@@ -3,52 +3,19 @@
3
  package middleware
4
 
5
  import (
6
- "errors"
7
  "time"
8
 
9
  "api.qobiltu.id/config"
10
  "api.qobiltu.id/models"
 
11
  "github.com/gin-gonic/gin"
12
  "github.com/golang-jwt/jwt/v5"
13
- "golang.org/x/crypto/bcrypt"
14
  )
15
 
16
- // Define a secret key for signing the JWT token
17
  var salt = config.Salt
18
  var secretKey = []byte(salt)
19
 
20
- // GenerateToken generates a JWT token for the given user
21
- func GenerateToken(user *models.Account) (string, error) {
22
-
23
- // Create a new token
24
- token := jwt.New(jwt.SigningMethodHS256)
25
-
26
- // Set claims
27
- claims := token.Claims.(jwt.MapClaims)
28
- claims["id"] = user.Id
29
- claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours
30
-
31
- // Sign the token with the secret key
32
- tokenString, err := token.SignedString(secretKey)
33
- if err != nil {
34
- return "", err
35
- }
36
-
37
- return tokenString, nil
38
- }
39
-
40
  // VerifyPassword verifies if the provided password matches the hashed password
41
- func VerifyPassword(hashedPassword, password string) error {
42
- err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
43
- if err != nil {
44
- return errors.New("invalid password")
45
- }
46
- return nil
47
- }
48
- func HashPassword(password string) (string, error) {
49
- bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
50
- return string(bytes), err
51
- }
52
 
53
  type CustomClaims struct {
54
  jwt.RegisteredClaims
@@ -84,21 +51,20 @@ func AuthUser(c *gin.Context) {
84
  // fmt.Println("Verify Status :", currAccData.verifyStatus)
85
  if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" {
86
  currAccData.UserID = 0
87
- message := "Your session is expired, Please re-Login!"
88
- SendJSON401(c, &currAccData.VerifyStatus, &message)
89
  c.Abort()
90
  return
 
 
 
91
  }
92
  } else {
93
  currAccData.UserID = 0
94
  currAccData.VerifyStatus = "no-token"
95
  currAccData.ErrVerif = nil
96
- message := "You have to Login First!"
97
- SendJSON401(c, &currAccData.VerifyStatus, &message)
98
  c.Abort()
99
  return
100
  }
101
 
102
- c.Set("accountData", currAccData)
103
- c.Next()
104
  }
 
3
  package middleware
4
 
5
  import (
 
6
  "time"
7
 
8
  "api.qobiltu.id/config"
9
  "api.qobiltu.id/models"
10
+ "api.qobiltu.id/utils"
11
  "github.com/gin-gonic/gin"
12
  "github.com/golang-jwt/jwt/v5"
 
13
  )
14
 
 
15
  var salt = config.Salt
16
  var secretKey = []byte(salt)
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  // VerifyPassword verifies if the provided password matches the hashed password
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  type CustomClaims struct {
21
  jwt.RegisteredClaims
 
51
  // fmt.Println("Verify Status :", currAccData.verifyStatus)
52
  if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" {
53
  currAccData.UserID = 0
54
+ utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"})
 
55
  c.Abort()
56
  return
57
+ } else {
58
+ c.Set("accountData", currAccData)
59
+ c.Next()
60
  }
61
  } else {
62
  currAccData.UserID = 0
63
  currAccData.VerifyStatus = "no-token"
64
  currAccData.ErrVerif = nil
65
+ utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"})
 
66
  c.Abort()
67
  return
68
  }
69
 
 
 
70
  }
space/middleware/response_middleware.go CHANGED
@@ -9,31 +9,37 @@ import (
9
  // SendJSON200 sends a JSON response with HTTP status code 200
10
  func SendJSON200(c *gin.Context, data interface{}) {
11
  c.JSON(http.StatusOK, gin.H{"status": "success", "data": data})
 
12
  }
13
 
14
  // SendJSON400 sends a JSON response with HTTP status code 400
15
  func SendJSON400(c *gin.Context, error_status *string, message *string) {
16
  c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message})
 
17
  }
18
 
19
  // SendJSON401 sends a JSON response with HTTP status code 401
20
  func SendJSON401(c *gin.Context, error_status *string, message *string) {
21
  c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message})
 
22
  }
23
 
24
  // SendJSON403 sends a JSON response with HTTP status code 403
25
  func SendJSON403(c *gin.Context, message *string) {
26
  c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message})
 
27
  }
28
 
29
  // SendJSON404 sends a JSON response with HTTP status code 404
30
  func SendJSON404(c *gin.Context, message *string) {
31
  c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message})
 
32
  }
33
 
34
  // SendJSON500 sends a JSON response with HTTP status code 500
35
  func SendJSON500(c *gin.Context, error_status *string, message *string) {
36
  c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message})
 
37
  }
38
 
39
  // JSONResponseMiddleware is a middleware that provides functions for sending JSON responses
 
9
  // SendJSON200 sends a JSON response with HTTP status code 200
10
  func SendJSON200(c *gin.Context, data interface{}) {
11
  c.JSON(http.StatusOK, gin.H{"status": "success", "data": data})
12
+ return
13
  }
14
 
15
  // SendJSON400 sends a JSON response with HTTP status code 400
16
  func SendJSON400(c *gin.Context, error_status *string, message *string) {
17
  c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message})
18
+ return
19
  }
20
 
21
  // SendJSON401 sends a JSON response with HTTP status code 401
22
  func SendJSON401(c *gin.Context, error_status *string, message *string) {
23
  c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message})
24
+ return
25
  }
26
 
27
  // SendJSON403 sends a JSON response with HTTP status code 403
28
  func SendJSON403(c *gin.Context, message *string) {
29
  c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message})
30
+ return
31
  }
32
 
33
  // SendJSON404 sends a JSON response with HTTP status code 404
34
  func SendJSON404(c *gin.Context, message *string) {
35
  c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message})
36
+ return
37
  }
38
 
39
  // SendJSON500 sends a JSON response with HTTP status code 500
40
  func SendJSON500(c *gin.Context, error_status *string, message *string) {
41
  c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message})
42
+ return
43
  }
44
 
45
  // JSONResponseMiddleware is a middleware that provides functions for sending JSON responses
space/models/database_orm_model.go CHANGED
@@ -7,25 +7,113 @@ import (
7
  )
8
 
9
  type Account struct {
10
- Id uint `gorm:"primaryKey" json:"id"`
11
- UUID uuid.UUID `gorm:"type:uuid" json:"uuid" `
12
- Email string `gorm:"uniqueIndex" json:"email"`
13
- Password string `json:"password"`
14
- IsEmailVerified bool `json:"is_email_verified"`
15
- CreatedAt time.Time `json:"created_at"`
16
- DeletedAt time.Time `json:"deleted_at"`
 
17
  }
18
 
19
  type AccountDetails struct {
20
- IDDetail uint `gorm:"primaryKey" json:"id_detail"`
21
- Account_id uint `json:"id_account"`
22
- Province string `json:"province"`
23
- City string `json:"city"`
24
- Institution string `json:"institution"`
25
- UpdatedAt time.Time `json:"updated_at"`
26
- DeletedAt time.Time `json:"deleted_at"`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  }
28
 
29
  // Gorm table name settings
30
- func (Account) TableName() string { return "account" }
31
- func (AccountDetails) TableName() string { return "account_details" }
 
 
 
 
 
 
 
 
 
 
7
  )
8
 
9
  type Account struct {
10
+ Id uint `gorm:"primaryKey" json:"id"`
11
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid" `
12
+ Email string `gorm:"uniqueIndex" json:"email"`
13
+ Password string `json:"password"`
14
+ IsEmailVerified bool `json:"is_email_verified"`
15
+ IsDetailCompleted bool `json:"is_detail_completed"`
16
+ CreatedAt time.Time `json:"created_at"`
17
+ DeletedAt *time.Time `json:"deleted_at,omitempty" gorm:"default:null"`
18
  }
19
 
20
  type AccountDetails struct {
21
+ ID uint `gorm:"primaryKey" json:"id"`
22
+ AccountID uint `json:"account_id"`
23
+ InitialName string `json:"initial_name"`
24
+ FullName *string `json:"full_name,omitempty"`
25
+ DateOfBirth *time.Time `json:"date_of_birth,omitempty"`
26
+ PlaceOfBirth *string `json:"place_of_birth,omitempty"`
27
+ Domicile *string `json:"domicile,omitempty"`
28
+ LastJob *string `json:"last_job,omitempty"`
29
+ Gender *bool `json:"gender,omitempty"`
30
+ LastEducation *string `json:"last_education,omitempty"`
31
+ MaritalStatus *bool `json:"marital_status,omitempty"`
32
+ Avatar *string `json:"avatar,omitempty"`
33
+ PhoneNumber *uint `json:"phone_number,omitempty"`
34
+ }
35
+
36
+ type EmailVerification struct {
37
+ ID uint `gorm:"primaryKey" json:"id"`
38
+ Token uint `json:"token"`
39
+ AccountID uint `json:"account_id"`
40
+ IsExpired bool `json:"is_expired"`
41
+ CreatedAt time.Time `json:"created_at"`
42
+ ExpiredAt time.Time `json:"expired_at"`
43
+ }
44
+
45
+ type ExternalAuth struct {
46
+ ID uint `gorm:"primaryKey" json:"id"`
47
+ OauthID string `json:"oauth_id"`
48
+ AccountID uint `json:"account_id"`
49
+ OauthProvider string `json:"oauth_provider"`
50
+ }
51
+
52
+ type FCM struct {
53
+ ID uint `gorm:"primaryKey" json:"id"`
54
+ AccountID uint `json:"account_id"`
55
+ FCMToken string `json:"fcm_token"`
56
+ }
57
+
58
+ type ForgotPassword struct {
59
+ ID uint `gorm:"primaryKey" json:"id"`
60
+ UUID uint `json:"uuid"`
61
+ AccountID uint `json:"account_id"`
62
+ IsExpired bool `json:"is_expired"`
63
+ CreatedAt time.Time `json:"created_at"`
64
+ ExpiredAt time.Time `json:"expired_at"`
65
+ }
66
+
67
+ type Academy struct {
68
+ ID uint `gorm:"primaryKey" json:"id"`
69
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
70
+ Title string `json:"title"`
71
+ Slug string `json:"slug"`
72
+ Description string `json:"description"`
73
+ }
74
+
75
+ type AcademyMaterial struct {
76
+ ID uint `gorm:"primaryKey" json:"id"`
77
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
78
+ AcademyID uint `json:"academy_id"`
79
+ Title string `json:"title"`
80
+ Slug string `json:"slug"`
81
+ Description string `json:"description"`
82
+ }
83
+
84
+ type AcademyContent struct {
85
+ ID uint `gorm:"primaryKey" json:"id"`
86
+ UUID uint `json:"uuid"`
87
+ Title string `json:"title"`
88
+ Order uint `json:"order"`
89
+ AcademyMaterialID uint `json:"academy_material_id"`
90
+ Description string `json:"description"`
91
+ }
92
+
93
+ type AcademyMaterialProgress struct {
94
+ ID uint `gorm:"primaryKey" json:"id"`
95
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
96
+ AccountID uint `json:"account_id"`
97
+ AcademyMaterialID uint `json:"academy_material_id"`
98
+ Progress uint `json:"progress"`
99
+ }
100
+
101
+ type AcademyContentProgress struct {
102
+ ID uint `gorm:"primaryKey" json:"id"`
103
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid"`
104
+ AccountID uint `json:"account_id"`
105
+ AcademyID uint `json:"academy_id"`
106
  }
107
 
108
  // Gorm table name settings
109
+ func (Account) TableName() string { return "account" }
110
+ func (AccountDetails) TableName() string { return "account_details" }
111
+ func (EmailVerification) TableName() string { return "email_verifications" }
112
+ func (ExternalAuth) TableName() string { return "extern_auth" }
113
+ func (FCM) TableName() string { return "fcm" }
114
+ func (ForgotPassword) TableName() string { return "forgot_password" }
115
+ func (Academy) TableName() string { return "academy" }
116
+ func (AcademyMaterial) TableName() string { return "academy_materials" }
117
+ func (AcademyContent) TableName() string { return "academy_contents" }
118
+ func (AcademyMaterialProgress) TableName() string { return "academy_materials_progress" }
119
+ func (AcademyContentProgress) TableName() string { return "academy_contents_progress" }
space/models/request_model.go CHANGED
@@ -11,3 +11,8 @@ type RegisterRequest struct {
11
  Phone int `json:"phone"`
12
  Password string `json:"password" binding:"required"`
13
  }
 
 
 
 
 
 
11
  Phone int `json:"phone"`
12
  Password string `json:"password" binding:"required"`
13
  }
14
+
15
+ type CreateEmailVerificationRequest struct {
16
+ AccountID int `json:"account_id" binding:"required"`
17
+
18
+ }
space/repositories/account_repository.go CHANGED
@@ -15,6 +15,16 @@ func GetAccountbyEmail(email string) Repository[models.Account, models.Account]
15
  return *repo
16
  }
17
 
 
 
 
 
 
 
 
 
 
 
18
  func CreateAccount(account models.Account) Repository[models.Account, models.Account] {
19
  repo := Construct[models.Account, models.Account](
20
  account,
@@ -22,3 +32,7 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc
22
  Create(repo)
23
  return *repo
24
  }
 
 
 
 
 
15
  return *repo
16
  }
17
 
18
+ func GetAccountbyId(account_id uint) Repository[models.Account, models.Account] {
19
+ repo := Construct[models.Account, models.Account](
20
+ models.Account{Id: account_id},
21
+ )
22
+ repo.Transactions(
23
+ WhereGivenConstructor[models.Account, models.Account],
24
+ Find[models.Account, models.Account],
25
+ )
26
+ return *repo
27
+ }
28
  func CreateAccount(account models.Account) Repository[models.Account, models.Account] {
29
  repo := Construct[models.Account, models.Account](
30
  account,
 
32
  Create(repo)
33
  return *repo
34
  }
35
+
36
+ // func UpdateAccount(account models.Account) Repository[models.Account, models.Account] {
37
+ // repo := Construct
38
+ // }
space/repositories/email_verification_repository.go ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package repositories
2
+
3
+ import (
4
+ "time"
5
+
6
+ "api.qobiltu.id/models"
7
+ )
8
+
9
+ func CreateEmailVerification(accountId uint, dueTime time.Time, token uint) Repository[models.EmailVerification, models.EmailVerification] {
10
+ repo := Construct[models.EmailVerification, models.EmailVerification](
11
+ models.EmailVerification{
12
+ AccountID: accountId,
13
+ IsExpired: false,
14
+ ExpiredAt: dueTime,
15
+ Token: token,
16
+ },
17
+ )
18
+ Create(repo)
19
+ return *repo
20
+ }
21
+
22
+ func GetEmailVerification(account_id uint, token uint) Repository[models.EmailVerification, models.EmailVerification] {
23
+ repo := Construct[models.EmailVerification, models.EmailVerification](
24
+ models.EmailVerification{
25
+ AccountID: account_id,
26
+ IsExpired: false,
27
+ Token: token,
28
+ },
29
+ )
30
+ repo.Transactions(
31
+ WhereGivenConstructor[models.EmailVerification, models.EmailVerification],
32
+ Find[models.EmailVerification, models.EmailVerification],
33
+ )
34
+ return *repo
35
+ }
36
+
37
+ func DeleteEmailVerification(token uint) Repository[models.EmailVerification, models.EmailVerification] {
38
+ repo := Construct[models.EmailVerification, models.EmailVerification](
39
+ models.EmailVerification{
40
+ Token: token,
41
+ },
42
+ )
43
+
44
+ repo.Transactions(
45
+ WhereGivenConstructor[models.EmailVerification, models.EmailVerification],
46
+ Delete[models.EmailVerification],
47
+ )
48
+ return *repo
49
+ }
space/router/email_route.go ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package router
2
+
3
+ import (
4
+ EmailController "api.qobiltu.id/controller/email"
5
+ "github.com/gin-gonic/gin"
6
+ )
7
+
8
+ func EmailRoute(router *gin.Engine) {
9
+ routerGroup := router.Group("/api/v1/email")
10
+ {
11
+ routerGroup.POST("/verify", EmailController.CreateVerification)
12
+ routerGroup.POST("/create-verification", EmailController.CreateVerification)
13
+ routerGroup.DELETE("/delete-verification", EmailController.DeleteVerification)
14
+ }
15
+ }
space/router/router.go CHANGED
@@ -8,11 +8,8 @@ import (
8
 
9
  func StartService() {
10
  router := gin.Default()
11
- routerGroup := router.Group("/api/v1")
12
- {
13
- routerGroup.GET("/", controller.HomeController)
14
- routerGroup.POST("/login", controller.LoginController)
15
- routerGroup.POST("/register", controller.RegisterController)
16
- }
17
  router.Run(config.TCP_ADDRESS)
18
  }
 
8
 
9
  func StartService() {
10
  router := gin.Default()
11
+ router.GET("/", controller.HomeController)
12
+ UserRoute(router)
13
+ EmailRoute(router)
 
 
 
14
  router.Run(config.TCP_ADDRESS)
15
  }
space/router/user_route.go ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package router
2
+
3
+ import (
4
+ UserController "api.qobiltu.id/controller/user"
5
+ "api.qobiltu.id/middleware"
6
+ "github.com/gin-gonic/gin"
7
+ )
8
+
9
+ func UserRoute(router *gin.Engine) {
10
+ routerGroup := router.Group("/api/v1/user")
11
+ {
12
+ routerGroup.POST("/login", UserController.Login)
13
+ routerGroup.POST("/register", UserController.Register)
14
+ routerGroup.GET("/me", middleware.AuthUser, UserController.Profile)
15
+ }
16
+ }
space/services/email_verification_service.go ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "math/rand"
5
+ "time"
6
+
7
+ "api.qobiltu.id/config"
8
+ "api.qobiltu.id/models"
9
+ "api.qobiltu.id/repositories"
10
+ )
11
+
12
+ type EmailVerificationService struct {
13
+ Service[models.EmailVerification, models.EmailVerification]
14
+ }
15
+
16
+ func (s *EmailVerificationService) Create() {
17
+ accountRepo := repositories.GetAccountbyId(s.Constructor.AccountID)
18
+ if accountRepo.NoRecord {
19
+ s.Error = accountRepo.RowsError
20
+ s.Exception.DataNotFound = true
21
+ s.Exception.Message = "There is no account data with given credentials!"
22
+ return
23
+ }
24
+
25
+ remainingTime := time.Duration(config.EMAIL_VERIFICATION_DURATION) * time.Hour
26
+ dueTime := CalculateDueTime(remainingTime)
27
+
28
+ randomizer := rand.New(rand.NewSource(10))
29
+ token := uint(randomizer.Int())
30
+
31
+ repo := repositories.CreateEmailVerification(s.Constructor.AccountID, dueTime, token)
32
+
33
+ s.Error = repo.RowsError
34
+ s.Result = repo.Result
35
+ }
36
+
37
+ func (s *EmailVerificationService) Validate() {
38
+ repo := repositories.GetEmailVerification(s.Constructor.AccountID, s.Constructor.Token)
39
+ s.Error = repo.RowsError
40
+ if repo.NoRecord {
41
+ s.Exception.DataNotFound = true
42
+ s.Exception.Message = "Invalid token!"
43
+ return
44
+ }
45
+ s.Result = repo.Result
46
+ }
47
+
48
+ func (s *EmailVerificationService) Delete() {
49
+ repo := repositories.DeleteEmailVerification(s.Constructor.Token)
50
+ s.Error = repo.RowsError
51
+ if repo.NoRecord {
52
+ s.Exception.DataNotFound = true
53
+ s.Exception.Message = "Invalid token!"
54
+ return
55
+ }
56
+ s.Result = repo.Result
57
+ }
space/services/jwt_service.go ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "errors"
5
+ "time"
6
+
7
+ "api.qobiltu.id/config"
8
+ "api.qobiltu.id/models"
9
+ "github.com/dgrijalva/jwt-go"
10
+ "golang.org/x/crypto/bcrypt"
11
+ )
12
+
13
+ var salt = config.Salt
14
+ var secretKey = []byte(salt)
15
+
16
+ func GenerateToken(user *models.Account) (string, error) {
17
+
18
+ // Create a new token
19
+ token := jwt.New(jwt.SigningMethodHS256)
20
+
21
+ // Set claims
22
+ claims := token.Claims.(jwt.MapClaims)
23
+ claims["id"] = user.Id
24
+ claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours
25
+
26
+ // Sign the token with the secret key
27
+ tokenString, err := token.SignedString(secretKey)
28
+ if err != nil {
29
+ return "", err
30
+ }
31
+
32
+ return tokenString, nil
33
+ }
34
+
35
+ func VerifyPassword(hashedPassword, password string) error {
36
+ err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
37
+ if err != nil {
38
+ return errors.New("invalid password")
39
+ }
40
+ return nil
41
+ }
42
+ func HashPassword(password string) (string, error) {
43
+ bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
44
+ return string(bytes), err
45
+ }
space/services/login_service.go CHANGED
@@ -3,7 +3,6 @@ package services
3
  import (
4
  "errors"
5
 
6
- "api.qobiltu.id/middleware"
7
  "api.qobiltu.id/models"
8
  "api.qobiltu.id/repositories"
9
  )
@@ -24,13 +23,13 @@ func (s *AuthenticationService) Authenticate() {
24
  s.Exception.Message = "there is no account with given credentials!"
25
  return
26
  }
27
- if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
28
  s.Exception.Unauthorized = true
29
  s.Exception.Message = "incorrect password!"
30
  return
31
  }
32
 
33
- token, err_tok := middleware.GenerateToken(&accountData.Result)
34
 
35
  if err_tok != nil {
36
  s.Error = errors.Join(s.Error, err_tok)
 
3
  import (
4
  "errors"
5
 
 
6
  "api.qobiltu.id/models"
7
  "api.qobiltu.id/repositories"
8
  )
 
23
  s.Exception.Message = "there is no account with given credentials!"
24
  return
25
  }
26
+ if VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
27
  s.Exception.Unauthorized = true
28
  s.Exception.Message = "incorrect password!"
29
  return
30
  }
31
 
32
+ token, err_tok := GenerateToken(&accountData.Result)
33
 
34
  if err_tok != nil {
35
  s.Error = errors.Join(s.Error, err_tok)
space/services/register_service.go CHANGED
@@ -3,7 +3,6 @@ package services
3
  import (
4
  "errors"
5
 
6
- "api.qobiltu.id/middleware"
7
  "api.qobiltu.id/models"
8
  "api.qobiltu.id/repositories"
9
  uuid "github.com/satori/go.uuid"
@@ -20,7 +19,7 @@ func (s *RegisterService) Create() {
20
  s.Exception.Message = "Password must have at least 8 characters!"
21
  return
22
  }
23
- hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password)
24
  s.Error = err_hash
25
  s.Constructor.Password = hashed_password
26
  s.Constructor.UUID = uuid.NewV4()
 
3
  import (
4
  "errors"
5
 
 
6
  "api.qobiltu.id/models"
7
  "api.qobiltu.id/repositories"
8
  uuid "github.com/satori/go.uuid"
 
19
  s.Exception.Message = "Password must have at least 8 characters!"
20
  return
21
  }
22
+ hashed_password, err_hash := HashPassword(s.Constructor.Password)
23
  s.Error = err_hash
24
  s.Constructor.Password = hashed_password
25
  s.Constructor.UUID = uuid.NewV4()
space/services/service.go CHANGED
@@ -1,6 +1,10 @@
1
  package services
2
 
3
- import "api.qobiltu.id/models"
 
 
 
 
4
 
5
  type (
6
  Services interface {
@@ -29,3 +33,7 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Serv
29
  Constructor: constructor[0],
30
  }
31
  }
 
 
 
 
 
1
  package services
2
 
3
+ import (
4
+ "time"
5
+
6
+ "api.qobiltu.id/models"
7
+ )
8
 
9
  type (
10
  Services interface {
 
33
  Constructor: constructor[0],
34
  }
35
  }
36
+
37
+ func CalculateDueTime(duration time.Duration) time.Time {
38
+ return time.Now().Add(duration)
39
+ }
space/services/user_profile_service.go ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "api.qobiltu.id/models"
5
+ "api.qobiltu.id/repositories"
6
+ )
7
+
8
+ type UserProfileConstructor struct {
9
+ AccountId int
10
+ }
11
+ type UserProfileService struct {
12
+ Service[UserProfileConstructor, models.Account]
13
+ }
14
+
15
+ func (s *UserProfileService) Retrieve() {
16
+ userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId))
17
+ s.Error = userProfile.RowsError
18
+ if userProfile.NoRecord {
19
+ s.Exception.DataNotFound = true
20
+ s.Exception.Message = "There is no account with given credentials!"
21
+ return
22
+ }
23
+ s.Result.Password = "SECRET"
24
+ s.Result = userProfile.Result
25
+ }
26
+
27
+ func (s *UserProfileService) Update() {
28
+ userProfile := repositories.GetAccountbyId(uint(s.Constructor.AccountId))
29
+ s.Error = userProfile.RowsError
30
+ if userProfile.NoRecord {
31
+ s.Exception.DataNotFound = true
32
+ s.Exception.Message = "There is no account with given credentials!"
33
+ return
34
+ }
35
+ s.Result.Password = "SECRET"
36
+ s.Result = userProfile.Result
37
+ }
space/space/LICENSE ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025 Abdan Hafidz
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
space/space/space/.github/workflows/main.yml CHANGED
@@ -49,4 +49,4 @@ jobs:
49
  cd space
50
  git add .
51
  git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
- git push --force origin main || echo "No changes to push"
 
49
  cd space
50
  git add .
51
  git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
+ git push origin main || echo "No changes to push"
space/space/space/space/.github/workflows/main.yml CHANGED
@@ -49,4 +49,4 @@ jobs:
49
  cd space
50
  git add .
51
  git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
- git push origin main || echo "No changes to push"
 
49
  cd space
50
  git add .
51
  git commit -m "Deploy files from GitHub repository" || echo "No changes to commit"
52
+ git push --force origin main || echo "No changes to push"
space/space/space/space/space/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
  .env
2
  vendor/
3
- quzuu-be.exe
 
 
1
  .env
2
  vendor/
3
+ quzuu-be.exe
4
+ README.md
space/space/space/space/space/config/config.go ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package config
2
+
3
+ import (
4
+ "os"
5
+
6
+ "github.com/joho/godotenv"
7
+ )
8
+
9
+ var TCP_ADDRESS string
10
+ var LOG_PATH string
11
+ var HOST_ADDRESS string
12
+ var HOST_PORT string
13
+
14
+ func init() {
15
+ godotenv.Load()
16
+ HOST_ADDRESS = os.Getenv("HOST_ADDRESS")
17
+ HOST_PORT = os.Getenv("HOST_PORT")
18
+ TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT
19
+ LOG_PATH = os.Getenv("LOG_PATH")
20
+ // Menampilkan nilai variabel lingkungan
21
+ }
space/space/space/space/space/config/database_connection_config.go ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package config
2
+
3
+ import (
4
+ "fmt"
5
+ "log"
6
+ "os"
7
+
8
+ "gorm.io/driver/postgres"
9
+ "gorm.io/gorm"
10
+ "gorm.io/gorm/logger"
11
+
12
+ "api.qobiltu.id/models"
13
+ "github.com/joho/godotenv"
14
+ )
15
+
16
+ var DB *gorm.DB
17
+ var err error
18
+ var Salt string
19
+
20
+ func init() {
21
+ godotenv.Load()
22
+ if err != nil {
23
+ fmt.Println("Gagal membaca file .env")
24
+ return
25
+ }
26
+ os.Setenv("TZ", "Asia/Jakarta")
27
+ dbHost := os.Getenv("DB_HOST")
28
+ dbPort := os.Getenv("DB_PORT")
29
+ dbUser := os.Getenv("DB_USER")
30
+ dbPassword := os.Getenv("DB_PASSWORD")
31
+ dbName := os.Getenv("DB_NAME")
32
+ Salt := os.Getenv("SALT")
33
+ dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta"
34
+ DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true})
35
+ if err != nil {
36
+ panic(err)
37
+ }
38
+ if Salt == "" {
39
+ Salt = "D3f4u|t"
40
+ }
41
+
42
+ // Call AutoMigrateAll to perform auto-migration
43
+ AutoMigrateAll(DB)
44
+ }
45
+
46
+ func AutoMigrateAll(db *gorm.DB) {
47
+ // Enable logger to see SQL logs
48
+ db.Logger.LogMode(logger.Info)
49
+
50
+ // Auto-migrate all models
51
+ err := db.AutoMigrate(
52
+ &models.Account{},
53
+ &models.AccountDetails{},
54
+ )
55
+ if err != nil {
56
+ log.Fatal(err)
57
+ }
58
+
59
+ fmt.Println("Migration completed successfully.")
60
+ }
space/space/space/space/space/controller/home_controller.go ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import "github.com/gin-gonic/gin"
4
+
5
+ func HomeController(c *gin.Context) {
6
+ c.JSON(200, gin.H{
7
+ "message": "Api Qobiltu 2025!",
8
+ })
9
+ }
space/space/space/space/space/controller/login_controller.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "api.qobiltu.id/models"
5
+ "api.qobiltu.id/services"
6
+ "github.com/gin-gonic/gin"
7
+ )
8
+
9
+ func LoginController(c *gin.Context) {
10
+ authentication := services.AuthenticationService{}
11
+ loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{
12
+ Service: &authentication.Service,
13
+ }
14
+ loginController.RequestJSON(c, func() {
15
+ loginController.Service.Constructor.Email = loginController.Request.Email
16
+ loginController.Service.Constructor.Password = loginController.Request.Password
17
+ authentication.Authenticate()
18
+ })
19
+ }
space/space/space/space/space/controller/register_controller.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "api.qobiltu.id/models"
5
+ "api.qobiltu.id/services"
6
+ "github.com/gin-gonic/gin"
7
+ )
8
+
9
+ func RegisterController(c *gin.Context) {
10
+ register := services.RegisterService{}
11
+ registerController := Controller[models.RegisterRequest, models.Account, models.Account]{
12
+ Service: &register.Service,
13
+ }
14
+ registerController.RequestJSON(c, func() {
15
+ registerController.Service.Constructor.Password = registerController.Request.Password
16
+ registerController.Service.Constructor.Email = registerController.Request.Email
17
+ register.Create()
18
+ })
19
+ }
space/space/space/space/space/middleware/authentication_middleware.go ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // auth/auth.go
2
+
3
+ package middleware
4
+
5
+ import (
6
+ "errors"
7
+ "time"
8
+
9
+ "api.qobiltu.id/config"
10
+ "api.qobiltu.id/models"
11
+ "github.com/gin-gonic/gin"
12
+ "github.com/golang-jwt/jwt/v5"
13
+ "golang.org/x/crypto/bcrypt"
14
+ )
15
+
16
+ // Define a secret key for signing the JWT token
17
+ var salt = config.Salt
18
+ var secretKey = []byte(salt)
19
+
20
+ // GenerateToken generates a JWT token for the given user
21
+ func GenerateToken(user *models.Account) (string, error) {
22
+
23
+ // Create a new token
24
+ token := jwt.New(jwt.SigningMethodHS256)
25
+
26
+ // Set claims
27
+ claims := token.Claims.(jwt.MapClaims)
28
+ claims["id"] = user.Id
29
+ claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours
30
+
31
+ // Sign the token with the secret key
32
+ tokenString, err := token.SignedString(secretKey)
33
+ if err != nil {
34
+ return "", err
35
+ }
36
+
37
+ return tokenString, nil
38
+ }
39
+
40
+ // VerifyPassword verifies if the provided password matches the hashed password
41
+ func VerifyPassword(hashedPassword, password string) error {
42
+ err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
43
+ if err != nil {
44
+ return errors.New("invalid password")
45
+ }
46
+ return nil
47
+ }
48
+ func HashPassword(password string) (string, error) {
49
+ bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14)
50
+ return string(bytes), err
51
+ }
52
+
53
+ type CustomClaims struct {
54
+ jwt.RegisteredClaims
55
+ UserID int `json:"id"`
56
+ }
57
+
58
+ func VerifyToken(bearer_token string) (int, string, error) {
59
+ // fmt.Println(bearer_token)
60
+ token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
61
+ return secretKey, nil
62
+ })
63
+ if err != nil {
64
+ return 0, "invalid-token", err
65
+ }
66
+
67
+ // Extract the claims
68
+ claims, ok := token.Claims.(*CustomClaims)
69
+ if !ok || !token.Valid {
70
+ return 0, "invalid-token", err
71
+ }
72
+ if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) {
73
+ return 0, "expired", err
74
+ }
75
+
76
+ return claims.UserID, "valid", err
77
+ }
78
+
79
+ func AuthUser(c *gin.Context) {
80
+ var currAccData models.AccountData
81
+ if c.Request.Header["Auth-Bearer-Token"] != nil {
82
+ token := c.Request.Header["Auth-Bearer-Token"]
83
+ currAccData.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0])
84
+ // fmt.Println("Verify Status :", currAccData.verifyStatus)
85
+ if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" {
86
+ currAccData.UserID = 0
87
+ message := "Your session is expired, Please re-Login!"
88
+ SendJSON401(c, &currAccData.VerifyStatus, &message)
89
+ c.Abort()
90
+ return
91
+ }
92
+ } else {
93
+ currAccData.UserID = 0
94
+ currAccData.VerifyStatus = "no-token"
95
+ currAccData.ErrVerif = nil
96
+ message := "You have to Login First!"
97
+ SendJSON401(c, &currAccData.VerifyStatus, &message)
98
+ c.Abort()
99
+ return
100
+ }
101
+
102
+ c.Set("accountData", currAccData)
103
+ c.Next()
104
+ }
space/space/space/space/space/middleware/response_middleware.go ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package middleware
2
+
3
+ import (
4
+ "net/http"
5
+
6
+ "github.com/gin-gonic/gin"
7
+ )
8
+
9
+ // SendJSON200 sends a JSON response with HTTP status code 200
10
+ func SendJSON200(c *gin.Context, data interface{}) {
11
+ c.JSON(http.StatusOK, gin.H{"status": "success", "data": data})
12
+ }
13
+
14
+ // SendJSON400 sends a JSON response with HTTP status code 400
15
+ func SendJSON400(c *gin.Context, error_status *string, message *string) {
16
+ c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message})
17
+ }
18
+
19
+ // SendJSON401 sends a JSON response with HTTP status code 401
20
+ func SendJSON401(c *gin.Context, error_status *string, message *string) {
21
+ c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message})
22
+ }
23
+
24
+ // SendJSON403 sends a JSON response with HTTP status code 403
25
+ func SendJSON403(c *gin.Context, message *string) {
26
+ c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message})
27
+ }
28
+
29
+ // SendJSON404 sends a JSON response with HTTP status code 404
30
+ func SendJSON404(c *gin.Context, message *string) {
31
+ c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message})
32
+ }
33
+
34
+ // SendJSON500 sends a JSON response with HTTP status code 500
35
+ func SendJSON500(c *gin.Context, error_status *string, message *string) {
36
+ c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message})
37
+ }
38
+
39
+ // JSONResponseMiddleware is a middleware that provides functions for sending JSON responses
space/space/space/space/space/models/authentication_payload_model.go ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ type AccountData struct {
4
+ UserID int
5
+ VerifyStatus string
6
+ ErrVerif error
7
+ }
space/space/space/space/space/models/database_orm_model.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ import (
4
+ "time"
5
+
6
+ uuid "github.com/satori/go.uuid"
7
+ )
8
+
9
+ type Account struct {
10
+ Id uint `gorm:"primaryKey" json:"id"`
11
+ UUID uuid.UUID `gorm:"type:uuid" json:"uuid" `
12
+ Email string `gorm:"uniqueIndex" json:"email"`
13
+ Password string `json:"password"`
14
+ IsEmailVerified bool `json:"is_email_verified"`
15
+ CreatedAt time.Time `json:"created_at"`
16
+ DeletedAt time.Time `json:"deleted_at"`
17
+ }
18
+
19
+ type AccountDetails struct {
20
+ IDDetail uint `gorm:"primaryKey" json:"id_detail"`
21
+ Account_id uint `json:"id_account"`
22
+ Province string `json:"province"`
23
+ City string `json:"city"`
24
+ Institution string `json:"institution"`
25
+ UpdatedAt time.Time `json:"updated_at"`
26
+ DeletedAt time.Time `json:"deleted_at"`
27
+ }
28
+
29
+ // Gorm table name settings
30
+ func (Account) TableName() string { return "account" }
31
+ func (AccountDetails) TableName() string { return "account_details" }
space/space/space/space/space/models/exception_model.go ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ type Exception struct {
4
+ Unauthorized bool `json:"unauthorized,omitempty"`
5
+ BadRequest bool `json:"bad_request,omitempty"`
6
+ DataNotFound bool `json:"data_not_found,omitempty"`
7
+ InternalServerError bool `json:"internal_server_error,omitempty"`
8
+ DataDuplicate bool `json:"data_duplicate,omitempty"`
9
+ QueryError bool `json:"query_error,omitempty"`
10
+ InvalidPasswordLength bool `json:"invalid_password_length,omitempty"`
11
+ Message string `json:"message,omitempty"`
12
+ }
space/space/space/space/space/models/model.go ADDED
@@ -0,0 +1 @@
 
 
1
+ package models