diff --git a/repositories/AccountRepository.go b/repositories/AccountRepository.go index b868ad4c0353135ae0659d8fa9db3ead4f334eb8..f191583a0de0f8ec9291985a06d40c00161522d0 100644 --- a/repositories/AccountRepository.go +++ b/repositories/AccountRepository.go @@ -4,9 +4,9 @@ import ( "go-dp.abdanhafidz.com/models" ) -func GetAccountbyEmailPassword(email string, password string) Repository[models.Account, models.Account] { +func GetAccountbyEmail(email string) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( - models.Account{Email: email, Password: password}, + models.Account{Email: email}, ) repo.Transactions( WhereGivenConstructor[models.Account, models.Account], diff --git a/repositories/repositories.go b/repositories/repositories.go index 7a5c2816396cbb729fc1e0df162e9d0eea6ea42b..9d34eda9880d655f8b4200d4792fff50fc144a54 100644 --- a/repositories/repositories.go +++ b/repositories/repositories.go @@ -1,8 +1,6 @@ package repositories import ( - "strconv" - "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -51,15 +49,10 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repo } } func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { - i := 1 for _, tx := range transactions { repo.Transaction = tx(repo) if repo.RowsError != nil { - repo.Transaction.Rollback() return - } else { - repo.Transaction.SavePoint("Save Point : " + strconv.Itoa(i)) - repo.Transaction.Commit() } } } diff --git a/services/LoginService.go b/services/LoginService.go index f1bdc1d654ff34ce16ece776c45919bd3841b38d..21778c5b70ddeded49df2a7069057abe28fb7c5d 100644 --- a/services/LoginService.go +++ b/services/LoginService.go @@ -18,7 +18,7 @@ type AuthenticationService struct { } func (s *AuthenticationService) Authenticate() { - accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) + accountData := repositories.GetAccountbyEmail(s.Constructor.Email) if accountData.NoRecord { s.Exception.DataNotFound = true s.Exception.Message = "there is no account with given credentials!" diff --git a/services/RegisterService.go b/services/RegisterService.go index 8809621fac0e901ca8c058868b6318d0ad9470ca..93acaba8cfa9364f620c1481dec0bf913e2bbfb5 100644 --- a/services/RegisterService.go +++ b/services/RegisterService.go @@ -3,6 +3,7 @@ package services import ( "errors" + uuid "github.com/satori/go.uuid" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" "go-dp.abdanhafidz.com/repositories" @@ -17,6 +18,7 @@ func (s *RegisterService) Create() { hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) s.Error = err_hash s.Constructor.Password = hashed_password + s.Constructor.UUID = uuid.NewV4() accountCreated := repositories.CreateAccount(s.Constructor) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true diff --git a/space/go.mod b/space/go.mod index efe450181fc1f3927c0b6658778233ce43ac1d76..f6d85920f3d50bf7a3fc013a4ee8875237b0e108 100644 --- a/space/go.mod +++ b/space/go.mod @@ -36,6 +36,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/satori/go.uuid v1.2.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect golang.org/x/arch v0.6.0 // indirect diff --git a/space/go.sum b/space/go.sum index 79ffaec719b95b12a37e3a787b1576ef5a53db03..a82e88494d1f86d0560792cace9c2a61284c1d8a 100644 --- a/space/go.sum +++ b/space/go.sum @@ -79,6 +79,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= +github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/space/models/DatabaseModel.go b/space/models/DatabaseModel.go index c8730cd31d769683f0900f12d40c1cf8ff038b8c..b2f38ee65f5094a7311928d1567e5263d87f9c7a 100644 --- a/space/models/DatabaseModel.go +++ b/space/models/DatabaseModel.go @@ -2,14 +2,18 @@ package models import ( "time" + + uuid "github.com/satori/go.uuid" ) type Account struct { - Id uint `gorm:"primaryKey" json:"id"` - Email string `gorm:"uniqueIndex" json:"email"` - Password string `json:"password"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + Id uint `gorm:"primaryKey" json:"id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + Email string `gorm:"uniqueIndex" json:"email"` + Password string `json:"password"` + IsEmailVerified bool `json:"is_email_verified"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` } type AccountDetails struct { @@ -22,6 +26,14 @@ type AccountDetails struct { DeletedAt time.Time `json:"deleted_at"` } +type EmailVerification struct { + Id int `gorm:"primaryKey" json:"id"` + AccountId int `json:"account_id"` + UUID uuid.UUID `gorm:"type:uuid" json:"uuid" ` + CreatedAt time.Time `json:"created_at"` + ExpiredAt time.Time `json:"expired_at"` +} + // Gorm table name settings func (Account) TableName() string { return "account" } func (AccountDetails) TableName() string { return "account_details" } diff --git a/space/repositories/AccountRepository.go b/space/repositories/AccountRepository.go index c6b942973c0f0ab0a5e4204a77bd8e7482def6e1..b868ad4c0353135ae0659d8fa9db3ead4f334eb8 100644 --- a/space/repositories/AccountRepository.go +++ b/space/repositories/AccountRepository.go @@ -1,8 +1,6 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/models" ) @@ -10,7 +8,6 @@ func GetAccountbyEmailPassword(email string, password string) Repository[models. repo := Construct[models.Account, models.Account]( models.Account{Email: email, Password: password}, ) - fmt.Println(repo.Constructor) repo.Transactions( WhereGivenConstructor[models.Account, models.Account], Find[models.Account, models.Account], diff --git a/space/services/RegisterService.go b/space/services/RegisterService.go index 054093a50f74a7c8530f498afacc8ca18de63315..8809621fac0e901ca8c058868b6318d0ad9470ca 100644 --- a/space/services/RegisterService.go +++ b/space/services/RegisterService.go @@ -14,7 +14,8 @@ type RegisterService struct { } func (s *RegisterService) Create() { - hashed_password, _ := middleware.HashPassword(s.Constructor.Password) + hashed_password, err_hash := middleware.HashPassword(s.Constructor.Password) + s.Error = err_hash s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { @@ -26,7 +27,7 @@ func (s *RegisterService) Create() { s.Exception.Message = "Bad request!" return } - + s.Error = accountCreated.RowsError s.Result = accountCreated.Result s.Result.Password = "SECRET" } diff --git a/space/space/controller/LoginController.go b/space/space/controller/LoginController.go index 8102aa46984caff0eda1d09563bdb9544c377821..6d04567f7a776399c5faafc4f11dff4c0735ae9a 100644 --- a/space/space/controller/LoginController.go +++ b/space/space/controller/LoginController.go @@ -11,9 +11,9 @@ func LoginController(c *gin.Context) { loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ Service: &authentication.Service, } - loginController.RequestJSON(c) - loginController.Service.Constructor.Email = loginController.Request.Email - loginController.Service.Constructor.Password = loginController.Request.Password - authentication.Authenticate() - loginController.Response(c) + loginController.RequestJSON(c, func() { + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + }) } diff --git a/space/space/controller/RegisterController.go b/space/space/controller/RegisterController.go index 25244306baf7b5ff373c0e1c925c584740710eee..e3375ba58212ddd73d2d4e29fbf67d247e89398b 100644 --- a/space/space/controller/RegisterController.go +++ b/space/space/controller/RegisterController.go @@ -11,9 +11,9 @@ func RegisterController(c *gin.Context) { registerController := Controller[models.RegisterRequest, models.Account, models.Account]{ Service: ®ister.Service, } - registerController.RequestJSON(c) - registerController.Service.Constructor.Password = registerController.Request.Password - registerController.Service.Constructor.Email = registerController.Request.Email - register.Create() - registerController.Response(c) + registerController.RequestJSON(c, func() { + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + }) } diff --git a/space/space/controller/controller.go b/space/space/controller/controller.go index 45298c05462bb0b95532d7c35bde0e27fe76d500..fa14d0b2c6ed4e25a72839a3fad18b2d59e40af3 100644 --- a/space/space/controller/controller.go +++ b/space/space/controller/controller.go @@ -19,7 +19,7 @@ type ( } ) -func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { +func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context, act func()) { cParam, _ := c.Get("accountData") if cParam != nil { controller.AccountData = cParam.(models.AccountData) @@ -28,9 +28,12 @@ func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { if errBinding != nil { utils.ResponseFAIL(c, 400, models.Exception{ BadRequest: true, - Message: "Invalid Request Type", + Message: "Invalid Request!, recheck your request, there's must be some problem about required parameter or type parameter", }) return + } else { + act() + controller.Response(c) } } func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { diff --git a/space/space/models/DatabaseModel.go b/space/space/models/DatabaseModel.go index 355323a487ca5c8fc593a1bdea525f30081b884d..c8730cd31d769683f0900f12d40c1cf8ff038b8c 100644 --- a/space/space/models/DatabaseModel.go +++ b/space/space/models/DatabaseModel.go @@ -6,7 +6,7 @@ import ( type Account struct { Id uint `gorm:"primaryKey" json:"id"` - Email string `gorm:"primaryKey" json:"email"` + Email string `gorm:"uniqueIndex" json:"email"` Password string `json:"password"` CreatedAt time.Time `json:"created_at"` DeletedAt time.Time `json:"deleted_at"` diff --git a/space/space/repositories/AccountRepository.go b/space/space/repositories/AccountRepository.go index 9243b47af1fc1184b20eae52a1300010764f5bab..c6b942973c0f0ab0a5e4204a77bd8e7482def6e1 100644 --- a/space/space/repositories/AccountRepository.go +++ b/space/space/repositories/AccountRepository.go @@ -1,6 +1,8 @@ package repositories import ( + "fmt" + "go-dp.abdanhafidz.com/models" ) @@ -8,7 +10,11 @@ func GetAccountbyEmailPassword(email string, password string) Repository[models. repo := Construct[models.Account, models.Account]( models.Account{Email: email, Password: password}, ) - Find(repo) + fmt.Println(repo.Constructor) + repo.Transactions( + WhereGivenConstructor[models.Account, models.Account], + Find[models.Account, models.Account], + ) return *repo } @@ -16,7 +22,6 @@ func CreateAccount(account models.Account) Repository[models.Account, models.Acc repo := Construct[models.Account, models.Account]( account, ) - Create(repo) return *repo } diff --git a/space/space/repositories/repositories.go b/space/space/repositories/repositories.go index 767e3d93bb01248012ed0a89768db740be5157c9..7a5c2816396cbb729fc1e0df162e9d0eea6ea42b 100644 --- a/space/space/repositories/repositories.go +++ b/space/space/repositories/repositories.go @@ -1,6 +1,8 @@ package repositories import ( + "strconv" + "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -56,52 +58,63 @@ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1 repo.Transaction.Rollback() return } else { - repo.Transaction.SavePoint("Save Point : " + string(i)) + repo.Transaction.SavePoint("Save Point : " + strconv.Itoa(i)) repo.Transaction.Commit() } } } func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Where(&repo.Constructor) - repo.RowsError = repo.Transaction.Error + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Find(&repo.Result) - repo.RowsError = repo.Transaction.Error + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Create(&repo.Constructor) - repo.Result = repo.Constructor + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error + repo.Result = repo.Constructor return tx } func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Save(&repo.Constructor) - repo.Result = repo.Constructor + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB { tx := repo.Transaction.Delete(&repo.Constructor) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) + repo.RowsCount = int(tx.RowsAffected) + repo.NoRecord = repo.RowsCount == 0 repo.RowsError = tx.Error return tx } diff --git a/space/space/services/LoginService.go b/space/space/services/LoginService.go index 2d734758bcd0ea31246376da2e71187be98d81ca..f1bdc1d654ff34ce16ece776c45919bd3841b38d 100644 --- a/space/space/services/LoginService.go +++ b/space/space/services/LoginService.go @@ -21,10 +21,9 @@ func (s *AuthenticationService) Authenticate() { accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) if accountData.NoRecord { s.Exception.DataNotFound = true - s.Exception.Message = "there is no account with given email!" + s.Exception.Message = "there is no account with given credentials!" return } - if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { s.Exception.Unauthorized = true s.Exception.Message = "incorrect password!" diff --git a/space/space/services/RegisterService.go b/space/space/services/RegisterService.go index 0aca7d7de67105b79deaf19685ec9414df3f5d45..054093a50f74a7c8530f498afacc8ca18de63315 100644 --- a/space/space/services/RegisterService.go +++ b/space/space/services/RegisterService.go @@ -2,7 +2,6 @@ package services import ( "errors" - "fmt" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" @@ -18,9 +17,6 @@ func (s *RegisterService) Create() { hashed_password, _ := middleware.HashPassword(s.Constructor.Password) s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) - - // fmt.Println("Error :", accountCreated.RowsError.Error()) - fmt.Println(errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey)) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!" diff --git a/space/space/space/controller/LoginController.go b/space/space/space/controller/LoginController.go index 78d94dcb74fe945b45f4491f85d9b3784a6947a1..8102aa46984caff0eda1d09563bdb9544c377821 100644 --- a/space/space/space/controller/LoginController.go +++ b/space/space/space/controller/LoginController.go @@ -8,7 +8,7 @@ import ( func LoginController(c *gin.Context) { authentication := services.AuthenticationService{} - loginController := Controller[models.LoginRequest, services.LoginConstructor, models.Account]{ + loginController := Controller[models.LoginRequest, services.LoginConstructor, models.AuthenticatedUser]{ Service: &authentication.Service, } loginController.RequestJSON(c) diff --git a/space/space/space/controller/controller.go b/space/space/space/controller/controller.go index aba28ae8d9d64b8bb62cda08de9658163b038602..45298c05462bb0b95532d7c35bde0e27fe76d500 100644 --- a/space/space/space/controller/controller.go +++ b/space/space/space/controller/controller.go @@ -41,6 +41,8 @@ func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { Message: "Internal Server Error", }) utils.LogError(controller.Service.Error) + case controller.Service.Exception.DataDuplicate: + utils.ResponseFAIL(c, 400, controller.Service.Exception) case controller.Service.Exception.Unauthorized: utils.ResponseFAIL(c, 401, controller.Service.Exception) case controller.Service.Exception.DataNotFound: diff --git a/space/space/space/middleware/AuthMiddleware.go b/space/space/space/middleware/AuthMiddleware.go index 6dc04aa18ba75ef21252e87baf23c0158cc3daea..c153979ac01f108193541d23f821c288b4752f01 100644 --- a/space/space/space/middleware/AuthMiddleware.go +++ b/space/space/space/middleware/AuthMiddleware.go @@ -25,7 +25,7 @@ func GenerateToken(user *models.Account) (string, error) { // Set claims claims := token.Claims.(jwt.MapClaims) - claims["id"] = user.IDAccount + claims["id"] = user.Id claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours // Sign the token with the secret key diff --git a/space/space/space/models/DatabaseModel.go b/space/space/space/models/DatabaseModel.go index f048c17a9838438555d6fd795e6c7617c2c1b729..355323a487ca5c8fc593a1bdea525f30081b884d 100644 --- a/space/space/space/models/DatabaseModel.go +++ b/space/space/space/models/DatabaseModel.go @@ -5,14 +5,11 @@ import ( ) type Account struct { - IDAccount uint `gorm:"primaryKey" json:"id_account"` - Name string `json:"name"` - Username string `json:"username"` - Email string `json:"email"` - Password string `json:"password"` - PhoneNumber int `json:"phone_number"` - CreatedAt time.Time `json:"created_at"` - DeletedAt time.Time `json:"deleted_at"` + Id uint `gorm:"primaryKey" json:"id"` + Email string `gorm:"primaryKey" json:"email"` + Password string `json:"password"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` } type AccountDetails struct { diff --git a/space/space/space/models/ResponseModel.go b/space/space/space/models/ResponseModel.go index 9ce401186cb92d50737155815c1f511164b86407..cd7b65b6a4ffd53ca1cc3c430abf0b256d0a4936 100644 --- a/space/space/space/models/ResponseModel.go +++ b/space/space/space/models/ResponseModel.go @@ -1 +1,6 @@ package models + +type AuthenticatedUser struct { + Account Account `json:"account"` + Token string `json:"token"` +} diff --git a/space/space/space/repositories/AccountRepository.go b/space/space/space/repositories/AccountRepository.go index 79e43f0707a436c344e5cddd53187d428274bde9..9243b47af1fc1184b20eae52a1300010764f5bab 100644 --- a/space/space/space/repositories/AccountRepository.go +++ b/space/space/space/repositories/AccountRepository.go @@ -1,24 +1,22 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/models" ) -func GetAccountbyEmailPassword(username string, password string) Repository[models.Account, models.Account] { +func GetAccountbyEmailPassword(email string, password string) Repository[models.Account, models.Account] { repo := Construct[models.Account, models.Account]( - models.Account{Username: username, Password: password}, + models.Account{Email: email, Password: password}, ) Find(repo) return *repo } func CreateAccount(account models.Account) Repository[models.Account, models.Account] { - fmt.Println(account) repo := Construct[models.Account, models.Account]( account, ) + Create(repo) return *repo } diff --git a/space/space/space/repositories/repositories.go b/space/space/space/repositories/repositories.go index c82f550472331b9b8d0f5d4cdcf5aab441e5cdfa..767e3d93bb01248012ed0a89768db740be5157c9 100644 --- a/space/space/space/repositories/repositories.go +++ b/space/space/space/repositories/repositories.go @@ -1,8 +1,6 @@ package repositories import ( - "fmt" - "go-dp.abdanhafidz.com/config" "gorm.io/gorm" ) @@ -39,7 +37,6 @@ type Repository[TConstructor any, TResult any] struct { } func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] { - fmt.Println("Len = ", len(constructor)) if len(constructor) == 1 { return &Repository[TConstructor, TResult]{ Constructor: constructor[0], @@ -51,11 +48,10 @@ func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repo Transaction: config.DB.Begin(), } } -func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2])) { +func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2]) *gorm.DB) { i := 1 for _, tx := range transactions { - tx(repo) - repo.RowsError = repo.Transaction.Error + repo.Transaction = tx(repo) if repo.RowsError != nil { repo.Transaction.Rollback() return @@ -65,32 +61,47 @@ func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1 } } } -func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Where(&repo.Constructor) +func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Where(&repo.Constructor) + repo.RowsError = repo.Transaction.Error + repo.RowsError = tx.Error + return tx } -func Find[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Find(&repo.Result) +func Find[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Find(&repo.Result) + repo.RowsError = repo.Transaction.Error + repo.RowsError = tx.Error + return tx } -func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) +func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) + repo.RowsError = tx.Error + return tx } -func Create[T1 any](repo *Repository[T1, T1]) { - fmt.Println(repo.Constructor) - repo.Transaction.Create(&repo.Constructor) +func Create[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Create(&repo.Constructor) repo.Result = repo.Constructor + repo.RowsError = tx.Error + return tx } -func Update[T1 any](repo *Repository[T1, T1]) { - repo.Transaction.Save(&repo.Constructor) +func Update[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Save(&repo.Constructor) repo.Result = repo.Constructor + repo.RowsError = tx.Error + return tx } -func Delete[T1 any](repo *Repository[T1, T1]) { - repo.Transaction.Delete(&repo.Constructor) +func Delete[T1 any](repo *Repository[T1, T1]) *gorm.DB { + tx := repo.Transaction.Delete(&repo.Constructor) + repo.RowsError = tx.Error + return tx } -func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) { - repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) +func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) *gorm.DB { + tx := repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) + repo.RowsError = tx.Error + return tx } diff --git a/space/space/space/services/LoginService.go b/space/space/space/services/LoginService.go index e798298cb58036f0b00cc04a919500d6b9c83c09..2d734758bcd0ea31246376da2e71187be98d81ca 100644 --- a/space/space/space/services/LoginService.go +++ b/space/space/space/services/LoginService.go @@ -1,6 +1,8 @@ package services import ( + "errors" + "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" "go-dp.abdanhafidz.com/repositories" @@ -12,7 +14,7 @@ type LoginConstructor struct { } type AuthenticationService struct { - Service[LoginConstructor, models.Account] + Service[LoginConstructor, models.AuthenticatedUser] } func (s *AuthenticationService) Authenticate() { @@ -29,7 +31,17 @@ func (s *AuthenticationService) Authenticate() { return } - s.Result = accountData.Result + token, err_tok := middleware.GenerateToken(&accountData.Result) + + if err_tok != nil { + s.Error = errors.Join(s.Error, err_tok) + } + + accountData.Result.Password = "SECRET" + s.Result = models.AuthenticatedUser{ + Account: accountData.Result, + Token: token, + } s.Error = accountData.RowsError } diff --git a/space/space/space/services/RegisterService.go b/space/space/space/services/RegisterService.go index 615a3a595a056802ecd6e130c8f0f04be2643d05..0aca7d7de67105b79deaf19685ec9414df3f5d45 100644 --- a/space/space/space/services/RegisterService.go +++ b/space/space/space/services/RegisterService.go @@ -2,6 +2,7 @@ package services import ( "errors" + "fmt" "go-dp.abdanhafidz.com/middleware" "go-dp.abdanhafidz.com/models" @@ -17,15 +18,19 @@ func (s *RegisterService) Create() { hashed_password, _ := middleware.HashPassword(s.Constructor.Password) s.Constructor.Password = hashed_password accountCreated := repositories.CreateAccount(s.Constructor) + + // fmt.Println("Error :", accountCreated.RowsError.Error()) + fmt.Println(errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey)) if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { s.Exception.DataDuplicate = true - s.Exception.Message = "There is account registered with given data!" + s.Exception.Message = "Account with email " + s.Constructor.Email + " already exists!" return } 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) { s.Exception.BadRequest = true s.Exception.Message = "Bad request!" return } + s.Result = accountCreated.Result s.Result.Password = "SECRET" } diff --git a/space/space/space/space/space/Dockerfile b/space/space/space/space/space/Dockerfile index 7d4a695141f39c703c8e6188f6a2b6eba686d624..6183c94745bf456bad5ea308482bd6c3602ad543 100644 --- a/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/Dockerfile @@ -21,8 +21,8 @@ RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ echo "DB_NAME=postgres" >> .env && \ echo "HOST_ADDRESS = 0.0.0.0" >> .env && \ echo "HOST_PORT = 7860" >> .env && \ - echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env - echo "LOG_PATH = logs" + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ + echo "LOG_PATH = logs" >> .env # Build aplikasi RUN go build -o main . diff --git a/space/space/space/space/space/space/controller/HomeController.go b/space/space/space/space/space/space/controller/HomeController.go index bb3aae6e57284e4a46908560193aa2a32228d7e2..06cfc3d7fac0385d6aef847a186de2eae7dbb4bb 100644 --- a/space/space/space/space/space/space/controller/HomeController.go +++ b/space/space/space/space/space/space/controller/HomeController.go @@ -4,6 +4,6 @@ import "github.com/gin-gonic/gin" func HomeController(c *gin.Context) { c.JSON(200, gin.H{ - "message": "Api Quzuu Backend 2024!", + "message": "Api Qobiltu 2025!", }) } diff --git a/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/router/router.go index a225f4fa148e7c8b5551686ace69dcd3c1b17562..27ce310f782a252cab580d08873505f02ec38a03 100644 --- a/space/space/space/space/space/space/router/router.go +++ b/space/space/space/space/space/space/router/router.go @@ -8,8 +8,9 @@ import ( func StartService() { router := gin.Default() - routerGroup := router.Group("/api") + routerGroup := router.Group("/api/v1") { + routerGroup.GET("/", controller.HomeController) routerGroup.POST("/login", controller.LoginController) routerGroup.POST("/register", controller.RegisterController) } diff --git a/space/space/space/space/space/space/space/.env.example b/space/space/space/space/space/space/space/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..7a13c9d5665f68dc7087f208910958eb620cc5c6 --- /dev/null +++ b/space/space/space/space/space/space/space/.env.example @@ -0,0 +1,9 @@ +DB_HOST = +DB_USER = +DB_PASSWORD = +DB_PORT = +DB_NAME = +SALT = +HOST_ADDRESS = +HOST_PORT = +LOG_PATH = logs \ No newline at end of file diff --git a/space/space/space/space/space/space/space/.github/workflows/main.yml b/space/space/space/space/space/space/space/.github/workflows/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..39a67571ac3a2e6a6e2a972b9550becd551bdded --- /dev/null +++ b/space/space/space/space/space/space/space/.github/workflows/main.yml @@ -0,0 +1,52 @@ +name: Deploy to Development via Huggingface + +on: + push: + branches: + - main + +jobs: + deploy-to-huggingface: + runs-on: ubuntu-latest + + steps: + # Checkout repository + - name: Checkout Repository + uses: actions/checkout@v3 + + # Setup Git + - name: Setup Git for Huggingface + run: | + git config --global user.email "abdan.hafidz@gmail.com" + git config --global user.name "abdanhafidz" + + # Clone Huggingface Space Repository + - name: Clone Huggingface Space + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + git clone https://huggingface.co/spaces/lifedebugger/api-qobiltu-dev space + + # Update Git Remote URL and Pull Latest Changes + - name: Update Remote and Pull Changes + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git remote set-url origin https://lifedebugger:$HF_TOKEN@huggingface.co/spaces/lifedebugger/api-qobiltu-dev + git pull origin main || echo "No changes to pull" + + # Copy Files to Huggingface Space + - name: Copy Files to Space + run: | + rsync -av --exclude='.git' ./ space/ + + # Commit and Push to Huggingface Space + - name: Commit and Push to Huggingface + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + cd space + git add . + git commit -m "Deploy files from GitHub repository" || echo "No changes to commit" + git push origin main || echo "No changes to push" diff --git a/space/space/space/space/space/space/space/.gitignore b/space/space/space/space/space/space/space/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..deeb9479f4cb982ccd42fa0d7210b9777509e4ae --- /dev/null +++ b/space/space/space/space/space/space/space/.gitignore @@ -0,0 +1,3 @@ +.env +vendor/ +quzuu-be.exe \ No newline at end of file diff --git a/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7d4a695141f39c703c8e6188f6a2b6eba686d624 --- /dev/null +++ b/space/space/space/space/space/space/space/Dockerfile @@ -0,0 +1,30 @@ +# Gunakan image dasar Golang versi 1.21.6 +FROM golang:1.21.6 + +# Set working directory +WORKDIR /app + +# Copy go.mod dan go.sum +COPY go.mod go.sum ./ + +# Download dependencies +RUN go mod download + +# Copy seluruh kode +COPY . . + +# Buat file .env dengan variabel environment yang dibutuhkan +RUN echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ + echo "DB_USER=postgres.rdscploxoikqsevhduii" >> .env && \ + echo "DB_PASSWORD=Qobiltu12233334444" >> .env && \ + echo "DB_PORT=5432" >> .env && \ + echo "DB_NAME=postgres" >> .env && \ + echo "HOST_ADDRESS = 0.0.0.0" >> .env && \ + echo "HOST_PORT = 7860" >> .env && \ + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env + echo "LOG_PATH = logs" +# Build aplikasi +RUN go build -o main . + +# Jalankan aplikasi +CMD ["./main"] diff --git a/space/space/space/space/space/space/space/config/DatabaseConfig.go b/space/space/space/space/space/space/space/config/DatabaseConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..7dfa116ec7152e6338c764ce5853ad21782bd26f --- /dev/null +++ b/space/space/space/space/space/space/space/config/DatabaseConfig.go @@ -0,0 +1,60 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "github.com/joho/godotenv" + "go-dp.abdanhafidz.com/models" +) + +func AutoMigrateAll(db *gorm.DB) { + // Enable logger to see SQL logs + db.Logger.LogMode(logger.Info) + + // Auto-migrate all models + err := db.AutoMigrate( + &models.Account{}, + &models.AccountDetails{}, + ) + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} + +var DB *gorm.DB +var err error +var Salt string + +func init() { + godotenv.Load() + if err != nil { + fmt.Println("Gagal membaca file .env") + return + } + os.Setenv("TZ", "Asia/Jakarta") + dbHost := os.Getenv("DB_HOST") + dbPort := os.Getenv("DB_PORT") + dbUser := os.Getenv("DB_USER") + dbPassword := os.Getenv("DB_PASSWORD") + dbName := os.Getenv("DB_NAME") + Salt := os.Getenv("SALT") + dsn := "host=" + dbHost + " user=" + dbUser + " password=" + dbPassword + " dbname=" + dbName + " port=" + dbPort + " sslmode=disable TimeZone=Asia/Jakarta" + DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{TranslateError: true}) + if err != nil { + panic(err) + } + if Salt == "" { + Salt = "D3f4u|t" + } + + // Call AutoMigrateAll to perform auto-migration + AutoMigrateAll(DB) +} diff --git a/space/space/space/space/space/space/space/config/EnvConfig.go b/space/space/space/space/space/space/space/config/EnvConfig.go new file mode 100644 index 0000000000000000000000000000000000000000..ae4a686c65b851a6e8d960b47a77e4c67470c615 --- /dev/null +++ b/space/space/space/space/space/space/space/config/EnvConfig.go @@ -0,0 +1,16 @@ +package config + +import "os" + +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string + +func init() { + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + // Menampilkan nilai variabel lingkungan +} diff --git a/space/space/space/space/space/space/space/controller/HomeController.go b/space/space/space/space/space/space/space/controller/HomeController.go new file mode 100644 index 0000000000000000000000000000000000000000..bb3aae6e57284e4a46908560193aa2a32228d7e2 --- /dev/null +++ b/space/space/space/space/space/space/space/controller/HomeController.go @@ -0,0 +1,9 @@ +package controller + +import "github.com/gin-gonic/gin" + +func HomeController(c *gin.Context) { + c.JSON(200, gin.H{ + "message": "Api Quzuu Backend 2024!", + }) +} diff --git a/space/space/space/space/space/space/space/controller/LoginController.go b/space/space/space/space/space/space/space/controller/LoginController.go new file mode 100644 index 0000000000000000000000000000000000000000..78d94dcb74fe945b45f4491f85d9b3784a6947a1 --- /dev/null +++ b/space/space/space/space/space/space/space/controller/LoginController.go @@ -0,0 +1,19 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func LoginController(c *gin.Context) { + authentication := services.AuthenticationService{} + loginController := Controller[models.LoginRequest, services.LoginConstructor, models.Account]{ + Service: &authentication.Service, + } + loginController.RequestJSON(c) + loginController.Service.Constructor.Email = loginController.Request.Email + loginController.Service.Constructor.Password = loginController.Request.Password + authentication.Authenticate() + loginController.Response(c) +} diff --git a/space/space/space/space/space/space/space/controller/RegisterController.go b/space/space/space/space/space/space/space/controller/RegisterController.go new file mode 100644 index 0000000000000000000000000000000000000000..25244306baf7b5ff373c0e1c925c584740710eee --- /dev/null +++ b/space/space/space/space/space/space/space/controller/RegisterController.go @@ -0,0 +1,19 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func RegisterController(c *gin.Context) { + register := services.RegisterService{} + registerController := Controller[models.RegisterRequest, models.Account, models.Account]{ + Service: ®ister.Service, + } + registerController.RequestJSON(c) + registerController.Service.Constructor.Password = registerController.Request.Password + registerController.Service.Constructor.Email = registerController.Request.Email + register.Create() + registerController.Response(c) +} diff --git a/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/controller/controller.go new file mode 100644 index 0000000000000000000000000000000000000000..aba28ae8d9d64b8bb62cda08de9658163b038602 --- /dev/null +++ b/space/space/space/space/space/space/space/controller/controller.go @@ -0,0 +1,53 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" + "go-dp.abdanhafidz.com/utils" +) + +type ( + Controllers interface { + RequestJSON(c *gin.Context) + Response(c *gin.Context) + } + Controller[T1 any, T2 any, T3 any] struct { + AccountData models.AccountData + Request T1 + Service *services.Service[T2, T3] + } +) + +func (controller *Controller[T1, T2, T3]) RequestJSON(c *gin.Context) { + cParam, _ := c.Get("accountData") + if cParam != nil { + controller.AccountData = cParam.(models.AccountData) + } + errBinding := c.ShouldBindJSON(&controller.Request) + if errBinding != nil { + utils.ResponseFAIL(c, 400, models.Exception{ + BadRequest: true, + Message: "Invalid Request Type", + }) + return + } +} +func (controller *Controller[T1, T2, T3]) Response(c *gin.Context) { + switch { + case controller.Service.Error != nil: + utils.ResponseFAIL(c, 500, models.Exception{ + InternalServerError: true, + Message: "Internal Server Error", + }) + utils.LogError(controller.Service.Error) + case controller.Service.Exception.Unauthorized: + utils.ResponseFAIL(c, 401, controller.Service.Exception) + case controller.Service.Exception.DataNotFound: + utils.ResponseFAIL(c, 404, controller.Service.Exception) + case controller.Service.Exception.Message != "": + utils.ResponseFAIL(c, 400, controller.Service.Exception) + default: + utils.ResponseOK(c, controller.Service.Result) + } +} diff --git a/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..efe450181fc1f3927c0b6658778233ce43ac1d76 --- /dev/null +++ b/space/space/space/space/space/space/space/go.mod @@ -0,0 +1,48 @@ +module go-dp.abdanhafidz.com + +go 1.21.0 + +require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/gin-gonic/gin v1.9.1 + github.com/joho/godotenv v1.5.1 + golang.org/x/crypto v0.32.0 + gorm.io/driver/postgres v1.5.4 + gorm.io/gorm v1.25.5 +) + +require ( + github.com/bytedance/sonic v1.10.2 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.25.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.5.0 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.1.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/arch v0.6.0 // indirect + golang.org/x/net v0.34.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/sys v0.29.0 // indirect + golang.org/x/text v0.21.0 // indirect + google.golang.org/protobuf v1.31.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..79ffaec719b95b12a37e3a787b1576ef5a53db03 --- /dev/null +++ b/space/space/space/space/space/space/space/go.sum @@ -0,0 +1,138 @@ +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= +github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0= +github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= +github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= +github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= +github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= +github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0nmsZJxEAnFLNO8= +github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= +github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= +github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= +github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= +golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA= +golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg= +golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= +golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0= +golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= +golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo= +gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/space/space/space/space/space/space/space/logs/error_log.txt b/space/space/space/space/space/space/space/logs/error_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/logs/security_log.txt b/space/space/space/space/space/space/space/logs/security_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/main.go b/space/space/space/space/space/space/space/main.go new file mode 100644 index 0000000000000000000000000000000000000000..d89532891b559d7d8361e79a1090e77a31cafb95 --- /dev/null +++ b/space/space/space/space/space/space/space/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/router" +) + +func main() { + fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT) + router.StartService() + +} diff --git a/space/space/space/space/space/space/space/middleware/AuthMiddleware.go b/space/space/space/space/space/space/space/middleware/AuthMiddleware.go new file mode 100644 index 0000000000000000000000000000000000000000..6dc04aa18ba75ef21252e87baf23c0158cc3daea --- /dev/null +++ b/space/space/space/space/space/space/space/middleware/AuthMiddleware.go @@ -0,0 +1,103 @@ +// auth/auth.go + +package middleware + +import ( + "errors" + "time" + + "github.com/dgrijalva/jwt-go" + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/models" + "golang.org/x/crypto/bcrypt" +) + +// Define a secret key for signing the JWT token +var salt = config.Salt +var secretKey = []byte(salt) + +// GenerateToken generates a JWT token for the given user +func GenerateToken(user *models.Account) (string, error) { + + // Create a new token + token := jwt.New(jwt.SigningMethodHS256) + + // Set claims + claims := token.Claims.(jwt.MapClaims) + claims["id"] = user.IDAccount + claims["exp"] = time.Now().Add(time.Hour * 24).Unix() // Token expires in 24 hours + + // Sign the token with the secret key + tokenString, err := token.SignedString(secretKey) + if err != nil { + return "", err + } + + return tokenString, nil +} + +// VerifyPassword verifies if the provided password matches the hashed password +func VerifyPassword(hashedPassword, password string) error { + err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) + if err != nil { + return errors.New("invalid password") + } + return nil +} +func HashPassword(password string) (string, error) { + bytes, err := bcrypt.GenerateFromPassword([]byte(password), 14) + return string(bytes), err +} + +type CustomClaims struct { + jwt.StandardClaims + IDUser int `json:"id"` +} + +func VerifyToken(bearer_token string) (int, string, error) { + // fmt.Println(bearer_token) + token, err := jwt.ParseWithClaims(bearer_token, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) + if err != nil { + return 0, "invalid-token", err + } + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } else if claims.StandardClaims.ExpiresAt != 0 && claims.ExpiresAt < time.Now().Unix() { + return 0, "expired", err + } else if !ok && token.Valid { + return 0, "invalid-token", err + } + + return claims.IDUser, "valid", err +} + +func AuthUser(c *gin.Context) { + var currAccData models.AccountData + if c.Request.Header["Auth-Bearer-Token"] != nil { + token := c.Request.Header["Auth-Bearer-Token"] + currAccData.IdUser, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.verifyStatus) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { + currAccData.IdUser = 0 + message := "Your session is expired, Please re-Login!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + } else { + currAccData.IdUser = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + message := "You have to Login First!" + SendJSON401(c, &currAccData.VerifyStatus, &message) + c.Abort() + return + } + + c.Set("accountData", currAccData) + c.Next() +} diff --git a/space/space/space/space/space/space/space/middleware/middleware.go b/space/space/space/space/space/space/space/middleware/middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..5b140903a637aa1d80f02382cb8dbb821c5c01bf --- /dev/null +++ b/space/space/space/space/space/space/space/middleware/middleware.go @@ -0,0 +1,31 @@ +package middleware + +import ( + "math" + "time" + + "gorm.io/gorm" +) + +func RecordCheck(rows *gorm.DB) (string, error) { + count := rows.RowsAffected + err := rows.Error + // fmt.Println(rows) + // fmt.Println(count) + if count == 0 { + return "no-record", err + } else if err != nil { + return "query-error", err + } else { + return "ok", err + } +} + +func DiffTime(t1 time.Time, t2 time.Time) (int, int, int) { + hs := t1.Sub(t2).Hours() + hs, mf := math.Modf(hs) + ms := mf * 60 + ms, sf := math.Modf(ms) + ss := sf * 60 + return int(hs), int(ms), int(ss) +} diff --git a/space/space/space/space/space/space/space/middleware/responseMiddleware.go b/space/space/space/space/space/space/space/middleware/responseMiddleware.go new file mode 100644 index 0000000000000000000000000000000000000000..55f3385077839115eadb8dace294229f4b4c3df2 --- /dev/null +++ b/space/space/space/space/space/space/space/middleware/responseMiddleware.go @@ -0,0 +1,39 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +// SendJSON200 sends a JSON response with HTTP status code 200 +func SendJSON200(c *gin.Context, data interface{}) { + c.JSON(http.StatusOK, gin.H{"status": "success", "data": data}) +} + +// SendJSON400 sends a JSON response with HTTP status code 400 +func SendJSON400(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusBadRequest, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON401 sends a JSON response with HTTP status code 401 +func SendJSON401(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// SendJSON403 sends a JSON response with HTTP status code 403 +func SendJSON403(c *gin.Context, message *string) { + c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": message}) +} + +// SendJSON404 sends a JSON response with HTTP status code 404 +func SendJSON404(c *gin.Context, message *string) { + c.JSON(http.StatusNotFound, gin.H{"status": "error", "message": message}) +} + +// SendJSON500 sends a JSON response with HTTP status code 500 +func SendJSON500(c *gin.Context, error_status *string, message *string) { + c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "error-status": error_status, "message": message}) +} + +// JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/space/space/space/space/models/AuthModel.go b/space/space/space/space/space/space/space/models/AuthModel.go new file mode 100644 index 0000000000000000000000000000000000000000..5f7c52925aee7c529ef737ae0feb88ee3cb3dbc7 --- /dev/null +++ b/space/space/space/space/space/space/space/models/AuthModel.go @@ -0,0 +1,7 @@ +package models + +type AccountData struct { + IdUser int + VerifyStatus string + ErrVerif error +} diff --git a/space/space/space/space/space/space/space/models/DatabaseModel.go b/space/space/space/space/space/space/space/models/DatabaseModel.go new file mode 100644 index 0000000000000000000000000000000000000000..f048c17a9838438555d6fd795e6c7617c2c1b729 --- /dev/null +++ b/space/space/space/space/space/space/space/models/DatabaseModel.go @@ -0,0 +1,30 @@ +package models + +import ( + "time" +) + +type Account struct { + IDAccount uint `gorm:"primaryKey" json:"id_account"` + Name string `json:"name"` + Username string `json:"username"` + Email string `json:"email"` + Password string `json:"password"` + PhoneNumber int `json:"phone_number"` + CreatedAt time.Time `json:"created_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +type AccountDetails struct { + IDDetail uint `gorm:"primaryKey" json:"id_detail"` + IDAccount uint `json:"id_account"` + Province string `json:"province"` + City string `json:"city"` + Institution string `json:"institution"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt time.Time `json:"deleted_at"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (AccountDetails) TableName() string { return "account_details" } diff --git a/space/space/space/space/space/space/space/models/ExceptionModel.go b/space/space/space/space/space/space/space/models/ExceptionModel.go new file mode 100644 index 0000000000000000000000000000000000000000..b4e76203056e9c2ee6ea5b447b6d3b90842f1d88 --- /dev/null +++ b/space/space/space/space/space/space/space/models/ExceptionModel.go @@ -0,0 +1,11 @@ +package models + +type Exception struct { + Unauthorized bool `json:"unauthorized,omitempty"` + BadRequest bool `json:"bad_request,omitempty"` + DataNotFound bool `json:"data_not_found,omitempty"` + InternalServerError bool `json:"internal_server_error,omitempty"` + DataDuplicate bool `json:"data_duplicate,omitempty"` + QueryError bool `json:"query_error,omitempty"` + Message string `json:"message,omitempty"` +} diff --git a/space/space/space/space/space/space/space/models/RequestModel.go b/space/space/space/space/space/space/space/models/RequestModel.go new file mode 100644 index 0000000000000000000000000000000000000000..cfae888ec34edbdd3f5b6588050e2b382c63792d --- /dev/null +++ b/space/space/space/space/space/space/space/models/RequestModel.go @@ -0,0 +1,13 @@ +package models + +type LoginRequest struct { + Email string `json:"email" binding:"required"` + Password string `json:"password" binding:"required"` +} + +type RegisterRequest struct { + Name string `json:"name"` + Email string `json:"email" binding:"required,email"` + Phone int `json:"phone"` + Password string `json:"password" binding:"required"` +} diff --git a/space/space/space/space/space/space/space/models/ResponseModel.go b/space/space/space/space/space/space/space/models/ResponseModel.go new file mode 100644 index 0000000000000000000000000000000000000000..9ce401186cb92d50737155815c1f511164b86407 --- /dev/null +++ b/space/space/space/space/space/space/space/models/ResponseModel.go @@ -0,0 +1 @@ +package models diff --git a/space/space/space/space/space/space/space/repositories/AccountRepository.go b/space/space/space/space/space/space/space/repositories/AccountRepository.go new file mode 100644 index 0000000000000000000000000000000000000000..79e43f0707a436c344e5cddd53187d428274bde9 --- /dev/null +++ b/space/space/space/space/space/space/space/repositories/AccountRepository.go @@ -0,0 +1,24 @@ +package repositories + +import ( + "fmt" + + "go-dp.abdanhafidz.com/models" +) + +func GetAccountbyEmailPassword(username string, password string) Repository[models.Account, models.Account] { + repo := Construct[models.Account, models.Account]( + models.Account{Username: username, Password: password}, + ) + Find(repo) + return *repo +} + +func CreateAccount(account models.Account) Repository[models.Account, models.Account] { + fmt.Println(account) + repo := Construct[models.Account, models.Account]( + account, + ) + Create(repo) + return *repo +} diff --git a/space/space/space/space/space/space/space/repositories/DatabaseScoope.go b/space/space/space/space/space/space/space/repositories/DatabaseScoope.go new file mode 100644 index 0000000000000000000000000000000000000000..e11987b79a890dc6e84923fe7c241c6e8fa99c35 --- /dev/null +++ b/space/space/space/space/space/space/space/repositories/DatabaseScoope.go @@ -0,0 +1,5 @@ +package repositories + +import "go-dp.abdanhafidz.com/config" + +var db = config.DB diff --git a/space/space/space/space/space/space/space/repositories/repositories.go b/space/space/space/space/space/space/space/repositories/repositories.go new file mode 100644 index 0000000000000000000000000000000000000000..c82f550472331b9b8d0f5d4cdcf5aab441e5cdfa --- /dev/null +++ b/space/space/space/space/space/space/space/repositories/repositories.go @@ -0,0 +1,96 @@ +package repositories + +import ( + "fmt" + + "go-dp.abdanhafidz.com/config" + "gorm.io/gorm" +) + +type Repositories interface { + FindAllPaginate() + Where() + Find() + Create() + Update() + CustomQuery() + Delete() +} +type PaginationConstructor struct { + Limit int + Offset int + Filter string +} + +type CustomQueryConstructor struct { + SQL string + Values interface{} +} + +type Repository[TConstructor any, TResult any] struct { + Constructor TConstructor + Pagination PaginationConstructor + CustomQuery CustomQueryConstructor + Result TResult + Transaction *gorm.DB + RowsCount int + NoRecord bool + RowsError error +} + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Repository[TConstructor, TResult] { + fmt.Println("Len = ", len(constructor)) + if len(constructor) == 1 { + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB, + } + } + return &Repository[TConstructor, TResult]{ + Constructor: constructor[0], + Transaction: config.DB.Begin(), + } +} +func (repo *Repository[T1, T2]) Transactions(transactions ...func(*Repository[T1, T2])) { + i := 1 + for _, tx := range transactions { + tx(repo) + repo.RowsError = repo.Transaction.Error + if repo.RowsError != nil { + repo.Transaction.Rollback() + return + } else { + repo.Transaction.SavePoint("Save Point : " + string(i)) + repo.Transaction.Commit() + } + } +} +func WhereGivenConstructor[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Where(&repo.Constructor) +} +func Find[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Find(&repo.Result) +} + +func FinddAllPaginate[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Limit(repo.Pagination.Limit).Offset(repo.Pagination.Offset).Find(&repo.Result) +} + +func Create[T1 any](repo *Repository[T1, T1]) { + fmt.Println(repo.Constructor) + repo.Transaction.Create(&repo.Constructor) + repo.Result = repo.Constructor +} + +func Update[T1 any](repo *Repository[T1, T1]) { + repo.Transaction.Save(&repo.Constructor) + repo.Result = repo.Constructor +} + +func Delete[T1 any](repo *Repository[T1, T1]) { + repo.Transaction.Delete(&repo.Constructor) +} + +func CustomQuery[T1 any, T2 any](repo *Repository[T1, T2]) { + repo.Transaction.Raw(repo.CustomQuery.SQL, repo.CustomQuery.Values).Scan(&repo.Result) +} diff --git a/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/router/router.go new file mode 100644 index 0000000000000000000000000000000000000000..a225f4fa148e7c8b5551686ace69dcd3c1b17562 --- /dev/null +++ b/space/space/space/space/space/space/space/router/router.go @@ -0,0 +1,17 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/config" + "go-dp.abdanhafidz.com/controller" +) + +func StartService() { + router := gin.Default() + routerGroup := router.Group("/api") + { + routerGroup.POST("/login", controller.LoginController) + routerGroup.POST("/register", controller.RegisterController) + } + router.Run(config.TCP_ADDRESS) +} diff --git a/space/space/space/space/space/space/space/services/LoginService.go b/space/space/space/space/space/space/space/services/LoginService.go new file mode 100644 index 0000000000000000000000000000000000000000..e798298cb58036f0b00cc04a919500d6b9c83c09 --- /dev/null +++ b/space/space/space/space/space/space/space/services/LoginService.go @@ -0,0 +1,36 @@ +package services + +import ( + "go-dp.abdanhafidz.com/middleware" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/repositories" +) + +type LoginConstructor struct { + Email string + Password string +} + +type AuthenticationService struct { + Service[LoginConstructor, models.Account] +} + +func (s *AuthenticationService) Authenticate() { + accountData := repositories.GetAccountbyEmailPassword(s.Constructor.Email, s.Constructor.Password) + if accountData.NoRecord { + s.Exception.DataNotFound = true + s.Exception.Message = "there is no account with given email!" + return + } + + if middleware.VerifyPassword(accountData.Result.Password, s.Constructor.Password) != nil { + s.Exception.Unauthorized = true + s.Exception.Message = "incorrect password!" + return + } + + s.Result = accountData.Result + s.Error = accountData.RowsError +} + +// LoginHandler handles user login diff --git a/space/space/space/space/space/space/space/services/RegisterService.go b/space/space/space/space/space/space/space/services/RegisterService.go new file mode 100644 index 0000000000000000000000000000000000000000..615a3a595a056802ecd6e130c8f0f04be2643d05 --- /dev/null +++ b/space/space/space/space/space/space/space/services/RegisterService.go @@ -0,0 +1,31 @@ +package services + +import ( + "errors" + + "go-dp.abdanhafidz.com/middleware" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/repositories" + "gorm.io/gorm" +) + +type RegisterService struct { + Service[models.Account, models.Account] +} + +func (s *RegisterService) Create() { + hashed_password, _ := middleware.HashPassword(s.Constructor.Password) + s.Constructor.Password = hashed_password + accountCreated := repositories.CreateAccount(s.Constructor) + if errors.Is(accountCreated.RowsError, gorm.ErrDuplicatedKey) { + s.Exception.DataDuplicate = true + s.Exception.Message = "There is account registered with given data!" + return + } 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) { + s.Exception.BadRequest = true + s.Exception.Message = "Bad request!" + return + } + s.Result = accountCreated.Result + s.Result.Password = "SECRET" +} diff --git a/space/space/space/space/space/space/space/services/services.go b/space/space/space/space/space/space/space/services/services.go new file mode 100644 index 0000000000000000000000000000000000000000..e684786fdd3dc071dd085b87f27a0e7bb8becfff --- /dev/null +++ b/space/space/space/space/space/space/space/services/services.go @@ -0,0 +1,31 @@ +package services + +import "go-dp.abdanhafidz.com/models" + +type ( + Services interface { + Retrieve() + Update() + Create() + Delete() + Validate() + Authenticate() + Authorize() + } + Service[TConstructor any, TResult any] struct { + Constructor TConstructor + Result TResult + Exception models.Exception + Error error + } +) + +func Construct[TConstructor any, TResult any](constructor ...TConstructor) *Service[TConstructor, TResult] { + if len(constructor) == 0 { + return &Service[TConstructor, TResult]{} + } + + return &Service[TConstructor, TResult]{ + Constructor: constructor[0], + } +} diff --git a/space/space/space/space/space/space/space/space/.gitattributes b/space/space/space/space/space/space/space/space/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/space/space/space/space/space/space/space/space/.gitattributes @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/space/space/space/space/space/space/space/space/README.md b/space/space/space/space/space/space/space/space/README.md new file mode 100644 index 0000000000000000000000000000000000000000..6ebd472f68999f72453c5168cded8b9e4a1026f9 --- /dev/null +++ b/space/space/space/space/space/space/space/space/README.md @@ -0,0 +1,8 @@ +--- +title: Quzuu Api Dev +emoji: 🐠 +colorFrom: indigo +colorTo: gray +sdk: docker +pinned: false +--- \ No newline at end of file diff --git a/space/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/space/utils/APIResponse.go new file mode 100644 index 0000000000000000000000000000000000000000..7a3e7df219e76ae94bc17ef748c6dfe7f8afeb93 --- /dev/null +++ b/space/space/space/space/space/space/space/utils/APIResponse.go @@ -0,0 +1,39 @@ +package utils + +import ( + "net/http" + "reflect" + + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" + "go-dp.abdanhafidz.com/services" +) + +func ResponseOK(c *gin.Context, data any) { + c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"}) +} + +func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { + message := exception.Message + exception.Message = "" + c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) + return +} + +func SendResponse(c *gin.Context, data services.Service[any, any]) { + if reflect.ValueOf(data.Exception).IsNil() { + ResponseOK(c, data) + } else { + if data.Exception.Unauthorized { + ResponseFAIL(c, 401, data.Exception) + } else if data.Exception.BadRequest { + ResponseFAIL(c, 400, data.Exception) + } else if data.Exception.DataNotFound { + ResponseFAIL(c, 404, data.Exception) + } else if data.Exception.InternalServerError { + ResponseFAIL(c, 500, data.Exception) + } else { + ResponseFAIL(c, 403, data.Exception) + } + } +} diff --git a/space/space/space/space/space/space/space/utils/Exception.go b/space/space/space/space/space/space/space/utils/Exception.go new file mode 100644 index 0000000000000000000000000000000000000000..0b273b83d7e7cb140e2b9df6801f161f0ff08be4 --- /dev/null +++ b/space/space/space/space/space/space/space/utils/Exception.go @@ -0,0 +1 @@ +package utils diff --git a/space/space/space/space/space/space/space/utils/Helper.go b/space/space/space/space/space/space/space/utils/Helper.go new file mode 100644 index 0000000000000000000000000000000000000000..3e36fc57268d71ef58e1511b038f8c56e7f8928c --- /dev/null +++ b/space/space/space/space/space/space/space/utils/Helper.go @@ -0,0 +1,11 @@ +package utils + +import ( + "github.com/gin-gonic/gin" + "go-dp.abdanhafidz.com/models" +) + +func GetAccount(c *gin.Context) models.AccountData { + cParam, _ := c.Get("accountData") + return cParam.(models.AccountData) +} diff --git a/space/space/space/space/space/space/space/utils/Logger.go b/space/space/space/space/space/space/space/utils/Logger.go new file mode 100644 index 0000000000000000000000000000000000000000..98d4a9447588e08458738263844c45eb0d6ae98b --- /dev/null +++ b/space/space/space/space/space/space/space/utils/Logger.go @@ -0,0 +1,19 @@ +package utils + +import ( + "log" + "os" + + "go-dp.abdanhafidz.com/config" +) + +func LogError(errorLogged error) { + file, err := os.OpenFile(config.LOG_PATH+"error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) + if err != nil { + log.Fatal(err) + } + + log.SetOutput(file) + + log.Println(errorLogged) +} diff --git a/space/space/space/space/space/space/space/utils/utils.go b/space/space/space/space/space/space/space/utils/utils.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/space/space/space/space/space/space/space/utils/utils.go @@ -0,0 +1,9 @@ +package utils + +func ternaryMessage(condition bool, valueIfTrue string, valueIfFalse string) string { + if condition { + return valueIfTrue + } else { + return valueIfFalse + } +} diff --git a/space/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/space/utils/APIResponse.go index 7a3e7df219e76ae94bc17ef748c6dfe7f8afeb93..da8a2be04c3d8ef4a28939d5c40cdb3574efb650 100644 --- a/space/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/space/utils/APIResponse.go @@ -11,6 +11,7 @@ import ( func ResponseOK(c *gin.Context, data any) { c.JSON(http.StatusOK, gin.H{"message": "Request Success!", "data": data, "status": "success"}) + return } func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { diff --git a/space/space/space/space/space/utils/APIResponse.go b/space/space/space/space/space/utils/APIResponse.go index da8a2be04c3d8ef4a28939d5c40cdb3574efb650..68eb789f1daf3ed57cfe6ea600c948f2ee0be9cc 100644 --- a/space/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/space/utils/APIResponse.go @@ -18,6 +18,7 @@ func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { message := exception.Message exception.Message = "" c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) + c. return } diff --git a/space/space/space/space/utils/APIResponse.go b/space/space/space/space/utils/APIResponse.go index 68eb789f1daf3ed57cfe6ea600c948f2ee0be9cc..da8a2be04c3d8ef4a28939d5c40cdb3574efb650 100644 --- a/space/space/space/space/utils/APIResponse.go +++ b/space/space/space/space/utils/APIResponse.go @@ -18,7 +18,6 @@ func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { message := exception.Message exception.Message = "" c.AbortWithStatusJSON(status, gin.H{"status": "error", "message": message, "error": exception}) - c. return } diff --git a/space/space/space/utils/Logger.go b/space/space/space/utils/Logger.go index 98d4a9447588e08458738263844c45eb0d6ae98b..6473e869e33fbe10150a80de942cfcb9f46a4749 100644 --- a/space/space/space/utils/Logger.go +++ b/space/space/space/utils/Logger.go @@ -15,5 +15,5 @@ func LogError(errorLogged error) { log.SetOutput(file) - log.Println(errorLogged) + log.Println("Error Log :", errorLogged) } diff --git a/space/space/utils/Logger.go b/space/space/utils/Logger.go index 6473e869e33fbe10150a80de942cfcb9f46a4749..2bb3f755429b22e6c3d62cdf95ac630010eacae6 100644 --- a/space/space/utils/Logger.go +++ b/space/space/utils/Logger.go @@ -1,6 +1,7 @@ package utils import ( + "fmt" "log" "os" @@ -8,6 +9,7 @@ import ( ) func LogError(errorLogged error) { + fmt.Println("There is an error!") file, err := os.OpenFile(config.LOG_PATH+"error_log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { log.Fatal(err)