lifedebugger commited on
Commit
8d3c966
·
1 Parent(s): 784e2b7

Deploy files from GitHub repository

Browse files
.env.example ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ DB_HOST =
2
+ DB_USER =
3
+ DB_PASSWORD =
4
+ DB_PORT =
5
+ DB_NAME =
6
+ SALT =
7
+ HOST_ADDRESS =
8
+ HOST_PORT =
.github/workflows/main.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"
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .env
2
+ vendor/
3
+ quzuu-be.exe
Dockerfile ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gunakan image dasar Golang versi 1.21.6
2
+ FROM golang:1.21.6
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
+
26
+ # Build aplikasi
27
+ RUN go build -o main .
28
+
29
+ # Jalankan aplikasi
30
+ CMD ["./main"]
README.md CHANGED
@@ -1,10 +1,8 @@
1
- ---
2
- title: Api Qobiltu Dev
3
- emoji: 🏆
4
- colorFrom: green
5
- colorTo: red
6
- sdk: docker
7
- pinned: false
8
- ---
9
-
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Quzuu Api Dev
3
+ emoji: 🐠
4
+ colorFrom: indigo
5
+ colorTo: gray
6
+ sdk: docker
7
+ pinned: false
8
+ ---
 
 
config/DatabaseConfig.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
+ "github.com/joho/godotenv"
13
+ "go-dp.abdanhafidz.com/models"
14
+ )
15
+
16
+ func AutoMigrateAll(db *gorm.DB) {
17
+ // Enable logger to see SQL logs
18
+ db.Logger.LogMode(logger.Info)
19
+
20
+ // Auto-migrate all models
21
+ err := db.AutoMigrate(
22
+ &models.Account{},
23
+ &models.AccountDetails{},
24
+ )
25
+ if err != nil {
26
+ log.Fatal(err)
27
+ }
28
+
29
+ fmt.Println("Migration completed successfully.")
30
+ }
31
+
32
+ var DB *gorm.DB
33
+ var err error
34
+ var Salt string
35
+
36
+ func init() {
37
+ godotenv.Load()
38
+ if err != nil {
39
+ fmt.Println("Gagal membaca file .env")
40
+ return
41
+ }
42
+ os.Setenv("TZ", "Asia/Jakarta")
43
+ dbHost := os.Getenv("DB_HOST")
44
+ dbPort := os.Getenv("DB_PORT")
45
+ dbUser := os.Getenv("DB_USER")
46
+ dbPassword := os.Getenv("DB_PASSWORD")
47
+ dbName := os.Getenv("DB_NAME")
48
+ Salt := os.Getenv("SALT")
49
+ dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta"
50
+ DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true})
51
+ if err != nil {
52
+ panic(err)
53
+ }
54
+ if Salt == "" {
55
+ Salt = "D3f4u|t"
56
+ }
57
+
58
+ // Call AutoMigrateAll to perform auto-migration
59
+ AutoMigrateAll(DB)
60
+ }
config/EnvConfig.go ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package config
2
+
3
+ import "os"
4
+
5
+ var TCP_ADDRESS string
6
+ var LOG_PATH string
7
+ var HOST_ADDRESS string
8
+ var HOST_PORT string
9
+
10
+ func init() {
11
+ HOST_ADDRESS = os.Getenv("HOST_ADDRESS")
12
+ HOST_PORT = os.Getenv("HOST_PORT")
13
+ TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT
14
+ LOG_PATH = os.Getenv("LOG_PATH")
15
+ // Menampilkan nilai variabel lingkungan
16
+ }
controller/HomeController.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 Quzuu Backend 2024!",
8
+ })
9
+ }
controller/LoginController.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "github.com/gin-gonic/gin"
5
+ "go-dp.abdanhafidz.com/models"
6
+ "go-dp.abdanhafidz.com/services"
7
+ )
8
+
9
+ func LoginController(c *gin.Context) {
10
+ authentication := services.AuthenticationService{}
11
+ loginController := Controller[models.LoginRequest, services.LoginConstructor, models.Account]{
12
+ Service: &authentication.Service,
13
+ }
14
+ loginController.RequestJSON(c)
15
+ loginController.Service.Constructor.Email = loginController.Request.Email
16
+ loginController.Service.Constructor.Password = loginController.Request.Password
17
+ authentication.Authenticate()
18
+ loginController.Response(c)
19
+ }
controller/RegisterController.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "github.com/gin-gonic/gin"
5
+ "go-dp.abdanhafidz.com/models"
6
+ "go-dp.abdanhafidz.com/services"
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)
15
+ registerController.Service.Constructor.Password = registerController.Request.Password
16
+ registerController.Service.Constructor.Email = registerController.Request.Email
17
+ register.Create()
18
+ registerController.Response(c)
19
+ }
controller/controller.go ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package controller
2
+
3
+ import (
4
+ "github.com/gin-gonic/gin"
5
+ "go-dp.abdanhafidz.com/models"
6
+ "go-dp.abdanhafidz.com/services"
7
+ "go-dp.abdanhafidz.com/utils"
8
+ )
9
+
10
+ type (
11
+ Controllers interface {
12
+ RequestJSON(c *gin.Context)
13
+ Response(c *gin.Context)
14
+ }
15
+ Controller[T1 any, T2 any, T3 any] struct {
16
+ AccountData models.AccountData
17
+ Request T1
18
+ Service *services.Service[T2, T3]
19
+ }
20
+ )
21
+
22
+ func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) {
23
+ cParam, _ := c.Get("accountData")
24
+ if cParam != nil {
25
+ controller.AccountData = cParam.(models.AccountData)
26
+ }
27
+ errBinding := c.ShouldBindJSON(&controller.Request)
28
+ if errBinding != nil {
29
+ utils.ResponseFAIL(c, 400, models.Exception{
30
+ BadRequest: true,
31
+ Message: "Invalid Request Type",
32
+ })
33
+ return
34
+ }
35
+ }
36
+ func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) {
37
+ switch {
38
+ case controller.Service.Error != nil:
39
+ utils.ResponseFAIL(c, 500, models.Exception{
40
+ InternalServerError: true,
41
+ Message: "Internal Server Error",
42
+ })
43
+ utils.LogError(controller.Service.Error)
44
+ case controller.Service.Exception.Unauthorized:
45
+ utils.ResponseFAIL(c, 401, controller.Service.Exception)
46
+ case controller.Service.Exception.DataNotFound:
47
+ utils.ResponseFAIL(c, 404, controller.Service.Exception)
48
+ case controller.Service.Exception.Message != "":
49
+ utils.ResponseFAIL(c, 400, controller.Service.Exception)
50
+ default:
51
+ utils.ResponseOK(c, controller.Service.Result)
52
+ }
53
+ }
go.mod ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ module go-dp.abdanhafidz.com
2
+
3
+ go 1.21.0
4
+
5
+ require (
6
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible
7
+ github.com/gin-gonic/gin v1.9.1
8
+ github.com/joho/godotenv v1.5.1
9
+ golang.org/x/crypto v0.32.0
10
+ gorm.io/driver/postgres v1.5.4
11
+ gorm.io/gorm v1.25.5
12
+ )
13
+
14
+ require (
15
+ github.com/bytedance/sonic v1.10.2 // indirect
16
+ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
17
+ github.com/chenzhuoyu/iasm v0.9.0 // indirect
18
+ github.com/gabriel-vasile/mimetype v1.4.8 // indirect
19
+ github.com/gin-contrib/sse v0.1.0 // indirect
20
+ github.com/go-playground/locales v0.14.1 // indirect
21
+ github.com/go-playground/universal-translator v0.18.1 // indirect
22
+ github.com/go-playground/validator/v10 v10.25.0 // indirect
23
+ github.com/goccy/go-json v0.10.2 // indirect
24
+ github.com/jackc/pgpassfile v1.0.0 // indirect
25
+ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
26
+ github.com/jackc/pgx/v5 v5.5.0 // indirect
27
+ github.com/jackc/puddle/v2 v2.2.1 // indirect
28
+ github.com/jinzhu/inflection v1.0.0 // indirect
29
+ github.com/jinzhu/now v1.1.5 // indirect
30
+ github.com/json-iterator/go v1.1.12 // indirect
31
+ github.com/klauspost/cpuid/v2 v2.2.6 // indirect
32
+ github.com/kr/text v0.2.0 // indirect
33
+ github.com/leodido/go-urn v1.4.0 // indirect
34
+ github.com/mattn/go-isatty v0.0.20 // indirect
35
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
36
+ github.com/modern-go/reflect2 v1.0.2 // indirect
37
+ github.com/pelletier/go-toml/v2 v2.1.0 // indirect
38
+ github.com/rogpeppe/go-internal v1.11.0 // indirect
39
+ github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
40
+ github.com/ugorji/go/codec v1.2.11 // indirect
41
+ golang.org/x/arch v0.6.0 // indirect
42
+ golang.org/x/net v0.34.0 // indirect
43
+ golang.org/x/sync v0.10.0 // indirect
44
+ golang.org/x/sys v0.29.0 // indirect
45
+ golang.org/x/text v0.21.0 // indirect
46
+ google.golang.org/protobuf v1.31.0 // indirect
47
+ gopkg.in/yaml.v3 v3.0.1 // indirect
48
+ )
go.sum ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
2
+ github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
3
+ github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
4
+ github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
5
+ github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
6
+ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
7
+ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
8
+ github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
9
+ github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
10
+ github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
11
+ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
+ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
13
+ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
14
+ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
15
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
16
+ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
17
+ github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
18
+ github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
19
+ github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
20
+ github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
21
+ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
22
+ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
23
+ github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
24
+ github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
25
+ github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
26
+ github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
27
+ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
28
+ github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
29
+ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
30
+ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
31
+ github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE=
32
+ github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
33
+ github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8=
34
+ github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus=
35
+ github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
36
+ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
37
+ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
38
+ github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
39
+ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
40
+ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
41
+ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
42
+ github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
43
+ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
44
+ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
45
+ github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw=
46
+ github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
47
+ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
48
+ github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
49
+ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
50
+ github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
51
+ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
52
+ github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
53
+ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
54
+ github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
55
+ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
56
+ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
57
+ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
58
+ github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
59
+ github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
60
+ github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
61
+ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
62
+ github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
63
+ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
64
+ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
65
+ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
66
+ github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
67
+ github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
68
+ github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
69
+ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
70
+ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
71
+ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
72
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
73
+ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
74
+ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
75
+ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
76
+ github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
77
+ github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
78
+ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
79
+ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
80
+ github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
81
+ github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
82
+ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
83
+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
84
+ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
85
+ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
86
+ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
87
+ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
88
+ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
89
+ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
90
+ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
91
+ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
92
+ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
93
+ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
94
+ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
95
+ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
96
+ github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
97
+ golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
98
+ golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
99
+ golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
100
+ golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
101
+ golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
102
+ golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
103
+ golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
104
+ golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
105
+ golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
106
+ golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
107
+ golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
108
+ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
109
+ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
110
+ golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
111
+ golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
112
+ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
113
+ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
114
+ golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
115
+ golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
116
+ golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
117
+ golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
118
+ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
119
+ golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
120
+ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
121
+ golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
122
+ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
123
+ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
124
+ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
125
+ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
126
+ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
127
+ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
128
+ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
129
+ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
130
+ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
131
+ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
132
+ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
133
+ gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
134
+ gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
135
+ gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
136
+ gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
137
+ nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
138
+ rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
logs/error_log.txt ADDED
File without changes
logs/security_log.txt ADDED
File without changes
main.go ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "go-dp.abdanhafidz.com/config"
7
+ "go-dp.abdanhafidz.com/router"
8
+ )
9
+
10
+ func main() {
11
+ fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT)
12
+ router.StartService()
13
+
14
+ }
middleware/AuthMiddleware.go ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // auth/auth.go
2
+
3
+ package middleware
4
+
5
+ import (
6
+ "errors"
7
+ "time"
8
+
9
+ "github.com/dgrijalva/jwt-go"
10
+ "github.com/gin-gonic/gin"
11
+ "go-dp.abdanhafidz.com/config"
12
+ "go-dp.abdanhafidz.com/models"
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.IDAccount
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.StandardClaims
55
+ IDUser 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
+ claims, ok := token.Claims.(*CustomClaims)
67
+ if !ok || !token.Valid {
68
+ return 0, "invalid-token", err
69
+ } else if claims.StandardClaims.ExpiresAt != 0 && claims.ExpiresAt < time.Now().Unix() {
70
+ return 0, "expired", err
71
+ } else if !ok && token.Valid {
72
+ return 0, "invalid-token", err
73
+ }
74
+
75
+ return claims.IDUser, "valid", err
76
+ }
77
+
78
+ func AuthUser(c *gin.Context) {
79
+ var currAccData models.AccountData
80
+ if c.Request.Header["Auth-Bearer-Token"] != nil {
81
+ token := c.Request.Header["Auth-Bearer-Token"]
82
+ currAccData.IdUser, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0])
83
+ // fmt.Println("Verify Status :", currAccData.verifyStatus)
84
+ if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" {
85
+ currAccData.IdUser = 0
86
+ message := "Your session is expired, Please re-Login!"
87
+ SendJSON401(c, &currAccData.VerifyStatus, &message)
88
+ c.Abort()
89
+ return
90
+ }
91
+ } else {
92
+ currAccData.IdUser = 0
93
+ currAccData.VerifyStatus = "no-token"
94
+ currAccData.ErrVerif = nil
95
+ message := "You have to Login First!"
96
+ SendJSON401(c, &currAccData.VerifyStatus, &message)
97
+ c.Abort()
98
+ return
99
+ }
100
+
101
+ c.Set("accountData", currAccData)
102
+ c.Next()
103
+ }
middleware/middleware.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package middleware
2
+
3
+ import (
4
+ "math"
5
+ "time"
6
+
7
+ "gorm.io/gorm"
8
+ )
9
+
10
+ func RecordCheck(rows *gorm.DB) (string, error) {
11
+ count := rows.RowsAffected
12
+ err := rows.Error
13
+ // fmt.Println(rows)
14
+ // fmt.Println(count)
15
+ if count == 0 {
16
+ return "no-record", err
17
+ } else if err != nil {
18
+ return "query-error", err
19
+ } else {
20
+ return "ok", err
21
+ }
22
+ }
23
+
24
+ func DiffTime(t1 time.Time, t2 time.Time) (int, int, int) {
25
+ hs := t1.Sub(t2).Hours()
26
+ hs, mf := math.Modf(hs)
27
+ ms := mf * 60
28
+ ms, sf := math.Modf(ms)
29
+ ss := sf * 60
30
+ return int(hs), int(ms), int(ss)
31
+ }
middleware/responseMiddleware.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
models/AuthModel.go ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ type AccountData struct {
4
+ IdUser int
5
+ VerifyStatus string
6
+ ErrVerif error
7
+ }
models/DatabaseModel.go ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ import (
4
+ "time"
5
+ )
6
+
7
+ type Account struct {
8
+ IDAccount uint `gorm:"primaryKey" json:"id_account"`
9
+ Name string `json:"name"`
10
+ Username string `json:"username"`
11
+ Email string `json:"email"`
12
+ Password string `json:"password"`
13
+ PhoneNumber int `json:"phone_number"`
14
+ CreatedAt time.Time `json:"created_at"`
15
+ DeletedAt time.Time `json:"deleted_at"`
16
+ }
17
+
18
+ type AccountDetails struct {
19
+ IDDetail uint `gorm:"primaryKey" json:"id_detail"`
20
+ IDAccount uint `json:"id_account"`
21
+ Province string `json:"province"`
22
+ City string `json:"city"`
23
+ Institution string `json:"institution"`
24
+ UpdatedAt time.Time `json:"updated_at"`
25
+ DeletedAt time.Time `json:"deleted_at"`
26
+ }
27
+
28
+ // Gorm table name settings
29
+ func (Account) TableName() string { return "account" }
30
+ func (AccountDetails) TableName() string { return "account_details" }
models/ExceptionModel.go ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
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
+ Message string `json:"message,omitempty"`
11
+ }
models/RequestModel.go ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package models
2
+
3
+ type LoginRequest struct {
4
+ Email string `json:"email" binding:"required"`
5
+ Password string `json:"password" binding:"required"`
6
+ }
7
+
8
+ type RegisterRequest struct {
9
+ Name string `json:"name"`
10
+ Email string `json:"email" binding:"required,email"`
11
+ Phone int `json:"phone"`
12
+ Password string `json:"password" binding:"required"`
13
+ }
models/ResponseModel.go ADDED
@@ -0,0 +1 @@
 
 
1
+ package models
repositories/AccountRepository.go ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package repositories
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "go-dp.abdanhafidz.com/models"
7
+ )
8
+
9
+ func GetAccountbyEmailPassword(username string, password string) Repository[models.Account, models.Account] {
10
+ repo := Construct[models.Account, models.Account](
11
+ models.Account{Username: username, Password: password},
12
+ )
13
+ Find(repo)
14
+ return *repo
15
+ }
16
+
17
+ func CreateAccount(account models.Account) Repository[models.Account, models.Account] {
18
+ fmt.Println(account)
19
+ repo := Construct[models.Account, models.Account](
20
+ account,
21
+ )
22
+ Create(repo)
23
+ return *repo
24
+ }
repositories/DatabaseScoope.go ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ package repositories
2
+
3
+ import "go-dp.abdanhafidz.com/config"
4
+
5
+ var db = config.DB
repositories/repositories.go ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package repositories
2
+
3
+ import (
4
+ "fmt"
5
+
6
+ "go-dp.abdanhafidz.com/config"
7
+ "gorm.io/gorm"
8
+ )
9
+
10
+ type Repositories interface {
11
+ FindAllPaginate()
12
+ Where()
13
+ Find()
14
+ Create()
15
+ Update()
16
+ CustomQuery()
17
+ Delete()
18
+ }
19
+ type PaginationConstructor struct {
20
+ Limit int
21
+ Offset int
22
+ Filter string
23
+ }
24
+
25
+ type CustomQueryConstructor struct {
26
+ SQL string
27
+ Values interface{}
28
+ }
29
+
30
+ type Repository[TConstructor any, TResult any] struct {
31
+ Constructor TConstructor
32
+ Pagination PaginationConstructor
33
+ CustomQuery CustomQueryConstructor
34
+ Result TResult
35
+ Transaction *gorm.DB
36
+ RowsCount int
37
+ NoRecord bool
38
+ RowsError error
39
+ }
40
+
41
+ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] {
42
+ fmt.Println("Len = ", len(constructor))
43
+ if len(constructor) == 1 {
44
+ return &Repository[TConstructor, TResult]{
45
+ Constructor: constructor[0],
46
+ Transaction: config.DB,
47
+ }
48
+ }
49
+ return &Repository[TConstructor, TResult]{
50
+ Constructor: constructor[0],
51
+ Transaction: config.DB.Begin(),
52
+ }
53
+ }
54
+ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2])) {
55
+ i := 1
56
+ for _, tx := range transactions {
57
+ tx(repo)
58
+ repo.RowsError = repo.Transaction.Error
59
+ if repo.RowsError != nil {
60
+ repo.Transaction.Rollback()
61
+ return
62
+ } else {
63
+ repo.Transaction.SavePoint("Save Point : " + string(i))
64
+ repo.Transaction.Commit()
65
+ }
66
+ }
67
+ }
68
+ func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) {
69
+ repo.Transaction.Where(&repo.Constructor)
70
+ }
71
+ func Find[T1 any, T2 any](repo *Repository[T1, T2]) {
72
+ repo.Transaction.Find(&repo.Result)
73
+ }
74
+
75
+ func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) {
76
+ repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result)
77
+ }
78
+
79
+ func Create[T1 any](repo *Repository[T1, T1]) {
80
+ fmt.Println(repo.Constructor)
81
+ repo.Transaction.Create(&repo.Constructor)
82
+ repo.Result = repo.Constructor
83
+ }
84
+
85
+ func Update[T1 any](repo *Repository[T1, T1]) {
86
+ repo.Transaction.Save(&repo.Constructor)
87
+ repo.Result = repo.Constructor
88
+ }
89
+
90
+ func Delete[T1 any](repo *Repository[T1, T1]) {
91
+ repo.Transaction.Delete(&repo.Constructor)
92
+ }
93
+
94
+ func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) {
95
+ repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result)
96
+ }
router/router.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package router
2
+
3
+ import (
4
+ "github.com/gin-gonic/gin"
5
+ "go-dp.abdanhafidz.com/config"
6
+ "go-dp.abdanhafidz.com/controller"
7
+ )
8
+
9
+ func StartService() {
10
+ router := gin.Default()
11
+ routerGroup := router.Group("/api")
12
+ {
13
+ routerGroup.POST("/login", controller.LoginController)
14
+ routerGroup.POST("/register", controller.RegisterController)
15
+ }
16
+ router.Run(config.TCP_ADDRESS)
17
+ }
services/LoginService.go ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "go-dp.abdanhafidz.com/middleware"
5
+ "go-dp.abdanhafidz.com/models"
6
+ "go-dp.abdanhafidz.com/repositories"
7
+ )
8
+
9
+ type LoginConstructor struct {
10
+ Email string
11
+ Password string
12
+ }
13
+
14
+ type AuthenticationService struct {
15
+ Service[LoginConstructor, models.Account]
16
+ }
17
+
18
+ func (s *AuthenticationService) Authenticate() {
19
+ accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password)
20
+ if accountData.NoRecord {
21
+ s.Exception.DataNotFound = true
22
+ s.Exception.Message = "there is no account with given email!"
23
+ return
24
+ }
25
+
26
+ if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil {
27
+ s.Exception.Unauthorized = true
28
+ s.Exception.Message = "incorrect password!"
29
+ return
30
+ }
31
+
32
+ s.Result = accountData.Result
33
+ s.Error = accountData.RowsError
34
+ }
35
+
36
+ // LoginHandler handles user login
services/RegisterService.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import (
4
+ "errors"
5
+
6
+ "go-dp.abdanhafidz.com/middleware"
7
+ "go-dp.abdanhafidz.com/models"
8
+ "go-dp.abdanhafidz.com/repositories"
9
+ "gorm.io/gorm"
10
+ )
11
+
12
+ type RegisterService struct {
13
+ Service[models.Account, models.Account]
14
+ }
15
+
16
+ func (s *RegisterService) Create() {
17
+ hashed_password, _ := middleware.HashPassword(s.Constructor.Password)
18
+ s.Constructor.Password = hashed_password
19
+ accountCreated := repositories.CreateAccount(s.Constructor)
20
+ if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) {
21
+ s.Exception.DataDuplicate = true
22
+ s.Exception.Message = "There is account registered with given data!"
23
+ return
24
+ } else if errors.Is(accountCreated.RowsError, gorm.ErrModelAccessibleFieldsRequired) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidData) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidValue) || errors.Is(accountCreated.RowsError, gorm.ErrInvalidField) {
25
+ s.Exception.BadRequest = true
26
+ s.Exception.Message = "Bad request!"
27
+ return
28
+ }
29
+ s.Result = accountCreated.Result
30
+ s.Result.Password = "SECRET"
31
+ }
services/services.go ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package services
2
+
3
+ import "go-dp.abdanhafidz.com/models"
4
+
5
+ type (
6
+ Services interface {
7
+ Retrieve()
8
+ Update()
9
+ Create()
10
+ Delete()
11
+ Validate()
12
+ Authenticate()
13
+ Authorize()
14
+ }
15
+ Service[TConstructor any, TResult any] struct {
16
+ Constructor TConstructor
17
+ Result TResult
18
+ Exception models.Exception
19
+ Error error
20
+ }
21
+ )
22
+
23
+ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] {
24
+ if len(constructor) == 0 {
25
+ return &Service[TConstructor, TResult]{}
26
+ }
27
+
28
+ return &Service[TConstructor, TResult]{
29
+ Constructor: constructor[0],
30
+ }
31
+ }
space/.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
space/README.md ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Quzuu Api Dev
3
+ emoji: 🐠
4
+ colorFrom: indigo
5
+ colorTo: gray
6
+ sdk: docker
7
+ pinned: false
8
+ ---
utils/APIResponse.go ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package utils
2
+
3
+ import (
4
+ "net/http"
5
+ "reflect"
6
+
7
+ "github.com/gin-gonic/gin"
8
+ "go-dp.abdanhafidz.com/models"
9
+ "go-dp.abdanhafidz.com/services"
10
+ )
11
+
12
+ func ResponseOK(c *gin.Context, data any) {
13
+ c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"})
14
+ }
15
+
16
+ func ResponseFAIL(c *gin.Context, status int, exception models.Exception) {
17
+ message := exception.Message
18
+ exception.Message = ""
19
+ c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception})
20
+ return
21
+ }
22
+
23
+ func SendResponse(c *gin.Context, data services.Service[any, any]) {
24
+ if reflect.ValueOf(data.Exception).IsNil() {
25
+ ResponseOK(c, data)
26
+ } else {
27
+ if data.Exception.Unauthorized {
28
+ ResponseFAIL(c, 401, data.Exception)
29
+ } else if data.Exception.BadRequest {
30
+ ResponseFAIL(c, 400, data.Exception)
31
+ } else if data.Exception.DataNotFound {
32
+ ResponseFAIL(c, 404, data.Exception)
33
+ } else if data.Exception.InternalServerError {
34
+ ResponseFAIL(c, 500, data.Exception)
35
+ } else {
36
+ ResponseFAIL(c, 403, data.Exception)
37
+ }
38
+ }
39
+ }
utils/Exception.go ADDED
@@ -0,0 +1 @@
 
 
1
+ package utils
utils/Helper.go ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package utils
2
+
3
+ import (
4
+ "github.com/gin-gonic/gin"
5
+ "go-dp.abdanhafidz.com/models"
6
+ )
7
+
8
+ func GetAccount(c *gin.Context) models.AccountData {
9
+ cParam, _ := c.Get("accountData")
10
+ return cParam.(models.AccountData)
11
+ }
utils/Logger.go ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package utils
2
+
3
+ import (
4
+ "log"
5
+ "os"
6
+
7
+ "go-dp.abdanhafidz.com/config"
8
+ )
9
+
10
+ func LogError(errorLogged error) {
11
+ file, err := os.OpenFile(config.LOG_PATH+"error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
12
+ if err != nil {
13
+ log.Fatal(err)
14
+ }
15
+
16
+ log.SetOutput(file)
17
+
18
+ log.Println(errorLogged)
19
+ }
utils/utils.go ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ package utils
2
+
3
+ func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string {
4
+ if condition {
5
+ return valueIfTrue
6
+ } else {
7
+ return valueIfFalse
8
+ }
9
+ }