diff --git a/repositories/account_repository.go b/repositories/account_repository.go index 39fbb3c556f7a75e910ca4cc5b3caae6036bda2f..13fad342bdbaffc594e22626a7db1067029010c2 100644 --- a/repositories/account_repository.go +++ b/repositories/account_repository.go @@ -21,7 +21,7 @@ func NewAccountRepository(db *gorm.DB) AccountRepository { return &accountRepository{ repository: &repository[models.Account]{ entity: models.Account{}, - transaction: db, + transaction: db.Begin(), }, } } diff --git a/repositories/repository.go b/repositories/repository.go index 7d4289e6b83d145fa485f9306ab2971678d06487..e5b703fdf2803d348d72116c35674c4738c79eeb 100644 --- a/repositories/repository.go +++ b/repositories/repository.go @@ -55,36 +55,32 @@ func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx conte } func (repo *repository[T1]) Where(ctx context.Context) { - tx := repo.transaction - tx.WithContext(ctx).Model(&repo.entity) + tx := repo.transaction.WithContext(ctx).Model(&repo.entity) if tx.Error != nil { repo.rowsCount = int(tx.RowsAffected) - repo.noRecord = repo.rowsCount == 0 + repo.noRecord = (repo.rowsCount == 0) repo.rowsError = tx.Error repo.rowsError = repo.transaction.Error tx.Rollback() return } - repo.rowsCount = int(tx.RowsAffected) - repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error + return } func (repo *repository[T1]) Find(ctx context.Context, res any) { - - tx := repo.transaction - tx.WithContext(ctx).First(&res) - if tx.Error != nil { - repo.rowsCount = int(tx.RowsAffected) + repo.transaction = repo.transaction.WithContext(ctx).First(&res) + if repo.transaction.Error != nil { + repo.rowsCount = int(repo.transaction.RowsAffected) repo.noRecord = repo.rowsCount == 0 - repo.rowsError = tx.Error repo.rowsError = repo.transaction.Error - tx.Rollback() + repo.rowsError = repo.transaction.Error + repo.transaction.Rollback() return } - repo.rowsCount = int(tx.RowsAffected) + repo.rowsCount = int(repo.transaction.RowsAffected) repo.noRecord = repo.rowsCount == 0 - repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error } diff --git a/services/authentication_service.go b/services/authentication_service.go index a23548d6dfddc5341df55e1bfbfe523d78691a18..00f7bed2f74bb15d7834c61d4a337abb80e022b0 100644 --- a/services/authentication_service.go +++ b/services/authentication_service.go @@ -59,15 +59,14 @@ func (s *authenticationService) Register(ctx context.Context, passPhrase string, func (s *authenticationService) Login(ctx context.Context, passPhrase string) string { account := s.repository.GetAccountByPassPhrase(ctx, passPhrase) - if s.ThrowsRepoException() { - return "" - } if s.repository.IsNoRecord() { s.ThrowsException(&s.exception.Unauthorized, "Account not found!") return " " } - + if s.ThrowsRepoException() { + return "" + } token := s.jwtService.GenerateToken(ctx, models.JWTCustomClaims{IdUser: account.ID}) if s.jwtService.Error() != nil { s.ThrowsException(&s.exception.Unauthorized, "JWTService Error") diff --git a/space/repositories/account_repository.go b/space/repositories/account_repository.go index 7fba5a8035a4bdc289f47774896b4cce3884fa1c..39fbb3c556f7a75e910ca4cc5b3caae6036bda2f 100644 --- a/space/repositories/account_repository.go +++ b/space/repositories/account_repository.go @@ -35,6 +35,6 @@ func (r *accountRepository) CreateAccount(ctx context.Context, passPhrase string func (r *accountRepository) GetAccountByPassPhrase(ctx context.Context, passPhrase string) (res models.Account) { r.entity.PassPhrase = passPhrase r.Where(ctx) - r.Find(ctx, res) + r.Find(ctx, &res) return res } diff --git a/space/space/config/turnstile_client_config.go b/space/space/config/turnstile_client_config.go index 3e06a0e9ed49f7fbcc29ba9e200199bbba3968cd..564bab71190a1be1278460ce120440eff3c11cb6 100644 --- a/space/space/config/turnstile_client_config.go +++ b/space/space/config/turnstile_client_config.go @@ -1,11 +1,11 @@ package config -import "github.com/9ssi7/turnstile" +import ( + "github.com/meyskens/go-turnstile" +) -var TurnstileClient turnstile.Service +var TurnstileClient *turnstile.Turnstile func InitTurnStileClient() { - TurnstileClient = turnstile.New(turnstile.Config{ - Secret: TURNSTILE_SECRET_KEY, - }) + TurnstileClient = turnstile.New(TURNSTILE_SECRET_KEY) } diff --git a/space/space/controller/authentication_controller.go b/space/space/controller/authentication_controller.go index 6290e0254acba7b54dc3a830ff18f159c3d4a4d0..c91a8ed788a764b03fe78724b5d607e874aecdc9 100644 --- a/space/space/controller/authentication_controller.go +++ b/space/space/controller/authentication_controller.go @@ -22,7 +22,7 @@ func NewAuthenticationController(authenticationService services.AuthenticationSe } func (c *authenticationController) Register(ctx *gin.Context) { var loginRequest models.LoginRequest - c.RequestJSON(ctx, loginRequest) + c.RequestJSON(ctx, &loginRequest) if loginRequest.IPAddress == "" { loginRequest.IPAddress = ctx.ClientIP() } @@ -32,7 +32,7 @@ func (c *authenticationController) Register(ctx *gin.Context) { } func (c *authenticationController) Login(ctx *gin.Context) { var loginRequest models.LoginRequest - c.RequestJSON(ctx, loginRequest) + c.RequestJSON(ctx, &loginRequest) token := c.service.Login(ctx.Request.Context(), loginRequest.PassPhrase) diff --git a/space/space/go.mod b/space/space/go.mod index e7d59b91e84564b83c551448a8e9ee16ddca5b31..c7af92d87426d9545f8a7bb324e5e0e4846ef435 100644 --- a/space/space/go.mod +++ b/space/space/go.mod @@ -37,6 +37,7 @@ require ( 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/meyskens/go-turnstile v0.0.0-20230622160222-89160e594ca1 // 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.2.3 // indirect diff --git a/space/space/go.sum b/space/space/go.sum index c3fbd15a2759b505f37335b97b099e034985b1ba..7128fcba4744da77cc9aadd7b80048c37a5512af 100644 --- a/space/space/go.sum +++ b/space/space/go.sum @@ -67,6 +67,8 @@ 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/meyskens/go-turnstile v0.0.0-20230622160222-89160e594ca1 h1:lGjDY7OC1VfMpuVUN+b59vPPepbPx/eJQXGqpM2pCdw= +github.com/meyskens/go-turnstile v0.0.0-20230622160222-89160e594ca1/go.mod h1:YbEb1gFAr7w2NcabqA2aPAeyW4Mhf85fmt+vVrrLo4s= 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= diff --git a/space/space/models/entities_model.go b/space/space/models/entities_model.go index fa3e1a9fafb2ff35bd09a2f950b709df698954be..348c8d2aefebe3aab52152046fe9d27122b58f26 100644 --- a/space/space/models/entities_model.go +++ b/space/space/models/entities_model.go @@ -8,7 +8,7 @@ import ( type Account struct { ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` - PassPhrase string `gorm:"not null;"` + PassPhrase string `gorm:"not null;uniqueIndex"` CreatedAt time.Time DeletedAt *time.Time `gorm:"column:deleted_at"` // perhatikan penamaan kolom } diff --git a/space/space/repositories/account_repository.go b/space/space/repositories/account_repository.go index 311942c90c6dbe218600abb7542ae2aa6f99a9b3..7fba5a8035a4bdc289f47774896b4cce3884fa1c 100644 --- a/space/space/repositories/account_repository.go +++ b/space/space/repositories/account_repository.go @@ -9,8 +9,8 @@ import ( type AccountRepository interface { Repository - CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) - GetAccountByPassPhrase(ctx context.Context, fingePrint string) (res models.Account) + CreateAccount(ctx context.Context, passPhrase string) (res models.Account) + GetAccountByPassPhrase(ctx context.Context, passPhrase string) (res models.Account) } type accountRepository struct { diff --git a/space/space/repositories/repository.go b/space/space/repositories/repository.go index 5c36e497d68a54ac5d089d7fddd62371a1755f7e..7d4289e6b83d145fa485f9306ab2971678d06487 100644 --- a/space/space/repositories/repository.go +++ b/space/space/repositories/repository.go @@ -56,8 +56,15 @@ func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx conte } func (repo *repository[T1]) Where(ctx context.Context) { tx := repo.transaction - tx.WithContext(ctx).Where(&repo.entity) - + tx.WithContext(ctx).Model(&repo.entity) + if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error + tx.Rollback() + return + } repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error @@ -66,11 +73,15 @@ func (repo *repository[T1]) Where(ctx context.Context) { func (repo *repository[T1]) Find(ctx context.Context, res any) { tx := repo.transaction - tx.WithContext(ctx).Find(&res) + tx.WithContext(ctx).First(&res) if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } - repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error @@ -82,9 +93,13 @@ func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { tx := repo.transaction tx.WithContext(ctx).Limit(repo.pagination.limit).Offset(repo.pagination.offset).Find(&res) if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } - repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 repo.rowsError = tx.Error @@ -116,7 +131,12 @@ func (repo *repository[T1]) Update(ctx context.Context) { tx := repo.transaction tx.WithContext(ctx).Save(&repo.entity).Find(&repo.entity) if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } repo.rowsCount = int(tx.RowsAffected) @@ -129,8 +149,14 @@ func (repo *repository[T1]) Delete(ctx context.Context) { tx := repo.transaction tx.WithContext(ctx).Delete(&repo.entity) + if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } repo.rowsCount = int(tx.RowsAffected) @@ -143,8 +169,14 @@ func (repo *repository[T1]) Query(ctx context.Context, res any) { tx := repo.transaction tx.WithContext(ctx).Model(&repo.entity).Raw(repo.customQuery.sql, repo.customQuery.values).Scan(&res) + if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } repo.rowsCount = int(tx.RowsAffected) diff --git a/space/space/services/authentication_service.go b/space/space/services/authentication_service.go index 25448362dcdf19376679796eb8bd51d27d224e23..a23548d6dfddc5341df55e1bfbfe523d78691a18 100644 --- a/space/space/services/authentication_service.go +++ b/space/space/services/authentication_service.go @@ -2,10 +2,11 @@ package services import ( "context" + "fmt" - "github.com/9ssi7/turnstile" models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/meyskens/go-turnstile" ) type AuthenticationService interface { @@ -16,11 +17,11 @@ type AuthenticationService interface { type authenticationService struct { *service[repositories.AccountRepository] - turnStileClient turnstile.Service + turnStileClient *turnstile.Turnstile jwtService JWTService } -func NewAuthenticationService(accountRepository repositories.AccountRepository, turnStileClient turnstile.Service, jwtService JWTService) AuthenticationService { +func NewAuthenticationService(accountRepository repositories.AccountRepository, turnStileClient *turnstile.Turnstile, jwtService JWTService) AuthenticationService { return &authenticationService{ service: &service[repositories.AccountRepository]{repository: accountRepository}, turnStileClient: turnStileClient, @@ -28,14 +29,16 @@ func NewAuthenticationService(accountRepository repositories.AccountRepository, } } func (s *authenticationService) Register(ctx context.Context, passPhrase string, turnstile string, ip string) string { - verifiedTurnStile, err := s.turnStileClient.Verify(ctx, turnstile, ip) - + turnStileResponse, err := s.turnStileClient.Verify(turnstile, ip) + fmt.Println(turnstile) + fmt.Println(turnStileResponse) if err != nil { - s.ThrowsException(&s.exception.Unauthorized, "Turnstile error!") + s.ThrowsException(&s.exception.Unauthorized, "Turnstile error!, Turnstile Respose :") + s.ThrowsError(err) return "" } - if verifiedTurnStile { + if turnStileResponse.Success { account := s.repository.CreateAccount(ctx, passPhrase) if s.ThrowsRepoException() { return "" @@ -50,9 +53,8 @@ func (s *authenticationService) Register(ctx context.Context, passPhrase string, return token } else { s.ThrowsException(&s.exception.Unauthorized, "Invalid turnstile payload!") + return "" } - - return "" } func (s *authenticationService) Login(ctx context.Context, passPhrase string) string { diff --git a/space/space/space/space/Dockerfile b/space/space/space/space/Dockerfile index b7c156148a96794ec43fe41dc9c4d0dc37651bcf..3937c769375f709a8b91db73f15eb2bdb6575f8e 100644 --- a/space/space/space/space/Dockerfile +++ b/space/space/space/space/Dockerfile @@ -31,8 +31,8 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ echo "LOG_PATH=logs" >> .env && \ echo "EMAIL_VERIFICATION_DURATION=2" >> .env && \ echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ - echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env - + echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env && \ + echo "TURNSTILE_SECRET_KEY =$(cat /run/secrets/TURNSTILE_SECRET_KEY>/dev/null)" >> .env # Buat direktori audio dan logs, beri izin dan kepemilikan ke appuser RUN mkdir -p /app/images /app/logs /app/audio && \ chmod -R 777 /app/images /app/logs /app/audio && \ diff --git a/space/space/space/space/controller/authentication_controller.go b/space/space/space/space/controller/authentication_controller.go index 3030fe0006e846e5fdf2e0ece0e0abe3f1c175a2..6290e0254acba7b54dc3a830ff18f159c3d4a4d0 100644 --- a/space/space/space/space/controller/authentication_controller.go +++ b/space/space/space/space/controller/authentication_controller.go @@ -23,7 +23,7 @@ func NewAuthenticationController(authenticationService services.AuthenticationSe func (c *authenticationController) Register(ctx *gin.Context) { var loginRequest models.LoginRequest c.RequestJSON(ctx, loginRequest) - if (loginRequest.IPAddress == ""){ + if loginRequest.IPAddress == "" { loginRequest.IPAddress = ctx.ClientIP() } token := c.service.Register(ctx.Request.Context(), loginRequest.PassPhrase, loginRequest.TurnStile, loginRequest.IPAddress) diff --git a/space/space/space/space/space/config/config.go b/space/space/space/space/space/config/config.go index 279309a1e503f2423701b9d5415d493bc8ac9c66..d902550fd4c22b5aaa1543c22eff30b9a6e8bf93 100644 --- a/space/space/space/space/space/config/config.go +++ b/space/space/space/space/space/config/config.go @@ -5,4 +5,5 @@ func RunConfig() { InitializeDatabase() InitializeOpenAIClient() InitializeReplicateClient() + InitTurnStileClient() } diff --git a/space/space/space/space/space/config/env_config.go b/space/space/space/space/space/config/env_config.go index ffdc0ab332f3948ab86a1866c701747e5f2767e6..b85fdfd53e6cbc90883bc8990b61448adc8a656e 100644 --- a/space/space/space/space/space/config/env_config.go +++ b/space/space/space/space/space/config/env_config.go @@ -6,6 +6,7 @@ import ( "github.com/joho/godotenv" ) + var TCP_ADDRESS string var LOG_PATH string var HOST_ADDRESS string @@ -13,6 +14,7 @@ var HOST_PORT string var EMAIL_VERIFICATION_DURATION int var OPEN_AI_API_KEY string var REPLICATE_API_KEY string +var TURNSTILE_SECRET_KEY string func InitializeEnv() { godotenv.Load() @@ -23,4 +25,5 @@ func InitializeEnv() { EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) OPEN_AI_API_KEY = os.Getenv("OPEN_AI_API_KEY") REPLICATE_API_KEY = os.Getenv("REPLICATE_API_KEY") -} \ No newline at end of file + TURNSTILE_SECRET_KEY = os.Getenv("TURNSTILE_SECRET_KEY") +} diff --git a/space/space/space/space/space/config/turnstile_client_config.go b/space/space/space/space/space/config/turnstile_client_config.go new file mode 100644 index 0000000000000000000000000000000000000000..3e06a0e9ed49f7fbcc29ba9e200199bbba3968cd --- /dev/null +++ b/space/space/space/space/space/config/turnstile_client_config.go @@ -0,0 +1,11 @@ +package config + +import "github.com/9ssi7/turnstile" + +var TurnstileClient turnstile.Service + +func InitTurnStileClient() { + TurnstileClient = turnstile.New(turnstile.Config{ + Secret: TURNSTILE_SECRET_KEY, + }) +} diff --git a/space/space/space/space/space/controller/authentication_controller.go b/space/space/space/space/space/controller/authentication_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..3030fe0006e846e5fdf2e0ece0e0abe3f1c175a2 --- /dev/null +++ b/space/space/space/space/space/controller/authentication_controller.go @@ -0,0 +1,40 @@ +package controller + +import ( + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + "github.com/gin-gonic/gin" +) + +type AuhenticationController interface { + Controller + Login(ctx *gin.Context) + Register(ctx *gin.Context) +} +type authenticationController struct { + *controller[services.AuthenticationService] +} + +func NewAuthenticationController(authenticationService services.AuthenticationService) AuhenticationController { + return &authenticationController{ + controller: &controller[services.AuthenticationService]{service: authenticationService}, + } +} +func (c *authenticationController) Register(ctx *gin.Context) { + var loginRequest models.LoginRequest + c.RequestJSON(ctx, loginRequest) + if (loginRequest.IPAddress == ""){ + loginRequest.IPAddress = ctx.ClientIP() + } + token := c.service.Register(ctx.Request.Context(), loginRequest.PassPhrase, loginRequest.TurnStile, loginRequest.IPAddress) + + c.Response(ctx, token) +} +func (c *authenticationController) Login(ctx *gin.Context) { + var loginRequest models.LoginRequest + c.RequestJSON(ctx, loginRequest) + + token := c.service.Login(ctx.Request.Context(), loginRequest.PassPhrase) + + c.Response(ctx, token) +} diff --git a/space/space/space/space/space/factory/authentication_factory.go b/space/space/space/space/space/factory/authentication_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..db44395ec98fec30d99a7c56f6b4ed761247a784 --- /dev/null +++ b/space/space/space/space/space/factory/authentication_factory.go @@ -0,0 +1,16 @@ +package factory + +import ( + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/controller" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" +) + +func NewAuthenticationModule() controller.AuhenticationController { + accountRepository := repositories.NewAccountRepository(config.DB) + jwtService := services.NewJWTService(accountRepository, config.Salt) + authenticationService := services.NewAuthenticationService(accountRepository, config.TurnstileClient, jwtService) + authenticationController := controller.NewAuthenticationController(authenticationService) + return authenticationController +} diff --git a/space/space/space/space/space/go.mod b/space/space/space/space/space/go.mod index fd16864743e8c7e35500854f5cad1cfbeddd8726..e7d59b91e84564b83c551448a8e9ee16ddca5b31 100644 --- a/space/space/space/space/space/go.mod +++ b/space/space/space/space/space/go.mod @@ -14,6 +14,7 @@ require ( ) require ( + github.com/9ssi7/turnstile v1.0.0 // indirect github.com/bytedance/sonic v1.13.1 // indirect github.com/bytedance/sonic/loader v0.2.4 // indirect github.com/cloudwego/base64x v0.1.5 // indirect @@ -23,6 +24,8 @@ require ( 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.5 // indirect + github.com/golang-jwt/jwt/v4 v4.5.2 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.7.2 // indirect diff --git a/space/space/space/space/space/go.sum b/space/space/space/space/space/go.sum index 56ea54f781eb5a01a1a3b54a84e872d795922a0e..c3fbd15a2759b505f37335b97b099e034985b1ba 100644 --- a/space/space/space/space/space/go.sum +++ b/space/space/space/space/space/go.sum @@ -1,3 +1,5 @@ +github.com/9ssi7/turnstile v1.0.0 h1:MDH8pXAbStCeH9Yul1MOIp0e4YKctpUXvcPEqKEUyZs= +github.com/9ssi7/turnstile v1.0.0/go.mod h1:R37Sy9c6VdYzQc0jr/hUojAdn3bYpeKaAoo9nUSqVSI= github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= @@ -28,11 +30,15 @@ github.com/go-playground/validator/v10 v10.25.0 h1:5Dh7cjvzR7BRZadnsVOzPhWsrwUr0 github.com/go-playground/validator/v10 v10.25.0/go.mod h1:GGzBIJMuE98Ic/kJsBXbz1x/7cByt++cQ+YOuDM5wus= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/golang-jwt/jwt/v4 v4.5.2 h1:YtQM7lnr8iZ+j5q71MGKkNw9Mn7AjHM68uc9g5fXeUI= +github.com/golang-jwt/jwt/v4 v4.5.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= 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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= diff --git a/space/space/space/space/space/middleware/authentication_middleware.go b/space/space/space/space/space/middleware/authentication_middleware.go index d23e075a9b088fdd26e7dd964f826b6d3ac627fb..c565175c215686fef816d8dd603a7c07133cfc6a 100644 --- a/space/space/space/space/space/middleware/authentication_middleware.go +++ b/space/space/space/space/space/middleware/authentication_middleware.go @@ -3,13 +3,12 @@ package middleware import ( - "time" - config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" "github.com/gin-gonic/gin" - "github.com/golang-jwt/jwt/v5" + "github.com/golang-jwt/jwt/v4" ) var salt = config.Salt @@ -17,54 +16,39 @@ var secretKey = []byte(salt) // VerifyPassword verifies if the provided password matches the hashed password -type CustomClaims struct { - jwt.RegisteredClaims - UserID int `json:"id"` -} +func AuthenticationMiddleware(jwtService services.JWTService) gin.HandlerFunc { + return func(c *gin.Context) { + tokenString := c.GetHeader("Authorization") + if tokenString == "" { + utils.ResponseFAIL(c, 401, models.Exception{ + Unauthorized: true, + Message: "You Have To Login First!", + }) + return + } -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 - } + token, err := jwt.ParseWithClaims(tokenString, &models.JWTCustomClaims{}, func(token *jwt.Token) (interface{}, error) { + return secretKey, nil + }) - // Extract the claims - claims, ok := token.Claims.(*CustomClaims) - if !ok || !token.Valid { - return 0, "invalid-token", err - } - if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { - return 0, "expired", err - } - - return claims.UserID, "valid", err -} + if err != nil || !token.Valid { + utils.ResponseFAIL(c, 401, models.Exception{ + Unauthorized: true, + Message: "Invalid Authorization Token!", + }) + return + } -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.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) - // fmt.Println("Verify Status :", currAccData.verifyStatus) - if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { - currAccData.UserID = 0 - utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"}) - c.Abort() + claims, ok := token.Claims.(*models.JWTCustomClaims) + if !ok { + utils.ResponseFAIL(c, 401, models.Exception{ + Unauthorized: true, + Message: "Invalid Authorization Token!", + }) return - } else { - c.Set("accountData", currAccData) - c.Next() } - } else { - currAccData.UserID = 0 - currAccData.VerifyStatus = "no-token" - currAccData.ErrVerif = nil - utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) - c.Abort() - return - } + c.Set("user_id", claims.IdUser) + c.Next() + } } diff --git a/space/space/space/space/space/models/authentication_dto.go b/space/space/space/space/space/models/authentication_dto.go index 338f6dab4a210f64b1b7377b5d2f9887ee09ced3..a9ae215f4687f6cf95638082c9f11b83dce17965 100644 --- a/space/space/space/space/space/models/authentication_dto.go +++ b/space/space/space/space/space/models/authentication_dto.go @@ -1,6 +1,7 @@ package models type LoginRequest struct { - PassPhrase string `json:"pass_phrase binding:"required"` - TurnStile string `json:"turnstile_payload binding:"required"` + PassPhrase string `json:"pass_phrase" binding:"required"` + TurnStile string `json:"turnstile_payload" binding:"required"` + IPAddress string `json:"ip_address"` } diff --git a/space/space/space/space/space/models/authentication_payload_model.go b/space/space/space/space/space/models/authentication_payload_model.go index 0203b9b340d066705491edf66830674602f89655..9203b2156603b9efd2197feadcd639c8ec30c04f 100644 --- a/space/space/space/space/space/models/authentication_payload_model.go +++ b/space/space/space/space/space/models/authentication_payload_model.go @@ -1,7 +1,15 @@ package models +import ( + "github.com/golang-jwt/jwt/v4" + uuid "github.com/satori/go.uuid" +) + +type JWTCustomClaims struct { + IdUser uuid.UUID `json:"user_id" binding:"required"` + jwt.RegisteredClaims +} + type AccountData struct { - UserID int - VerifyStatus string - ErrVerif error + IdUser uuid.UUID `json:"user_id" binding:"required"` } diff --git a/space/space/space/space/space/models/entities_model.go b/space/space/space/space/space/models/entities_model.go index 72b5decb9ca3f739ff5a4126c48682d10464ae34..fa3e1a9fafb2ff35bd09a2f950b709df698954be 100644 --- a/space/space/space/space/space/models/entities_model.go +++ b/space/space/space/space/space/models/entities_model.go @@ -7,10 +7,10 @@ import ( ) type Account struct { - ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` - Fingerprint string `gorm:"not null;"` - CreatedAt time.Time - DeletedAt *time.Time `gorm:"column:deleted_at"` // perhatikan penamaan kolom + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` + PassPhrase string `gorm:"not null;"` + CreatedAt time.Time + DeletedAt *time.Time `gorm:"column:deleted_at"` // perhatikan penamaan kolom } type ChatHistory struct { diff --git a/space/space/space/space/space/models/exception_model.go b/space/space/space/space/space/models/exception_model.go index 0394b70d9ba8cbcaef8ee1c7aa24afd7a8c9767b..1f5d0b964b5729f6d5516a803900e414dcfc564d 100644 --- a/space/space/space/space/space/models/exception_model.go +++ b/space/space/space/space/space/models/exception_model.go @@ -12,5 +12,5 @@ type Exception struct { ReplicateConnectionRefused bool `json:"replicated_connection_refused,omitempty"` AudioFileError bool `json:"audio_file_error,omitempty"` FailedGenerateAudio bool `json:"audio_generation_failed,omitempty"` - Message string `json:"message"` + Message string `json:"message,omitempty"` } diff --git a/space/space/space/space/space/models/http_response_dto_model.go b/space/space/space/space/space/models/http_response_dto_model.go index 3eaf40a116f2611f9c71969c2e262bd71823fca3..597e3cab2c295ba4e688b677131f98852a8899bf 100644 --- a/space/space/space/space/space/models/http_response_dto_model.go +++ b/space/space/space/space/space/models/http_response_dto_model.go @@ -2,7 +2,7 @@ package models type SuccessResponse struct { Status string `json:"status"` - Message string `json:"message"` + Message string `json:"message,omitempty"` Data any `json:"data"` MetaData any `json:"meta_data"` } diff --git a/space/space/space/space/space/repositories/account_repository.go b/space/space/space/space/space/repositories/account_repository.go index 33a3cf86356f4330f74f9b90cbfb4a21887cc1f8..311942c90c6dbe218600abb7542ae2aa6f99a9b3 100644 --- a/space/space/space/space/space/repositories/account_repository.go +++ b/space/space/space/space/space/repositories/account_repository.go @@ -4,26 +4,36 @@ import ( "context" "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "gorm.io/gorm" ) type AccountRepository interface { Repository CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) - GetAccountByFingerPrint(ctx context.Context, fingePrint string) (res models.Account) + GetAccountByPassPhrase(ctx context.Context, fingePrint string) (res models.Account) } type accountRepository struct { - repository[models.Account] + *repository[models.Account] } -func (r *accountRepository) CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) { - r.entity.Fingerprint = fingerPrint +func NewAccountRepository(db *gorm.DB) AccountRepository { + return &accountRepository{ + repository: &repository[models.Account]{ + entity: models.Account{}, + transaction: db, + }, + } +} + +func (r *accountRepository) CreateAccount(ctx context.Context, passPhrase string) (res models.Account) { + r.entity.PassPhrase = passPhrase r.Create(ctx) return r.entity } -func (r *accountRepository) GetAccountByFingerPrint(ctx context.Context, fingerPrint string) (res models.Account) { - r.entity.Fingerprint = fingerPrint +func (r *accountRepository) GetAccountByPassPhrase(ctx context.Context, passPhrase string) (res models.Account) { + r.entity.PassPhrase = passPhrase r.Where(ctx) r.Find(ctx, res) return res diff --git a/space/space/space/space/space/router/authentication_route.go b/space/space/space/space/space/router/authentication_route.go new file mode 100644 index 0000000000000000000000000000000000000000..89bb3f94c48eb2a68418a62d53964cc9682951f0 --- /dev/null +++ b/space/space/space/space/space/router/authentication_route.go @@ -0,0 +1,20 @@ +package router + +import ( + factory "github.com/abdanhafidz/ai-visual-multi-modal-backend/factory" + "github.com/gin-gonic/gin" +) + +func AuthenticationRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1") + { + routerGroup.POST("/register", func(c *gin.Context) { + authenticationModule := factory.NewAuthenticationModule() + authenticationModule.Register(c) + }) + routerGroup.POST("/login", func(c *gin.Context) { + authenticationModule := factory.NewAuthenticationModule() + authenticationModule.Login(c) + }) + } +} diff --git a/space/space/space/space/space/router/router.go b/space/space/space/space/space/router/router.go index dccd24c6236352640d3db082572a690ae04421f1..88df671b18801a73b766e7387373f351617712f9 100644 --- a/space/space/space/space/space/router/router.go +++ b/space/space/space/space/space/router/router.go @@ -10,5 +10,6 @@ func StartService() { router := gin.Default() router.GET("/", controller.HomeController) PredictionRoute(router) + AuthenticationRoute(router) router.Run(config.TCP_ADDRESS) } diff --git a/space/space/space/space/space/services/authentication_service.go b/space/space/space/space/space/services/authentication_service.go index 110569a15eeb725db5ebd2071566db2fae841f88..25448362dcdf19376679796eb8bd51d27d224e23 100644 --- a/space/space/space/space/space/services/authentication_service.go +++ b/space/space/space/space/space/services/authentication_service.go @@ -1,11 +1,76 @@ package services -import "context" +import ( + "context" + + "github.com/9ssi7/turnstile" + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) type AuthenticationService interface { - Register(ctx context.Context, fingerPrint string) - Login(ctx context.Context, fingerPrint string) + Service + Register(ctx context.Context, passPhrase string, turnstile string, ip string) string + Login(ctx context.Context, passPhrase string) string } type authenticationService struct { + *service[repositories.AccountRepository] + turnStileClient turnstile.Service + jwtService JWTService +} + +func NewAuthenticationService(accountRepository repositories.AccountRepository, turnStileClient turnstile.Service, jwtService JWTService) AuthenticationService { + return &authenticationService{ + service: &service[repositories.AccountRepository]{repository: accountRepository}, + turnStileClient: turnStileClient, + jwtService: jwtService, + } +} +func (s *authenticationService) Register(ctx context.Context, passPhrase string, turnstile string, ip string) string { + verifiedTurnStile, err := s.turnStileClient.Verify(ctx, turnstile, ip) + + if err != nil { + s.ThrowsException(&s.exception.Unauthorized, "Turnstile error!") + return "" + } + + if verifiedTurnStile { + account := s.repository.CreateAccount(ctx, passPhrase) + if s.ThrowsRepoException() { + return "" + } + token := s.jwtService.GenerateToken(ctx, models.JWTCustomClaims{IdUser: account.ID}) + if s.jwtService.Error() != nil { + s.ThrowsException(&s.exception.Unauthorized, "JWTService Error") + s.ThrowsError(s.jwtService.Error()) + return "" + } + + return token + } else { + s.ThrowsException(&s.exception.Unauthorized, "Invalid turnstile payload!") + } + + return "" +} + +func (s *authenticationService) Login(ctx context.Context, passPhrase string) string { + account := s.repository.GetAccountByPassPhrase(ctx, passPhrase) + if s.ThrowsRepoException() { + return "" + } + + if s.repository.IsNoRecord() { + s.ThrowsException(&s.exception.Unauthorized, "Account not found!") + return " " + } + + token := s.jwtService.GenerateToken(ctx, models.JWTCustomClaims{IdUser: account.ID}) + if s.jwtService.Error() != nil { + s.ThrowsException(&s.exception.Unauthorized, "JWTService Error") + s.ThrowsError(s.jwtService.Error()) + return "" + } + return token } diff --git a/space/space/space/space/space/services/jwt_service.go b/space/space/space/space/space/services/jwt_service.go new file mode 100644 index 0000000000000000000000000000000000000000..9f9052bd6742ea3e0cde12b82d3ec53d177ba12c --- /dev/null +++ b/space/space/space/space/space/services/jwt_service.go @@ -0,0 +1,68 @@ +package services + +import ( + "context" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/golang-jwt/jwt/v4" + uuid "github.com/satori/go.uuid" +) + +type JWTService interface { + Service + GenerateToken(ctx context.Context, payload models.JWTCustomClaims) string + ValidateToken(ctx context.Context, tokenStr string) *models.JWTCustomClaims +} + +type jwtService struct { + *service[repositories.AccountRepository] + secretKey string +} + +func NewJWTService(repo repositories.AccountRepository, secretKey string) JWTService { + return &jwtService{ + service: &service[repositories.AccountRepository]{repository: repo}, + secretKey: secretKey, + } +} +func (s *jwtService) GenerateToken(ctx context.Context, payload models.JWTCustomClaims) string { + claims := jwt.MapClaims{ + "user_id": payload.IdUser, + } + + token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) + tokenStr, err := token.SignedString([]byte(s.secretKey)) + if err != nil { + s.ThrowsException(&s.exception.Unauthorized, "Failed to generate JWT token!") + s.ThrowsError(err) + return "" + } + return tokenStr +} + +func (s *jwtService) ValidateToken(ctx context.Context, tokenStr string) *models.JWTCustomClaims { + token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { + s.ThrowsException(&s.exception.Unauthorized, "Unexpected signing method") + return nil, jwt.ErrSignatureInvalid + } + return []byte(s.secretKey), nil + }) + + if err != nil || !token.Valid { + s.ThrowsException(&s.exception.Unauthorized, "Invalid token!") + s.ThrowsError(err) + return nil + } + + claims, ok := token.Claims.(jwt.MapClaims) + if !ok { + s.ThrowsException(&s.exception.Unauthorized, "Invalid token claims") + return nil + } + + return &models.JWTCustomClaims{ + IdUser: claims["user_id"].(uuid.UUID), + } +} diff --git a/space/space/space/space/space/space/services/openai_service.go b/space/space/space/space/space/space/services/openai_service.go index 0315adfa7b048ec31e15d730f12141c499a2446d..28e576bfeebecabf5a14c30221a780c37a30a2af 100644 --- a/space/space/space/space/space/space/services/openai_service.go +++ b/space/space/space/space/space/space/services/openai_service.go @@ -61,7 +61,6 @@ func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.Fi req := openai.AudioRequest{ Model: openai.Whisper1, - Prompt: "please give it on summarized information", FilePath: savedPath, } diff --git a/space/space/space/space/space/space/space/controller/prediction_controller.go b/space/space/space/space/space/space/space/controller/prediction_controller.go index eca91df5c93d1c77a965c83af5e2a996094962ca..ccb5137323e62b0814e920232f1c1ff860733d1a 100644 --- a/space/space/space/space/space/space/space/controller/prediction_controller.go +++ b/space/space/space/space/space/space/space/controller/prediction_controller.go @@ -76,7 +76,13 @@ func NewPredictionController(predictionService services.PredictionService) Predi func (c *predictionController) Predict(ctx *gin.Context) { var predictionRequest models.PredictionRequest - + if err := ctx.ShouldBind(&predictionRequest); err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Invalid request format", + }) + return + } requestImage(ctx, &predictionRequest.ImageFile, &predictionRequest.ImageFileName) requestAudio(ctx, &predictionRequest.AudioQuestionFile, &predictionRequest.AudioQuestionFilename) diff --git a/space/space/space/space/space/space/space/services/openai_service.go b/space/space/space/space/space/space/space/services/openai_service.go index 28e576bfeebecabf5a14c30221a780c37a30a2af..0315adfa7b048ec31e15d730f12141c499a2446d 100644 --- a/space/space/space/space/space/space/space/services/openai_service.go +++ b/space/space/space/space/space/space/space/services/openai_service.go @@ -61,6 +61,7 @@ func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.Fi req := openai.AudioRequest{ Model: openai.Whisper1, + Prompt: "please give it on summarized information", FilePath: savedPath, } diff --git a/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml b/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml index 6d5e7c86f90be02acf8fa3a8221a1c28c721ddb3..d42ba340565eeeb49fb0ca8cf906ace50af5053b 100644 --- a/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml +++ b/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml @@ -1,9 +1,10 @@ name: Deploy to Huggingface on: - push: - branches: - - main + workflow_run: + workflows: ["Go Regression Testing"] + types: + - completed jobs: deploy-to-huggingface: diff --git a/space/space/space/space/space/space/space/space/.github/workflows/go.yml b/space/space/space/space/space/space/space/space/.github/workflows/go.yml index 0b443f376a6a3de600d8cde57dbb383a426e02f1..a9ebe7b721005583a51215a88425897bc9864f81 100644 --- a/space/space/space/space/space/space/space/space/.github/workflows/go.yml +++ b/space/space/space/space/space/space/space/space/.github/workflows/go.yml @@ -1,7 +1,4 @@ -# This workflow will build a golang project -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go - -name: Go +name: Go Regression Testing on: push: @@ -10,19 +7,37 @@ on: branches: [ "main" ] jobs: - build: runs-on: ubuntu-latest + steps: - uses: actions/checkout@v4 - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.24.0' + + - name: Create .env files from secrets + run: | + ENV_CONTENT="DB_HOST=${{ secrets.DB_HOST }} + DB_USER=${{ secrets.DB_USER }} + DB_PASSWORD=${{ secrets.DB_PASSWORD }} + DB_PORT=${{ secrets.DB_PORT }} + DB_NAME=${{ secrets.DB_NAME }} + SALT=akunakpacarCHINDOFineshyt + HOST_ADDRESS=localhost + HOST_PORT=8080 + LOG_PATH=logs + EMAIL_VERIFICATION_DURATION=2 + OPEN_AI_API_KEY=${{ secrets.OPEN_AI_API_KEY }} + REPLICATE_API_TOKEN=${{ secrets.REPLICATE_API_TOKEN }}" + + echo "$ENV_CONTENT" > .env + echo "$ENV_CONTENT" > tests/.env - name: Build run: go build -v ./... - name: Test - run: go test -v ./... + run: go test -v -timeout 300s ./tests diff --git a/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/Dockerfile index 0388ba888943aecc35b5c9d046caa0d080dd2a7b..b7c156148a96794ec43fe41dc9c4d0dc37651bcf 100644 --- a/space/space/space/space/space/space/space/space/Dockerfile +++ b/space/space/space/space/space/space/space/space/Dockerfile @@ -34,9 +34,9 @@ RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env # Buat direktori audio dan logs, beri izin dan kepemilikan ke appuser -RUN mkdir -p /app/audio /app/logs && \ - chmod -R 777 /app/audio /app/logs && \ - chown -R appuser:appuser /app/audio /app/logs +RUN mkdir -p /app/images /app/logs /app/audio && \ + chmod -R 777 /app/images /app/logs /app/audio && \ + chown -R appuser:appuser /app/images /app/logs /app/audio # Build aplikasi RUN go build -o main . diff --git a/space/space/space/space/space/space/space/space/config/config.go b/space/space/space/space/space/space/space/space/config/config.go index b43609852aae4f728948d1b52ce4f04efdbcdcad..279309a1e503f2423701b9d5415d493bc8ac9c66 100644 --- a/space/space/space/space/space/space/space/space/config/config.go +++ b/space/space/space/space/space/space/space/space/config/config.go @@ -1,27 +1,8 @@ package config -import ( - "os" - "strconv" - - "github.com/joho/godotenv" -) - -var TCP_ADDRESS string -var LOG_PATH string -var HOST_ADDRESS string -var HOST_PORT string -var EMAIL_VERIFICATION_DURATION int -var OPEN_AI_API_KEY string -var REPLICATE_API_KEY string - -func init() { - godotenv.Load() - HOST_ADDRESS = os.Getenv("HOST_ADDRESS") - HOST_PORT = os.Getenv("HOST_PORT") - TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT - LOG_PATH = os.Getenv("LOG_PATH") - EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) - OPEN_AI_API_KEY = os.Getenv("OPEN_AI_API_KEY") - REPLICATE_API_KEY = os.Getenv("REPLICATE_API_KEY") +func RunConfig() { + InitializeEnv() + InitializeDatabase() + InitializeOpenAIClient() + InitializeReplicateClient() } diff --git a/space/space/space/space/space/space/space/space/config/database_connection_config.go b/space/space/space/space/space/space/space/space/config/database_connection_config.go index 4cce339630d01c5c44454c9090f59b24442384d2..1ec07bd54e6a6ca0a3f40b6937235edaaa05cfa2 100644 --- a/space/space/space/space/space/space/space/space/config/database_connection_config.go +++ b/space/space/space/space/space/space/space/space/config/database_connection_config.go @@ -17,7 +17,7 @@ var DB *gorm.DB var err error var Salt string -func init() { +func InitializeDatabase() { godotenv.Load() if err != nil { fmt.Println("Gagal membaca file .env") diff --git a/space/space/space/space/space/space/space/space/config/env_config.go b/space/space/space/space/space/space/space/space/config/env_config.go new file mode 100644 index 0000000000000000000000000000000000000000..ffdc0ab332f3948ab86a1866c701747e5f2767e6 --- /dev/null +++ b/space/space/space/space/space/space/space/space/config/env_config.go @@ -0,0 +1,26 @@ +package config + +import ( + "os" + "strconv" + + "github.com/joho/godotenv" +) +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string +var EMAIL_VERIFICATION_DURATION int +var OPEN_AI_API_KEY string +var REPLICATE_API_KEY string + +func InitializeEnv() { + godotenv.Load() + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) + OPEN_AI_API_KEY = os.Getenv("OPEN_AI_API_KEY") + REPLICATE_API_KEY = os.Getenv("REPLICATE_API_KEY") +} \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/config/openai_client_config.go b/space/space/space/space/space/space/space/space/config/openai_client_config.go index f7c3350837b4535833422da3e99601672e72ca1a..05a281c58e2de568ea3923dff9307c45ca06298c 100644 --- a/space/space/space/space/space/space/space/space/config/openai_client_config.go +++ b/space/space/space/space/space/space/space/space/config/openai_client_config.go @@ -6,6 +6,6 @@ import ( var OpenAIClient *openai.Client -func init() { +func InitializeOpenAIClient() { OpenAIClient = openai.NewClient(OPEN_AI_API_KEY) } diff --git a/space/space/space/space/space/space/space/space/config/replicate_client_config.go b/space/space/space/space/space/space/space/space/config/replicate_client_config.go index ffde651d46984521d49a56fb0a2fd15ca3794c4a..daa7f41c12498e3cf00fa17f06577d910028831f 100644 --- a/space/space/space/space/space/space/space/space/config/replicate_client_config.go +++ b/space/space/space/space/space/space/space/space/config/replicate_client_config.go @@ -6,7 +6,7 @@ import ( var ReplicateClient *replicate.Client -func init() { +func InitializeReplicateClient() { ReplicateClient, err = replicate.NewClient(replicate.WithTokenFromEnv()) if err != nil { panic(err) diff --git a/space/space/space/space/space/space/space/space/factory/prediction_factory.go b/space/space/space/space/space/space/space/space/factory/prediction_factory.go index 84b4fe9e047dc8f1a6d84ce41d0ad21a3422d9fb..5cd922797dbc2b3e5c73d2ee0562fffdf3493671 100644 --- a/space/space/space/space/space/space/space/space/factory/prediction_factory.go +++ b/space/space/space/space/space/space/space/space/factory/prediction_factory.go @@ -8,11 +8,21 @@ import ( ) func NewPredictionModule() controller.PredictionController { - chatHistoryRepository := repositories.NewChatHistoryRepository() - openAIService := services.NewOpenAIService(chatHistoryRepository, config.OpenAIClient) - replicateService := services.NewReplicateService(chatHistoryRepository, config.ReplicateClient, "spuuntries/urna-kp3l:9338a4573a17178b70515c0ef2e613d3b4213e2dc860ef23b3ad6149dacadc1e") - predictionService := services.NewPredictionService(chatHistoryRepository, replicateService, openAIService) + chatHistoryRepository := repositories.NewChatHistoryRepository(config.DB) + openAIService := services.NewOpenAIService( + chatHistoryRepository, + config.OpenAIClient, + ) + replicateService := services.NewReplicateService( + chatHistoryRepository, + config.ReplicateClient, + "spuuntries/urna-kp3l:9338a4573a17178b70515c0ef2e613d3b4213e2dc860ef23b3ad6149dacadc1e", + ) + predictionService := services.NewPredictionService( + chatHistoryRepository, + replicateService, + openAIService, + ) predictionController := controller.NewPredictionController(predictionService) - return predictionController } diff --git a/space/space/space/space/space/space/space/space/images/foto-pacarku.jpg b/space/space/space/space/space/space/space/space/images/foto-pacarku.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1473f8a17a553287ac9b778dc1404b3bcc3cbcb7 Binary files /dev/null and b/space/space/space/space/space/space/space/space/images/foto-pacarku.jpg differ diff --git a/space/space/space/space/space/space/space/space/images/foto_pacarku.jpg b/space/space/space/space/space/space/space/space/images/foto_pacarku.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1473f8a17a553287ac9b778dc1404b3bcc3cbcb7 Binary files /dev/null and b/space/space/space/space/space/space/space/space/images/foto_pacarku.jpg differ diff --git a/space/space/space/space/space/space/space/space/logs/error_log.txt b/space/space/space/space/space/space/space/space/logs/error_log.txt index b33de5212bad0512839b807354f2476f7b333fe7..976547b0ee80302a09be26397dcdf2206c3085db 100644 --- a/space/space/space/space/space/space/space/space/logs/error_log.txt +++ b/space/space/space/space/space/space/space/space/logs/error_log.txt @@ -24,3 +24,11 @@ duplicated key not allowed; invalid transaction; invalid transaction; invalid tr 2025/06/21 21:27:10 Error Log : duplicated key not allowed; invalid transaction 2025/06/21 21:48:34 Error Log : duplicated key not allowed; invalid transaction duplicated key not allowed; invalid transaction; invalid transaction; invalid transaction +2025/06/22 18:15:55 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/22 18:16:19 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/22 18:19:01 Error Log : open ./images/foto-pacarku.jpg: The system cannot find the path specified. +2025/06/22 21:51:05 Error Log : error, status code: 400, status: 400 Bad Request, message: Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm'] +2025/06/22 22:06:00 Error Log : error, status code: 400, status: 400 Bad Request, message: Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm'] +2025/06/22 22:06:37 Error Log : error, status code: 400, status: 400 Bad Request, message: Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm'] +2025/06/23 15:09:33 Error Log : error, status code: 400, status: 400 Bad Request, message: Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm'] +2025/06/23 21:55:35 Error Log : error, status code: 400, status: 400 Bad Request, message: Invalid file format. Supported formats: ['flac', 'm4a', 'mp3', 'mp4', 'mpeg', 'mpga', 'oga', 'ogg', 'wav', 'webm'] diff --git a/space/space/space/space/space/space/space/space/main.go b/space/space/space/space/space/space/space/space/main.go index e624f9e394e284357398b6f8d107672bbb6ba7a3..e065b8447f82194ea4cbc0b1c8e7d5f6b6ab4755 100644 --- a/space/space/space/space/space/space/space/space/main.go +++ b/space/space/space/space/space/space/space/space/main.go @@ -8,6 +8,7 @@ import ( ) func main() { + config.RunConfig() fmt.Println("Server started on ", config.TCP_ADDRESS, ", port :", config.HOST_PORT) router.StartService() diff --git a/space/space/space/space/space/space/space/space/models/authentication_dto.go b/space/space/space/space/space/space/space/space/models/authentication_dto.go index 42184c0b1ce6b924b8796b1e1be7a5402e2fe806..338f6dab4a210f64b1b7377b5d2f9887ee09ced3 100644 --- a/space/space/space/space/space/space/space/space/models/authentication_dto.go +++ b/space/space/space/space/space/space/space/space/models/authentication_dto.go @@ -1,5 +1,6 @@ package models type LoginRequest struct { - FingerPrintToken string `json:"email" binding:"required"` + PassPhrase string `json:"pass_phrase binding:"required"` + TurnStile string `json:"turnstile_payload binding:"required"` } diff --git a/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go b/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go index c9359f92f3dfa628ca2453382fbf36d05f8ac863..944b9882904817d3cf51599ea4b9eed36acc1712 100644 --- a/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go +++ b/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go @@ -3,8 +3,8 @@ package repositories import ( "context" - "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "gorm.io/gorm" ) type ChatHistoryRepository interface { @@ -13,13 +13,14 @@ type ChatHistoryRepository interface { } type chatHistoryRepository struct { - repository[models.ChatHistory] + *repository[models.ChatHistory] } -func NewChatHistoryRepository() ChatHistoryRepository { +func NewChatHistoryRepository(db *gorm.DB) ChatHistoryRepository { return &chatHistoryRepository{ - repository: repository[models.ChatHistory]{ - transaction: config.DB.Begin(), + repository: &repository[models.ChatHistory]{ + entity: models.ChatHistory{}, + transaction: db, }, } } diff --git a/space/space/space/space/space/space/space/space/repositories/repository.go b/space/space/space/space/space/space/space/space/repositories/repository.go index be89cb89c5cfa0b9c1f4a4bbc3fb0f5c37f6c6cb..5c36e497d68a54ac5d089d7fddd62371a1755f7e 100644 --- a/space/space/space/space/space/space/space/space/repositories/repository.go +++ b/space/space/space/space/space/space/space/space/repositories/repository.go @@ -47,9 +47,6 @@ func (repo *repository[T1]) RowsCount() int { return repo.rowsCount } func (repo *repository[T1]) IsNoRecord() bool { - - repo.noRecord = repo.transaction.RowsAffected == 0 - return repo.noRecord } func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) { @@ -98,9 +95,15 @@ func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { func (repo *repository[T1]) Create(ctx context.Context) { - tx := repo.transaction.Create(&repo.entity) + tx := repo.transaction.WithContext(ctx).Create(&repo.entity) + tx.WithContext(ctx).First(&repo.entity) if tx.Error != nil { + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + repo.rowsError = repo.transaction.Error tx.Rollback() + return } repo.rowsCount = int(tx.RowsAffected) repo.noRecord = repo.rowsCount == 0 diff --git a/space/space/space/space/space/space/space/space/services/authentication_service.go b/space/space/space/space/space/space/space/space/services/authentication_service.go index a7f1b4d97bc419a05aa49bc4ef098959a0c38d3f..110569a15eeb725db5ebd2071566db2fae841f88 100644 --- a/space/space/space/space/space/space/space/space/services/authentication_service.go +++ b/space/space/space/space/space/space/space/space/services/authentication_service.go @@ -7,5 +7,5 @@ type AuthenticationService interface { Login(ctx context.Context, fingerPrint string) } -type authenticationService interface { +type authenticationService struct { } diff --git a/space/space/space/space/space/space/space/space/services/openai_service.go b/space/space/space/space/space/space/space/space/services/openai_service.go index 8ed8bfa12da250fe4268ff9b35abbe49ab268d0a..28e576bfeebecabf5a14c30221a780c37a30a2af 100644 --- a/space/space/space/space/space/space/space/space/services/openai_service.go +++ b/space/space/space/space/space/space/space/space/services/openai_service.go @@ -35,7 +35,6 @@ func NewOpenAIService(repo repositories.Repository, openAIClient *openai.Client) } func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.File, filename string) string { - audioDir := "audio" if err := os.MkdirAll(audioDir, os.ModePerm); err != nil { @@ -45,14 +44,13 @@ func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.Fi } savedPath := filepath.Join(audioDir, filepath.Base(filename)) - outFile, err := os.Create(savedPath) + outFile, err := os.Create(savedPath) if err != nil { s.ThrowsException(&s.exception.AudioFileError, "Failed to save audio!") s.ThrowsError(err) return "Failed to save audio!" } - defer outFile.Close() if _, err := io.Copy(outFile, audioFile); err != nil { @@ -72,15 +70,13 @@ func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.Fi s.ThrowsError(err) return "Failed to create transcription!" } - // if resp.Text != "" { - // outFile.Close() - // } else { - // s.ThrowsException(&s.exception.FailedTranscripting, "Failed to create transcription! [Nil text]") - // } - - // if err := os.Remove(savedPath); err != nil { - // s.ThrowsError(err) - // } + if resp.Text != "" { + // Hapus file audio setelah transkripsi berhasil + outFile.Close() + if err := os.Remove(savedPath); err != nil { + s.ThrowsError(err) + } + } return resp.Text } diff --git a/space/space/space/space/space/space/space/space/services/prediction_service.go b/space/space/space/space/space/space/space/space/services/prediction_service.go index 1f4a531403d4a1675cd9a6ddc65ff6988838a445..ec2c4cd976e244ad5142f081cd1a0575a2737714 100644 --- a/space/space/space/space/space/space/space/space/services/prediction_service.go +++ b/space/space/space/space/space/space/space/space/services/prediction_service.go @@ -53,9 +53,10 @@ func (s *predictionService) Predict(ctx context.Context, req models.PredictionRe s.ThrowsError(s.openAIService.Error()) return nil, "" } - // savePrediction := s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) - // if s.ThrowsRepoException() { - // return nil, "" - // } + + s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) + if s.ThrowsRepoException() { + return nil, "" + } return ttsOutput, replicateOutput } diff --git a/space/space/space/space/space/space/space/space/services/replicate_service.go b/space/space/space/space/space/space/space/space/services/replicate_service.go index cfe409abb530b4595e58ed0b66010993eeddb94c..2b116dc24298f782e0f99b13d238e000a5e583c3 100644 --- a/space/space/space/space/space/space/space/space/services/replicate_service.go +++ b/space/space/space/space/space/space/space/space/services/replicate_service.go @@ -1,11 +1,11 @@ package services import ( - "bytes" "context" "fmt" "io" "mime/multipart" + "os" "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" "github.com/replicate/replicate-go" @@ -31,20 +31,32 @@ func NewReplicateService(repo repositories.Repository, replicateClient *replicat return &service } -func (s *replicateService) AskImage(ctx context.Context, imageFile multipart.File, filename, question string) string { - var buf bytes.Buffer - if _, err := io.Copy(&buf, imageFile); err != nil { +func (s *replicateService) AskImage(ctx context.Context, imageFile multipart.File, filename string, question string) string { + // Buat path file lokal + filePath := fmt.Sprintf("./images/%s", filename) + // Simpan file ke direktori ./images + outFile, err := os.Create(filePath) + if err != nil { + s.ThrowsError(err) + return "" + } + defer outFile.Close() + + // Salin data dari multipart.File ke file lokal + if _, err := io.Copy(outFile, imageFile); err != nil { s.ThrowsError(err) return "" } - file, err := s.client.CreateFileFromBuffer(ctx, &buf, &replicate.CreateFileOptions{Filename: filename}) + // Gunakan path file untuk membuat file di Replicate + file, err := s.client.CreateFileFromPath(ctx, filePath, &replicate.CreateFileOptions{Filename: filename}) if err != nil { s.ThrowsError(err) return "" } + // Buat input untuk prediksi input := replicate.PredictionInput{ "image": file, "question": question, @@ -54,21 +66,18 @@ func (s *replicateService) AskImage(ctx context.Context, imageFile multipart.Fil s.ThrowsError(err) return "" } - fmt.Println("Output slice", rawOutput) + + // Parsing output outputSlice, ok := rawOutput.([]interface{}) var result string + if ok { result = fmt.Sprintf("%v", outputSlice) - fmt.Println("Output slice", result) + // fmt.Println("Output slice", result) } else { result = fmt.Sprintf("%v", rawOutput) - fmt.Println("Output slice", result) + // fmt.Println("Output raw", result) } - // if !ok { - // s.ThrowsError(errors.New("failed to parse output as string")) - // return "" - // } - return result } diff --git a/space/space/space/space/space/space/space/space/services/service.go b/space/space/space/space/space/space/space/space/services/service.go index 7cb37fbb35497c0bf956cd43ba836231f25c6295..f5d5e27053a682009d355ff99558877ae3c005cc 100644 --- a/space/space/space/space/space/space/space/space/services/service.go +++ b/space/space/space/space/space/space/space/space/services/service.go @@ -53,16 +53,16 @@ func (s *service[TRepo]) ThrowsRepoException() bool { s.ThrowsException(&s.exception.QueryError, "Database error!") s.ThrowsError(s.repository.RowsError()) - return true + } + if errors.Is(s.repository.RowsError(), gorm.ErrDuplicatedKey) { s.ThrowsException(&s.exception.DataDuplicate, "Duplicated data!") return true } if s.repository.IsNoRecord() { - s.ThrowsException(&s.exception.DataNotFound, "No record found") return true } diff --git a/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go b/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go index 4cfca725d77199de1a81575c4b3d37731e3e0392..84b4fe9e047dc8f1a6d84ce41d0ad21a3422d9fb 100644 --- a/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go +++ b/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go @@ -10,7 +10,7 @@ import ( func NewPredictionModule() controller.PredictionController { chatHistoryRepository := repositories.NewChatHistoryRepository() openAIService := services.NewOpenAIService(chatHistoryRepository, config.OpenAIClient) - replicateService := services.NewReplicateService(chatHistoryRepository, config.ReplicateClient, "lucataco/moondream2:72ccb656353c348c1385df54b237eeb7bfa874bf11486cf0b9473e691b662d31") + replicateService := services.NewReplicateService(chatHistoryRepository, config.ReplicateClient, "spuuntries/urna-kp3l:9338a4573a17178b70515c0ef2e613d3b4213e2dc860ef23b3ad6149dacadc1e") predictionService := services.NewPredictionService(chatHistoryRepository, replicateService, openAIService) predictionController := controller.NewPredictionController(predictionService) diff --git a/space/space/space/space/space/space/space/space/space/space/.env.example b/space/space/space/space/space/space/space/space/space/space/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..6b4970eeb0f5d9b08b1ce4b8b800661ac6b0f490 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/.env.example @@ -0,0 +1,11 @@ +DB_HOST = +DB_USER = +DB_PASSWORD = +DB_PORT = +DB_NAME = +SALT = +HOST_ADDRESS = +HOST_PORT = +LOG_PATH = logs +OPEN_AI_API_KEY = +REPLICATE_API_TOKEN = \ No newline at end of file diff --git a/space/space/space/space/space/space/space/space/space/space/.gitattributes b/space/space/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/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/space/space/.github/workflows/deploy.yml b/space/space/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml new file mode 100644 index 0000000000000000000000000000000000000000..6d5e7c86f90be02acf8fa3a8221a1c28c721ddb3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/.github/workflows/deploy.yml @@ -0,0 +1,52 @@ +name: Deploy to 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/urna-backend 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/urna-backend + 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/space/space/space/.github/workflows/go.yml b/space/space/space/space/space/space/space/space/space/space/.github/workflows/go.yml new file mode 100644 index 0000000000000000000000000000000000000000..0b443f376a6a3de600d8cde57dbb383a426e02f1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/.github/workflows/go.yml @@ -0,0 +1,28 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.20' + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/space/space/space/space/space/space/space/space/space/space/.gitignore b/space/space/space/space/space/space/space/space/space/space/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..df8cf1c15e2917803db7f00fbc386495fe8f5479 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/.gitignore @@ -0,0 +1,5 @@ +.env +vendor/ +quzuu-be.exe +README.md +.qodo diff --git a/space/space/space/space/space/space/space/space/space/space/Dockerfile b/space/space/space/space/space/space/space/space/space/space/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..0388ba888943aecc35b5c9d046caa0d080dd2a7b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/Dockerfile @@ -0,0 +1,51 @@ +# Gunakan image dasar Golang versi 1.24.1 +FROM golang:1.24.1 + +# Tambahkan user non-root untuk keamanan (optional tapi best practice) +RUN useradd -m -u 1001 appuser + +# 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 menggunakan Hugging Face secrets +RUN --mount=type=secret,id=DB_PASSWORD,mode=0444,required=false \ + --mount=type=secret,id=OPENAI_API_KEY,mode=0444,required=false \ + --mount=type=secret,id=REPLICATE_API_TOKEN,mode=0444,required=false \ + echo "DB_HOST=aws-0-ap-southeast-1.pooler.supabase.com" >> .env && \ + echo "DB_USER=postgres.iuwuiuoisqnfdzlgwurl" >> .env && \ + echo "DB_PASSWORD=$(cat /run/secrets/DB_PASSWORD 2>/dev/null)" >> .env && \ + echo "DB_PORT=5432" >> .env && \ + echo "DB_NAME=postgres" >> .env && \ + echo "SALT=NZNZtY7dNPz8l0dWINJZLKafWaJrql1s" >> .env && \ + echo "HOST_ADDRESS=0.0.0.0" >> .env && \ + echo "HOST_PORT=7860" >> .env && \ + echo "LOG_PATH=logs" >> .env && \ + echo "EMAIL_VERIFICATION_DURATION=2" >> .env && \ + echo "OPEN_AI_API_KEY=$(cat /run/secrets/OPENAI_API_KEY 2>/dev/null)" >> .env && \ + echo "REPLICATE_API_TOKEN=$(cat /run/secrets/REPLICATE_API_TOKEN 2>/dev/null)" >> .env + +# Buat direktori audio dan logs, beri izin dan kepemilikan ke appuser +RUN mkdir -p /app/audio /app/logs && \ + chmod -R 777 /app/audio /app/logs && \ + chown -R appuser:appuser /app/audio /app/logs + +# Build aplikasi +RUN go build -o main . + +# Beralih ke user non-root +USER appuser + +# Expose port untuk Hugging Face Spaces +EXPOSE 7860 + +# Jalankan aplikasi +CMD ["./main"] diff --git a/space/space/space/space/space/space/space/space/space/space/LICENSE b/space/space/space/space/space/space/space/space/space/space/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..79da4b69760d1658f4dbc42604187316b368d857 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2025 Abdan Hafidz + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 b/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..d6ec250b2eeedbdd36b90c6c1a2a7875bcd00f02 Binary files /dev/null and b/space/space/space/space/space/space/space/space/space/space/audio/20250616_235223.mp3 differ diff --git a/space/space/space/space/space/space/space/space/space/space/config/config.go b/space/space/space/space/space/space/space/space/space/space/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..b43609852aae4f728948d1b52ce4f04efdbcdcad --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/config/config.go @@ -0,0 +1,27 @@ +package config + +import ( + "os" + "strconv" + + "github.com/joho/godotenv" +) + +var TCP_ADDRESS string +var LOG_PATH string +var HOST_ADDRESS string +var HOST_PORT string +var EMAIL_VERIFICATION_DURATION int +var OPEN_AI_API_KEY string +var REPLICATE_API_KEY string + +func init() { + godotenv.Load() + HOST_ADDRESS = os.Getenv("HOST_ADDRESS") + HOST_PORT = os.Getenv("HOST_PORT") + TCP_ADDRESS = HOST_ADDRESS + ":" + HOST_PORT + LOG_PATH = os.Getenv("LOG_PATH") + EMAIL_VERIFICATION_DURATION, _ = strconv.Atoi(os.Getenv("EMAIL_VERIFICATION_DURATION")) + OPEN_AI_API_KEY = os.Getenv("OPEN_AI_API_KEY") + REPLICATE_API_KEY = os.Getenv("REPLICATE_API_KEY") +} diff --git a/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go b/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go new file mode 100644 index 0000000000000000000000000000000000000000..4cce339630d01c5c44454c9090f59b24442384d2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/config/database_connection_config.go @@ -0,0 +1,61 @@ +package config + +import ( + "fmt" + "log" + "os" + + "gorm.io/driver/postgres" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/joho/godotenv" +) + +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) +} + +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.ChatHistory{}, + ) + + if err != nil { + log.Fatal(err) + } + + fmt.Println("Migration completed successfully.") +} diff --git a/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go b/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go new file mode 100644 index 0000000000000000000000000000000000000000..f7c3350837b4535833422da3e99601672e72ca1a --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/config/openai_client_config.go @@ -0,0 +1,11 @@ +package config + +import ( + "github.com/sashabaranov/go-openai" +) + +var OpenAIClient *openai.Client + +func init() { + OpenAIClient = openai.NewClient(OPEN_AI_API_KEY) +} diff --git a/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go b/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go new file mode 100644 index 0000000000000000000000000000000000000000..ffde651d46984521d49a56fb0a2fd15ca3794c4a --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/config/replicate_client_config.go @@ -0,0 +1,14 @@ +package config + +import ( + "github.com/replicate/replicate-go" +) + +var ReplicateClient *replicate.Client + +func init() { + ReplicateClient, err = replicate.NewClient(replicate.WithTokenFromEnv()) + if err != nil { + panic(err) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/controller/controller.go b/space/space/space/space/space/space/space/space/space/space/controller/controller.go new file mode 100644 index 0000000000000000000000000000000000000000..0fa1dfc466312fd712e3b2353e3ea744f200630b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/controller/controller.go @@ -0,0 +1,64 @@ +package controller + +import ( + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" +) + +type ( + Controller interface { + HeaderParse(ctx *gin.Context) + RequestJSON(ctx *gin.Context, request any) + Response(ctx *gin.Context, res any) + } + controller[TService services.Service] struct { + accountData models.AccountData + service TService + } +) + +func (c *controller[TService]) HeaderParse(ctx *gin.Context) { + cParam, _ := ctx.Get("account_data") + if cParam != nil { + c.accountData = cParam.(models.AccountData) + } +} + +func (c *controller[TService]) RequestJSON(ctx *gin.Context, request any) { + cParam, _ := ctx.Get("AccountData") + if cParam != nil { + c.accountData = cParam.(models.AccountData) + } + + errBinding := ctx.ShouldBindJSON(&request) + if errBinding != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Invalid Request!, recheck your request, there's must be some problem about required parameter or type parameter", + }) + return + } +} + +func (c *controller[TService]) Response(ctx *gin.Context, res any) { + switch { + case c.service.Error() != nil: + utils.ResponseFAIL(ctx, 500, models.Exception{ + InternalServerError: true, + Message: "Internal Server Error", + }) + utils.LogError(c.service.Error()) + case c.service.Exception().DataDuplicate: + utils.ResponseFAIL(ctx, 400, c.service.Exception()) + case c.service.Exception().Unauthorized: + utils.ResponseFAIL(ctx, 401, c.service.Exception()) + case c.service.Exception().DataNotFound: + utils.ResponseFAIL(ctx, 404, c.service.Exception()) + case c.service.Exception().Message != "": + utils.ResponseFAIL(ctx, 400, c.service.Exception()) + default: + utils.ResponseOK(ctx, res) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/controller/home_controller.go b/space/space/space/space/space/space/space/space/space/space/controller/home_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..60198b440a4795562848b532dc3480e3128a7729 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/controller/home_controller.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 Is Running Gladly!", + }) +} diff --git a/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go b/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go new file mode 100644 index 0000000000000000000000000000000000000000..eca91df5c93d1c77a965c83af5e2a996094962ca --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/controller/prediction_controller.go @@ -0,0 +1,94 @@ +package controller + +import ( + "fmt" + "mime/multipart" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" +) + +type ( + PredictionController interface { + Controller + Predict(ctx *gin.Context) + } + + predictionController struct { + *controller[services.PredictionService] + } +) + +func requestImage(ctx *gin.Context, image *multipart.File, imageFilename *string) { + imageHeader, err := ctx.FormFile("image_file") + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Image file is required", + }) + return + } + imageFile, err := imageHeader.Open() + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Failed to open image file", + }) + return + } + *image = imageFile + *imageFilename = imageHeader.Filename + defer imageFile.Close() +} + +func requestAudio(ctx *gin.Context, audio *multipart.File, audioFilename *string) { + audioHeader, err := ctx.FormFile("audio_file") + fmt.Println(audioHeader.Filename) + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Audio file is required", + }) + return + } + audioFile, err := audioHeader.Open() + if err != nil { + utils.ResponseFAIL(ctx, 400, models.Exception{ + BadRequest: true, + Message: "Failed to open audio file", + }) + return + } + *audio = audioFile + *audioFilename = audioHeader.Filename + defer audioFile.Close() +} + +func NewPredictionController(predictionService services.PredictionService) PredictionController { + return &predictionController{ + controller: &controller[services.PredictionService]{ + service: predictionService, + }, + } +} +func (c *predictionController) Predict(ctx *gin.Context) { + + var predictionRequest models.PredictionRequest + + requestImage(ctx, &predictionRequest.ImageFile, &predictionRequest.ImageFileName) + requestAudio(ctx, &predictionRequest.AudioQuestionFile, &predictionRequest.AudioQuestionFilename) + + predictionResult, text_output := c.service.Predict(ctx.Request.Context(), predictionRequest) + + if c.service.Error() != nil { + c.Response(ctx, nil) + return + } + + ctx.Header("Content-Type", "audio/mpeg") + ctx.Header("Content-Disposition", "inline; filename=response.mp3") + ctx.Header("X-Response-Text", text_output) + ctx.Data(200, "audio/mpeg", predictionResult) +} diff --git a/space/space/space/space/space/space/space/space/space/space/docker-compose.yaml b/space/space/space/space/space/space/space/space/space/space/docker-compose.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/space/space/space/space/space/space/space/space/space/space/factory/factory.go b/space/space/space/space/space/space/space/space/space/space/factory/factory.go new file mode 100644 index 0000000000000000000000000000000000000000..4682f9f75538c0f31e2fd022c6efa25cad45ed2e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/factory/factory.go @@ -0,0 +1 @@ +package factory diff --git a/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go b/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go new file mode 100644 index 0000000000000000000000000000000000000000..4cfca725d77199de1a81575c4b3d37731e3e0392 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/factory/prediction_factory.go @@ -0,0 +1,18 @@ +package factory + +import ( + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/controller" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" +) + +func NewPredictionModule() controller.PredictionController { + chatHistoryRepository := repositories.NewChatHistoryRepository() + openAIService := services.NewOpenAIService(chatHistoryRepository, config.OpenAIClient) + replicateService := services.NewReplicateService(chatHistoryRepository, config.ReplicateClient, "lucataco/moondream2:72ccb656353c348c1385df54b237eeb7bfa874bf11486cf0b9473e691b662d31") + predictionService := services.NewPredictionService(chatHistoryRepository, replicateService, openAIService) + predictionController := controller.NewPredictionController(predictionService) + + return predictionController +} diff --git a/space/space/space/space/space/space/space/space/space/space/go.mod b/space/space/space/space/space/space/space/space/space/space/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..fd16864743e8c7e35500854f5cad1cfbeddd8726 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/go.mod @@ -0,0 +1,53 @@ +module github.com/abdanhafidz/ai-visual-multi-modal-backend + +go 1.24.0 + +require ( + github.com/dgrijalva/jwt-go v3.2.0+incompatible + github.com/gin-gonic/gin v1.10.0 + github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/joho/godotenv v1.5.1 + github.com/satori/go.uuid v1.2.0 + golang.org/x/crypto v0.36.0 + gorm.io/driver/postgres v1.5.11 + gorm.io/gorm v1.25.12 +) + +require ( + github.com/bytedance/sonic v1.13.1 // indirect + github.com/bytedance/sonic/loader v0.2.4 // indirect + github.com/cloudwego/base64x v0.1.5 // indirect + github.com/gabriel-vasile/mimetype v1.4.8 // indirect + github.com/gin-contrib/sse v1.0.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.5 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect + github.com/jackc/puddle/v2 v2.2.2 // 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.10 // 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.2.3 // indirect + github.com/replicate/replicate-go v0.26.0 // indirect + github.com/rogpeppe/go-internal v1.11.0 // indirect + github.com/sashabaranov/go-openai v1.40.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.12 // indirect + github.com/vincent-petithory/dataurl v1.0.0 // indirect + golang.org/x/arch v0.15.0 // indirect + golang.org/x/net v0.37.0 // indirect + golang.org/x/sync v0.12.0 // indirect + golang.org/x/sys v0.31.0 // indirect + golang.org/x/text v0.23.0 // indirect + google.golang.org/protobuf v1.36.5 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/space/space/space/space/space/space/space/space/space/space/go.sum b/space/space/space/space/space/space/space/space/space/space/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..56ea54f781eb5a01a1a3b54a84e872d795922a0e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/go.sum @@ -0,0 +1,126 @@ +github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g= +github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY= +github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= +github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= +github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +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.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= +github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E= +github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0= +github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= +github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= +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.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.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +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-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/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.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE= +github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +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.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.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= +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/replicate/replicate-go v0.26.0 h1:F6XceIkO0x2ft08mc9MdNJSNbkXDqEtOK9GsgjqHQeQ= +github.com/replicate/replicate-go v0.26.0/go.mod h1:mnRw0hsQuVrgWKMm/kP29pY6Ldn//79b4C2Nw9sYn5M= +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/sashabaranov/go-openai v1.40.1 h1:bJ08Iwct5mHBVkuvG6FEcb9MDTfsXdTYPGjYLRdeTEU= +github.com/sashabaranov/go-openai v1.40.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= +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= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +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.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +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.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= +github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +github.com/vincent-petithory/dataurl v1.0.0 h1:cXw+kPto8NLuJtlMsI152irrVw9fRDX8AbShPRpg2CI= +github.com/vincent-petithory/dataurl v1.0.0/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U= +golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw= +golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= +golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= +golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw= +golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= +golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= +golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +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.36.5 h1:tPhr+woSbjfYvY6/GPufUoYizxw1cF/yFoxJ2fmpwlM= +google.golang.org/protobuf v1.36.5/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +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.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314= +gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= +gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= diff --git a/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt b/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt new file mode 100644 index 0000000000000000000000000000000000000000..b33de5212bad0512839b807354f2476f7b333fe7 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/logs/error_log.txt @@ -0,0 +1,26 @@ +2025/06/16 23:53:52 Error Log : open audio: is a directory +2025/06/16 23:54:20 Error Log : open audio: is a directory +open audio: is a directory +open audio: is a directory +2025/06/16 23:54:37 Error Log : open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +open audio: is a directory +2025/06/16 23:56:46 Error Log : open audio: is a directory +2025/06/16 23:57:43 Error Log : open audio: is a directory +2025/06/17 00:00:14 Error Log : open audio: is a directory +2025/06/17 00:01:06 Error Log : error, status code: 429, status: 429 Too Many Requests, message: You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors. +2025/06/17 00:16:55 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/17 00:29:21 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:30:56 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:32:14 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/17 00:33:03 Error Log : error, status code: 400, status: 400 Bad Request, message: [{'type': 'string_too_short', 'loc': ('body', 'input'), 'msg': 'String should have at least 1 character', 'input': '', 'ctx': {'min_length': 1}}] +2025/06/21 20:30:08 Error Log : remove audio\20250616_235223.mp3: The process cannot access the file because it is being used by another process. +2025/06/21 21:21:08 Error Log : duplicated key not allowed; invalid transaction +2025/06/21 21:23:54 Error Log : duplicated key not allowed; invalid transaction +duplicated key not allowed; invalid transaction; invalid transaction; invalid transaction +2025/06/21 21:27:10 Error Log : duplicated key not allowed; invalid transaction +2025/06/21 21:48:34 Error Log : duplicated key not allowed; invalid transaction +duplicated key not allowed; invalid transaction; invalid transaction; invalid transaction diff --git a/space/space/space/space/space/space/space/space/space/space/logs/security_log.txt b/space/space/space/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/space/space/space/main.go b/space/space/space/space/space/space/space/space/space/space/main.go new file mode 100644 index 0000000000000000000000000000000000000000..e624f9e394e284357398b6f8d107672bbb6ba7a3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + router "github.com/abdanhafidz/ai-visual-multi-modal-backend/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/space/space/space/middleware/authentication_middleware.go b/space/space/space/space/space/space/space/space/space/space/middleware/authentication_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..d23e075a9b088fdd26e7dd964f826b6d3ac627fb --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/middleware/authentication_middleware.go @@ -0,0 +1,70 @@ +// auth/auth.go + +package middleware + +import ( + "time" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + utils "github.com/abdanhafidz/ai-visual-multi-modal-backend/utils" + "github.com/gin-gonic/gin" + "github.com/golang-jwt/jwt/v5" +) + +var salt = config.Salt +var secretKey = []byte(salt) + +// VerifyPassword verifies if the provided password matches the hashed password + +type CustomClaims struct { + jwt.RegisteredClaims + UserID 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 + } + + // Extract the claims + claims, ok := token.Claims.(*CustomClaims) + if !ok || !token.Valid { + return 0, "invalid-token", err + } + if claims.ExpiresAt != nil && claims.ExpiresAt.Time.Before(time.Now()) { + return 0, "expired", err + } + + return claims.UserID, "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.UserID, currAccData.VerifyStatus, currAccData.ErrVerif = VerifyToken(token[0]) + // fmt.Println("Verify Status :", currAccData.verifyStatus) + if currAccData.VerifyStatus == "invalid-token" || currAccData.VerifyStatus == "expired" { + currAccData.UserID = 0 + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "Your session is expired, Please re-Login!"}) + c.Abort() + return + } else { + c.Set("accountData", currAccData) + c.Next() + } + } else { + currAccData.UserID = 0 + currAccData.VerifyStatus = "no-token" + currAccData.ErrVerif = nil + utils.ResponseFAIL(c, 401, models.Exception{Unauthorized: true, Message: "You have to login first!"}) + c.Abort() + return + } + +} diff --git a/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go b/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..cce0f4f3e125d37ff4feb157c980c9a9f14f4673 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/middleware/middleware.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "math" + "time" + + "gorm.io/gorm" +) + +func RecordCheck(rows *gorm.DB) (string, error) { + count := rows.RowsAffected + err := rows.Error + + 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/space/space/space/middleware/response_middleware.go b/space/space/space/space/space/space/space/space/space/space/middleware/response_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..d52391cd3607e6ec2697e6a5813b348d49ff5ee2 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/middleware/response_middleware.go @@ -0,0 +1,45 @@ +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}) + return +} + +// 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}) + return +} + +// 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}) + return +} + +// 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}) + return +} + +// 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}) + return +} + +// 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}) + return +} + +// JSONResponseMiddleware is a middleware that provides functions for sending JSON responses diff --git a/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go b/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go new file mode 100644 index 0000000000000000000000000000000000000000..42184c0b1ce6b924b8796b1e1be7a5402e2fe806 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/authentication_dto.go @@ -0,0 +1,5 @@ +package models + +type LoginRequest struct { + FingerPrintToken string `json:"email" binding:"required"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go b/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go new file mode 100644 index 0000000000000000000000000000000000000000..0203b9b340d066705491edf66830674602f89655 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/authentication_payload_model.go @@ -0,0 +1,7 @@ +package models + +type AccountData struct { + UserID int + VerifyStatus string + ErrVerif error +} diff --git a/space/space/space/space/space/space/space/space/space/space/models/entities_model.go b/space/space/space/space/space/space/space/space/space/space/models/entities_model.go new file mode 100644 index 0000000000000000000000000000000000000000..72b5decb9ca3f739ff5a4126c48682d10464ae34 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/entities_model.go @@ -0,0 +1,27 @@ +package models + +import ( + "time" + + uuid "github.com/satori/go.uuid" +) + +type Account struct { + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` + Fingerprint string `gorm:"not null;"` + CreatedAt time.Time + DeletedAt *time.Time `gorm:"column:deleted_at"` // perhatikan penamaan kolom +} + +type ChatHistory struct { + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey"` + ImagePath string `gorm:"type:text"` + Question string `gorm:"type:text"` + Answer string `gorm:"type:text"` + CreatedAt time.Time `gorm:"column:created_at"` + DeletedAt *time.Time `gorm:"column:deleted_at"` +} + +// Gorm table name settings +func (Account) TableName() string { return "account" } +func (ChatHistory) TableName() string { return "chat_history" } diff --git a/space/space/space/space/space/space/space/space/space/space/models/exception_model.go b/space/space/space/space/space/space/space/space/space/space/models/exception_model.go new file mode 100644 index 0000000000000000000000000000000000000000..0394b70d9ba8cbcaef8ee1c7aa24afd7a8c9767b --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/exception_model.go @@ -0,0 +1,16 @@ +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"` + InvalidPasswordLength bool `json:"invalid_password_length,omitempty"` + FailedTranscripting bool `json:"failed_transcripting,omitempty"` + ReplicateConnectionRefused bool `json:"replicated_connection_refused,omitempty"` + AudioFileError bool `json:"audio_file_error,omitempty"` + FailedGenerateAudio bool `json:"audio_generation_failed,omitempty"` + Message string `json:"message"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go b/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go new file mode 100644 index 0000000000000000000000000000000000000000..3eaf40a116f2611f9c71969c2e262bd71823fca3 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/http_response_dto_model.go @@ -0,0 +1,15 @@ +package models + +type SuccessResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Data any `json:"data"` + MetaData any `json:"meta_data"` +} + +type ErrorResponse struct { + Status string `json:"status"` + Message string `json:"message"` + Errors Exception `json:"errors"` + MetaData any `json:"meta_data"` +} diff --git a/space/space/space/space/space/space/space/space/space/space/models/model.go b/space/space/space/space/space/space/space/space/space/space/models/model.go new file mode 100644 index 0000000000000000000000000000000000000000..a68179cd2c301ae0e474db227fec2841ea200734 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/model.go @@ -0,0 +1,3 @@ +package models + + diff --git a/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go b/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go new file mode 100644 index 0000000000000000000000000000000000000000..96e18bcbc7f6a8b6e3991e6c3c76e20e14c1634e --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/models/prediction_dto_model.go @@ -0,0 +1,10 @@ +package models + +import "mime/multipart" + +type PredictionRequest struct { + ImageFile multipart.File + ImageFileName string + AudioQuestionFile multipart.File + AudioQuestionFilename string +} diff --git a/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go b/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..33a3cf86356f4330f74f9b90cbfb4a21887cc1f8 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/repositories/account_repository.go @@ -0,0 +1,30 @@ +package repositories + +import ( + "context" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" +) + +type AccountRepository interface { + Repository + CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) + GetAccountByFingerPrint(ctx context.Context, fingePrint string) (res models.Account) +} + +type accountRepository struct { + repository[models.Account] +} + +func (r *accountRepository) CreateAccount(ctx context.Context, fingerPrint string) (res models.Account) { + r.entity.Fingerprint = fingerPrint + r.Create(ctx) + return r.entity +} + +func (r *accountRepository) GetAccountByFingerPrint(ctx context.Context, fingerPrint string) (res models.Account) { + r.entity.Fingerprint = fingerPrint + r.Where(ctx) + r.Find(ctx, res) + return res +} diff --git a/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go b/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go new file mode 100644 index 0000000000000000000000000000000000000000..c9359f92f3dfa628ca2453382fbf36d05f8ac863 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/repositories/chat_history_repository.go @@ -0,0 +1,32 @@ +package repositories + +import ( + "context" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" +) + +type ChatHistoryRepository interface { + Repository + SaveChatHistory(ctx context.Context, imagePath string, question string, answer string) (res models.ChatHistory) +} + +type chatHistoryRepository struct { + repository[models.ChatHistory] +} + +func NewChatHistoryRepository() ChatHistoryRepository { + return &chatHistoryRepository{ + repository: repository[models.ChatHistory]{ + transaction: config.DB.Begin(), + }, + } +} +func (r *chatHistoryRepository) SaveChatHistory(ctx context.Context, imagePath string, question string, answer string) (res models.ChatHistory) { + r.entity.ImagePath = imagePath + r.entity.Question = question + r.entity.Answer = answer + r.Create(ctx) + return r.entity +} diff --git a/space/space/space/space/space/space/space/space/space/space/repositories/repository.go b/space/space/space/space/space/space/space/space/space/space/repositories/repository.go new file mode 100644 index 0000000000000000000000000000000000000000..be89cb89c5cfa0b9c1f4a4bbc3fb0f5c37f6c6cb --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/repositories/repository.go @@ -0,0 +1,151 @@ +package repositories + +import ( + "context" + + "gorm.io/gorm" +) + +type Repository interface { + Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) + FindAllPaginate(ctx context.Context, res any) + Where(ctx context.Context) + Find(ctx context.Context, res any) + Create(ctx context.Context) + Update(ctx context.Context) + Query(ctx context.Context, res any) + Delete(ctx context.Context) + IsNoRecord() bool + RowsCount() int + RowsError() error +} +type PaginationConstructor struct { + limit int + offset int + filter string +} + +type CustomQueryConstructor struct { + sql string + values interface{} +} + +type repository[TEntity any] struct { + entity TEntity + pagination PaginationConstructor + customQuery CustomQueryConstructor + transaction *gorm.DB + rowsCount int + noRecord bool + rowsError error +} + +func (repo *repository[T1]) RowsError() error { + return repo.rowsError +} +func (repo *repository[T1]) RowsCount() int { + return repo.rowsCount +} +func (repo *repository[T1]) IsNoRecord() bool { + + repo.noRecord = repo.transaction.RowsAffected == 0 + + return repo.noRecord +} +func (repo *repository[T1]) Transactions(ctx context.Context, act func(ctx context.Context, tx *gorm.DB)) { + + act(ctx, repo.transaction) + +} +func (repo *repository[T1]) Where(ctx context.Context) { + tx := repo.transaction + tx.WithContext(ctx).Where(&repo.entity) + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + +} +func (repo *repository[T1]) Find(ctx context.Context, res any) { + + tx := repo.transaction + tx.WithContext(ctx).Find(&res) + if tx.Error != nil { + tx.Rollback() + } + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + +} + +func (repo *repository[T1]) FindAllPaginate(ctx context.Context, res any) { + + tx := repo.transaction + tx.WithContext(ctx).Limit(repo.pagination.limit).Offset(repo.pagination.offset).Find(&res) + if tx.Error != nil { + tx.Rollback() + } + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + + return + +} + +func (repo *repository[T1]) Create(ctx context.Context) { + + tx := repo.transaction.Create(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + return +} + +func (repo *repository[T1]) Update(ctx context.Context) { + + tx := repo.transaction + tx.WithContext(ctx).Save(&repo.entity).Find(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + +} + +func (repo *repository[T1]) Delete(ctx context.Context) { + + tx := repo.transaction + tx.WithContext(ctx).Delete(&repo.entity) + if tx.Error != nil { + tx.Rollback() + } + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + +} + +func (repo *repository[T1]) Query(ctx context.Context, res any) { + + tx := repo.transaction + tx.WithContext(ctx).Model(&repo.entity).Raw(repo.customQuery.sql, repo.customQuery.values).Scan(&res) + if tx.Error != nil { + tx.Rollback() + } + + repo.rowsCount = int(tx.RowsAffected) + repo.noRecord = repo.rowsCount == 0 + repo.rowsError = tx.Error + +} diff --git a/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go b/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go new file mode 100644 index 0000000000000000000000000000000000000000..f9995e9e50b34b15ef8fe6d36829259fdb1a77fd --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/router/prediction_route.go @@ -0,0 +1,16 @@ +package router + +import ( + factory "github.com/abdanhafidz/ai-visual-multi-modal-backend/factory" + "github.com/gin-gonic/gin" +) + +func PredictionRoute(router *gin.Engine) { + routerGroup := router.Group("/api/v1") + { + routerGroup.POST("/predict", func(c *gin.Context) { + predictionModule := factory.NewPredictionModule() + predictionModule.Predict(c) + }) + } +} diff --git a/space/space/space/space/space/space/space/space/space/space/router/router.go b/space/space/space/space/space/space/space/space/space/space/router/router.go new file mode 100644 index 0000000000000000000000000000000000000000..dccd24c6236352640d3db082572a690ae04421f1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/router/router.go @@ -0,0 +1,14 @@ +package router + +import ( + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + controller "github.com/abdanhafidz/ai-visual-multi-modal-backend/controller" + "github.com/gin-gonic/gin" +) + +func StartService() { + router := gin.Default() + router.GET("/", controller.HomeController) + PredictionRoute(router) + router.Run(config.TCP_ADDRESS) +} diff --git a/space/space/space/space/space/space/space/space/space/space/services/authentication_service.go b/space/space/space/space/space/space/space/space/space/space/services/authentication_service.go new file mode 100644 index 0000000000000000000000000000000000000000..a7f1b4d97bc419a05aa49bc4ef098959a0c38d3f --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/services/authentication_service.go @@ -0,0 +1,11 @@ +package services + +import "context" + +type AuthenticationService interface { + Register(ctx context.Context, fingerPrint string) + Login(ctx context.Context, fingerPrint string) +} + +type authenticationService interface { +} diff --git a/space/space/space/space/space/space/space/space/space/space/services/openai_service.go b/space/space/space/space/space/space/space/space/space/space/services/openai_service.go new file mode 100644 index 0000000000000000000000000000000000000000..8ed8bfa12da250fe4268ff9b35abbe49ab268d0a --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/services/openai_service.go @@ -0,0 +1,110 @@ +package services + +import ( + "context" + "io" + "mime/multipart" + "os" + "path/filepath" + + "github.com/sashabaranov/go-openai" + + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +type ( + openAIService struct { + *service[repositories.Repository] + client *openai.Client + } + + OpenAIService interface { + Service + SpeechToText(ctx context.Context, audioFile multipart.File, filename string) string + TextToSpeech(ctx context.Context, text string) []byte + } +) + +func NewOpenAIService(repo repositories.Repository, openAIClient *openai.Client) OpenAIService { + return &openAIService{ + service: &service[repositories.Repository]{ + repository: repo, + }, + client: openAIClient, + } +} + +func (s *openAIService) SpeechToText(ctx context.Context, audioFile multipart.File, filename string) string { + + audioDir := "audio" + + if err := os.MkdirAll(audioDir, os.ModePerm); err != nil { + s.ThrowsException(&s.exception.InternalServerError, "Failed to create directory!") + s.ThrowsError(err) + return "failed to create directory!" + } + + savedPath := filepath.Join(audioDir, filepath.Base(filename)) + outFile, err := os.Create(savedPath) + + if err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to save audio!") + s.ThrowsError(err) + return "Failed to save audio!" + } + + defer outFile.Close() + + if _, err := io.Copy(outFile, audioFile); err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to save audio!") + s.ThrowsError(err) + return "Failed to save audio!" + } + + req := openai.AudioRequest{ + Model: openai.Whisper1, + FilePath: savedPath, + } + + resp, err := s.client.CreateTranscription(ctx, req) + if err != nil { + s.ThrowsException(&s.exception.FailedTranscripting, "Failed to create transcription!") + s.ThrowsError(err) + return "Failed to create transcription!" + } + // if resp.Text != "" { + // outFile.Close() + // } else { + // s.ThrowsException(&s.exception.FailedTranscripting, "Failed to create transcription! [Nil text]") + // } + + // if err := os.Remove(savedPath); err != nil { + // s.ThrowsError(err) + // } + + return resp.Text +} + +func (s *openAIService) TextToSpeech(ctx context.Context, text string) []byte { + req := openai.CreateSpeechRequest{ + Model: openai.TTSModel1, + Input: text, + Voice: openai.VoiceEcho, + ResponseFormat: openai.SpeechResponseFormatMp3, + } + + audioResp, err := s.client.CreateSpeech(ctx, req) + if err != nil { + s.ThrowsException(&s.exception.FailedGenerateAudio, "Failed to generate speech audio!") + s.ThrowsError(err) + return nil + } + + audioData, err := io.ReadAll(audioResp) + if err != nil { + s.ThrowsException(&s.exception.AudioFileError, "Failed to read audio response!") + s.ThrowsError(err) + return nil + } + return audioData +} diff --git a/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go b/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go new file mode 100644 index 0000000000000000000000000000000000000000..1f4a531403d4a1675cd9a6ddc65ff6988838a445 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/services/prediction_service.go @@ -0,0 +1,61 @@ +package services + +import ( + "context" + "fmt" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +type ( + PredictionService interface { + Service + Predict(ctx context.Context, req models.PredictionRequest) (audio []byte, text string) + } + + predictionService struct { + *service[repositories.ChatHistoryRepository] + replicateService ReplicateService + openAIService OpenAIService + } +) + +func NewPredictionService(chatHistoryRepository repositories.ChatHistoryRepository, replicateService ReplicateService, openAIService OpenAIService) PredictionService { + return &predictionService{ + service: &service[repositories.ChatHistoryRepository]{ + repository: chatHistoryRepository, + }, + replicateService: replicateService, + openAIService: openAIService, + } +} + +func (s *predictionService) Predict(ctx context.Context, req models.PredictionRequest) (audio []byte, text string) { + sttOutput := s.openAIService.SpeechToText(ctx, req.AudioQuestionFile, req.AudioQuestionFilename) + if s.openAIService.Error() != nil { + s.ThrowsException(&s.exception.BadRequest, "Failed to generate speech to text!") + s.ThrowsError(s.openAIService.Error()) + return nil, "" + } + + fmt.Println("Input :", sttOutput) + replicateOutput := s.replicateService.AskImage(ctx, req.ImageFile, req.ImageFileName, sttOutput) + if s.replicateService.Error() != nil { + s.ThrowsException(&s.exception.ReplicateConnectionRefused, "Replicate Connection Refused!") + s.ThrowsError(s.replicateService.Error()) + return nil, "" + } + + ttsOutput := s.openAIService.TextToSpeech(ctx, replicateOutput) + if s.openAIService.Error() != nil { + s.ThrowsException(&s.exception.FailedGenerateAudio, "Failed to convert audio output!") + s.ThrowsError(s.openAIService.Error()) + return nil, "" + } + // savePrediction := s.repository.SaveChatHistory(ctx, req.ImageFileName, sttOutput, replicateOutput) + // if s.ThrowsRepoException() { + // return nil, "" + // } + return ttsOutput, replicateOutput +} diff --git a/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go b/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go new file mode 100644 index 0000000000000000000000000000000000000000..cfe409abb530b4595e58ed0b66010993eeddb94c --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/services/replicate_service.go @@ -0,0 +1,74 @@ +package services + +import ( + "bytes" + "context" + "fmt" + "io" + "mime/multipart" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/replicate/replicate-go" +) + +type ReplicateService interface { + Service + AskImage(ctx context.Context, imageFile multipart.File, filename, question string) string +} + +type replicateService struct { + *service[repositories.Repository] + client *replicate.Client + model string +} + +func NewReplicateService(repo repositories.Repository, replicateClient *replicate.Client, model string) ReplicateService { + service := replicateService{ + service: &service[repositories.Repository]{repository: repo}, + client: replicateClient, + model: model, // e.g., "owner/moondream:versionHash" + } + return &service +} + +func (s *replicateService) AskImage(ctx context.Context, imageFile multipart.File, filename, question string) string { + var buf bytes.Buffer + if _, err := io.Copy(&buf, imageFile); err != nil { + + s.ThrowsError(err) + return "" + } + + file, err := s.client.CreateFileFromBuffer(ctx, &buf, &replicate.CreateFileOptions{Filename: filename}) + if err != nil { + s.ThrowsError(err) + return "" + } + + input := replicate.PredictionInput{ + "image": file, + "question": question, + } + rawOutput, err := s.client.Run(ctx, s.model, input, nil) + if err != nil { + s.ThrowsError(err) + return "" + } + fmt.Println("Output slice", rawOutput) + outputSlice, ok := rawOutput.([]interface{}) + var result string + if ok { + result = fmt.Sprintf("%v", outputSlice) + fmt.Println("Output slice", result) + } else { + result = fmt.Sprintf("%v", rawOutput) + fmt.Println("Output slice", result) + } + + // if !ok { + // s.ThrowsError(errors.New("failed to parse output as string")) + // return "" + // } + + return result +} diff --git a/space/space/space/space/space/space/space/space/space/space/services/service.go b/space/space/space/space/space/space/space/space/space/space/services/service.go new file mode 100644 index 0000000000000000000000000000000000000000..7cb37fbb35497c0bf956cd43ba836231f25c6295 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/services/service.go @@ -0,0 +1,71 @@ +package services + +import ( + "errors" + "time" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + repositories "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "gorm.io/gorm" +) + +type ( + service[TRepo repositories.Repository] struct { + repository TRepo + exception models.Exception + errors error + } + Service interface { + ThrowsException(*bool, string) + ThrowsError(error) + Exception() models.Exception + ThrowsRepoException() bool + Error() error + } +) + +func (s *service[TRepo]) ThrowsException(status *bool, message string) { + + *status = true + s.exception.Message = message + +} + +func (s *service[TRepo]) ThrowsError(err error) { + + s.errors = errors.Join(s.errors, err) + +} + +func (s *service[TRepo]) Exception() models.Exception { + return s.exception +} +func (s *service[TRepo]) Error() error { + return s.errors +} +func CalculateDueTime(duration time.Duration) time.Time { + return time.Now().Add(duration) +} + +func (s *service[TRepo]) ThrowsRepoException() bool { + + if s.repository.RowsError() != nil { + + s.ThrowsException(&s.exception.QueryError, "Database error!") + s.ThrowsError(s.repository.RowsError()) + + return true + } + if errors.Is(s.repository.RowsError(), gorm.ErrDuplicatedKey) { + s.ThrowsException(&s.exception.DataDuplicate, "Duplicated data!") + + return true + } + if s.repository.IsNoRecord() { + + s.ThrowsException(&s.exception.DataNotFound, "No record found") + return true + } + + return false +} diff --git a/space/space/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go b/space/space/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ed3b76b47d91d222d937ba28a6836db34220d668 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go @@ -0,0 +1,7 @@ +package tests + +// func SaveChatHistoryTest(t *testing.T) { +// ctx := context.Background() +// chatHistoryRepo := repositories.NewChatHistoryRepository() +// chatHistoryRepo.SaveChatHistory(ctx, "" +// } diff --git a/space/space/space/space/space/space/space/space/space/space/utils/api_response.go b/space/space/space/space/space/space/space/space/space/space/utils/api_response.go new file mode 100644 index 0000000000000000000000000000000000000000..a638c08da4c37bf82272e090fd9669522504f1f8 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/utils/api_response.go @@ -0,0 +1,52 @@ +package utils + +import ( + "net/http" + "reflect" + + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + services "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" + "github.com/gin-gonic/gin" +) + +func ResponseOK(c *gin.Context, data any) { + res := models.SuccessResponse{ + Status: "success", + Message: "Data retrieved successfully!", + Data: data, + MetaData: c.Request.Body, + } + c.JSON(http.StatusOK, res) + return +} + +func ResponseFAIL(c *gin.Context, status int, exception models.Exception) { + message := exception.Message + exception.Message = "" + res := models.ErrorResponse{ + Status: "error", + Message: message, + Errors: exception, + MetaData: c.Request.Body, + } + c.AbortWithStatusJSON(status, res) + return +} + +func SendResponse(c *gin.Context, data services.Service) { + 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/space/space/space/utils/helper.go b/space/space/space/space/space/space/space/space/space/space/utils/helper.go new file mode 100644 index 0000000000000000000000000000000000000000..fee40aef724f1cee53d507aca1f16006a8bcf512 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/utils/helper.go @@ -0,0 +1,11 @@ +package utils + +import ( + models "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/gin-gonic/gin" +) + +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/space/space/space/utils/logger.go b/space/space/space/space/space/space/space/space/space/space/utils/logger.go new file mode 100644 index 0000000000000000000000000000000000000000..40c86a2b23e2325776d43228382e6cc5655803a1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/utils/logger.go @@ -0,0 +1,21 @@ +package utils + +import ( + "fmt" + "log" + "os" + + config "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" +) + +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) + } + + log.SetOutput(file) + + log.Println("Error Log :", errorLogged) +} diff --git a/space/space/space/space/space/space/space/space/space/space/utils/util.go b/space/space/space/space/space/space/space/space/space/space/utils/util.go new file mode 100644 index 0000000000000000000000000000000000000000..480645d434c01f7152d6747171e5e3ea53969533 --- /dev/null +++ b/space/space/space/space/space/space/space/space/space/space/utils/util.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/space/space/tests/chat_history_repository_test.go b/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go index ed3b76b47d91d222d937ba28a6836db34220d668..1c459e2e17149eb59dcb10cb7b47a0c98d20d82c 100644 --- a/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go +++ b/space/space/space/space/space/space/space/space/tests/chat_history_repository_test.go @@ -1,7 +1,41 @@ package tests -// func SaveChatHistoryTest(t *testing.T) { -// ctx := context.Background() -// chatHistoryRepo := repositories.NewChatHistoryRepository() -// chatHistoryRepo.SaveChatHistory(ctx, "" -// } +import ( + "context" + "testing" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/models" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +func TestSaveChatHistoryRepository(t *testing.T) { + config.RunConfig() + var ctx context.Context + ctx = context.Background() + t.Log("DB Ptr :", config.DB) + chatHistoryRepo := repositories.NewChatHistoryRepository(config.DB) + t.Log("Repo Ptr :", chatHistoryRepo) + result := chatHistoryRepo.SaveChatHistory(ctx, "Testing", "Testing", "Testing") + t.Log("Error Log:", chatHistoryRepo.RowsError()) + expectedResult := models.ChatHistory{ + Answer: "Testing", + ImagePath: "Testing", + Question: "Testing", + } + if chatHistoryRepo.IsNoRecord() { + t.Log("Is No Record:", chatHistoryRepo.IsNoRecord()) + t.Errorf("Failed to create rows!") + } + err := chatHistoryRepo.RowsError() + if err != nil { + // t.Logf("Expected Result: %v, Got: %v", expectedResult, result) + t.Error("Error while saving chat history:", err) + } + if result.Answer != expectedResult.Answer || result.ImagePath != expectedResult.ImagePath || result.Question != expectedResult.Question { + t.Logf("Expected Result: %v, Got: %v", expectedResult, result) + t.Errorf("Wrong Result of chat history") + + return + } +} diff --git a/space/space/space/space/space/space/space/space/tests/images/foto_pacarku.jpg b/space/space/space/space/space/space/space/space/tests/images/foto_pacarku.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1473f8a17a553287ac9b778dc1404b3bcc3cbcb7 Binary files /dev/null and b/space/space/space/space/space/space/space/space/tests/images/foto_pacarku.jpg differ diff --git a/space/space/space/space/space/space/space/space/tests/open_ai_service_test.go b/space/space/space/space/space/space/space/space/tests/open_ai_service_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cd96019b1c92f0df5e1150259941eed57f17dcb1 --- /dev/null +++ b/space/space/space/space/space/space/space/space/tests/open_ai_service_test.go @@ -0,0 +1,62 @@ +package tests + +import ( + "context" + "os" + "testing" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" +) + +func TestSpeechToText(t *testing.T) { + config.RunConfig() + // Inisialisasi dependency + var dummyRepo repositories.Repository + openAIService := services.NewOpenAIService(dummyRepo, config.OpenAIClient) + + ctx := context.Background() + filename := "test_audio.mp3" + // Buka file audio dummy untuk pengujian + audioFile, err := os.Open("test_data/" + filename) + if err != nil { + t.Fatalf("Failed to open audio test file: %v", err) + } + defer audioFile.Close() + + result := openAIService.SpeechToText(ctx, audioFile, filename) + if openAIService.Error() != nil { + t.Fatalf("Speech To Text failed: %v", openAIService.Error()) + } + if result == "" { + t.Errorf("Expected transcription result, got empty string") + } else { + t.Logf("Transcription result: %s", result) + } +} + +func TestTextToSpeech(t *testing.T) { + config.RunConfig() + // Inisialisasi dependency + var dummyRepo repositories.Repository + openAIService := services.NewOpenAIService(dummyRepo, config.OpenAIClient) + + ctx := context.Background() + text := "Halo, ini adalah pengujian Text to Speech." + + audioBytes := openAIService.TextToSpeech(ctx, text) + + if openAIService.Error() != nil { + t.Fatalf("TextToSpeech failed: %v", openAIService.Error()) + } + + if len(audioBytes) == 0 { + t.Errorf("Expected non-empty audio bytes") + } + + // (Opsional) Simpan hasil untuk verifikasi manual + if err := os.WriteFile("test_data/output_test.mp3", audioBytes, 0644); err != nil { + t.Errorf("Gagal menyimpan hasil audio: %v", err) + } +} diff --git a/space/space/space/space/space/space/space/space/tests/replicate_service_test.go b/space/space/space/space/space/space/space/space/tests/replicate_service_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f0ed05f883c480175dce403fc59cac23d3bdca7b --- /dev/null +++ b/space/space/space/space/space/space/space/space/tests/replicate_service_test.go @@ -0,0 +1,34 @@ +package tests + +import ( + "context" + "os" + "testing" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/services" +) + +func TestAskImage(t *testing.T) { + config.RunConfig() + var dummyRepo repositories.Repository + replicateService := services.NewReplicateService(dummyRepo, config.ReplicateClient, "spuuntries/urna-kp3l:9338a4573a17178b70515c0ef2e613d3b4213e2dc860ef23b3ad6149dacadc1e") + ctx := context.Background() + filename := "foto_pacarku.jpg" + imageFile, err := os.Open("test_data/" + filename) + if err != nil { + t.Fatalf("Failed to open image test file: %v", err) + } + defer imageFile.Close() + result := replicateService.AskImage(ctx, imageFile, filename, "What is this image about?") + if replicateService.Error() != nil { + t.Fatalf("AskImage failed: %v", replicateService.Error()) + } + if result == "" { + t.Errorf("Expected non-empty result, got empty string") + } else { + t.Logf("AskImage result: %s", result) + } + +} diff --git a/space/space/space/space/space/space/space/space/tests/test_data/foto_pacarku.jpg b/space/space/space/space/space/space/space/space/tests/test_data/foto_pacarku.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1473f8a17a553287ac9b778dc1404b3bcc3cbcb7 Binary files /dev/null and b/space/space/space/space/space/space/space/space/tests/test_data/foto_pacarku.jpg differ diff --git a/space/space/space/space/space/space/space/space/tests/test_data/output_test.mp3 b/space/space/space/space/space/space/space/space/tests/test_data/output_test.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..1df7b0829c911d100369166af2b28169919366dd Binary files /dev/null and b/space/space/space/space/space/space/space/space/tests/test_data/output_test.mp3 differ diff --git a/space/space/space/space/space/space/space/space/tests/test_data/test_audio.mp3 b/space/space/space/space/space/space/space/space/tests/test_data/test_audio.mp3 new file mode 100644 index 0000000000000000000000000000000000000000..d6ec250b2eeedbdd36b90c6c1a2a7875bcd00f02 Binary files /dev/null and b/space/space/space/space/space/space/space/space/tests/test_data/test_audio.mp3 differ diff --git a/space/space/space/space/space/space/space/space/utils/logger.go b/space/space/space/space/space/space/space/space/utils/logger.go index 40c86a2b23e2325776d43228382e6cc5655803a1..5fa9c6eae8929c9d8cdb9771bfdd5e48b6f89c19 100644 --- a/space/space/space/space/space/space/space/space/utils/logger.go +++ b/space/space/space/space/space/space/space/space/utils/logger.go @@ -15,6 +15,7 @@ func LogError(errorLogged error) { log.Fatal(err) } + log.Println("Error Log :", errorLogged) log.SetOutput(file) log.Println("Error Log :", errorLogged) diff --git a/space/space/tests/account_repository_test.go b/space/space/tests/account_repository_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3a654db2ab4884984e0c7e93b90617354f12baf3 --- /dev/null +++ b/space/space/tests/account_repository_test.go @@ -0,0 +1,30 @@ +package tests + +import ( + "context" + "testing" + + "github.com/abdanhafidz/ai-visual-multi-modal-backend/config" + "github.com/abdanhafidz/ai-visual-multi-modal-backend/repositories" +) + +func TestCreateAccountRepository(t *testing.T) { + config.RunConfig() + var ctx context.Context + ctx = context.Background() + t.Log("DB Ptr :", config.DB) + accountRepo := repositories.NewAccountRepository(config.DB) + t.Log("Repo Ptr :", accountRepo) + + result := accountRepo.CreateAccount(ctx, "testpassphrases") + if accountRepo.RowsError() != nil { + t.Errorf("Error creating account: %v", accountRepo.RowsError()) + return + } + + expectedPassPhrase := "testpassphrases" + if result.PassPhrase != expectedPassPhrase { + t.Errorf("Expected passPhrase %s, got %s", expectedPassPhrase, result.PassPhrase) + } + t.Logf("Account created successfully: %+v", result) +} diff --git a/space/space/tests/test_data/output_test.mp3 b/space/space/tests/test_data/output_test.mp3 index 1df7b0829c911d100369166af2b28169919366dd..325a0876ac430251b564dec2be5fea5572d97580 100644 Binary files a/space/space/tests/test_data/output_test.mp3 and b/space/space/tests/test_data/output_test.mp3 differ diff --git a/space/tests/account_repository_test.go b/space/tests/account_repository_test.go index 3a654db2ab4884984e0c7e93b90617354f12baf3..f828bb5aa2ad3f7777bb3eb677e4f83ff27bf266 100644 --- a/space/tests/account_repository_test.go +++ b/space/tests/account_repository_test.go @@ -28,3 +28,25 @@ func TestCreateAccountRepository(t *testing.T) { } t.Logf("Account created successfully: %+v", result) } + +func TestGetAccountByPassPhraseRepository(t *testing.T) { + config.RunConfig() + var ctx context.Context + ctx = context.Background() + t.Log("DB Ptr :", config.DB) + accountRepo := repositories.NewAccountRepository(config.DB) + t.Log("Repo Ptr :", accountRepo) + + passPhrase := "testpassphrases" + result := accountRepo.GetAccountByPassPhrase(ctx, passPhrase) + if accountRepo.RowsError() != nil { + t.Errorf("Error getting account: %v", accountRepo.RowsError()) + return + } + + if result.PassPhrase != passPhrase { + t.Errorf("Expected passPhrase %s, got %s", passPhrase, result.PassPhrase) + } else { + t.Logf("Account retrieved successfully: %+v", result) + } +} diff --git a/tests/account_repository_test.go b/tests/account_repository_test.go index f828bb5aa2ad3f7777bb3eb677e4f83ff27bf266..01b169aed8ffa84676429c982c436f48fb692459 100644 --- a/tests/account_repository_test.go +++ b/tests/account_repository_test.go @@ -17,6 +17,10 @@ func TestCreateAccountRepository(t *testing.T) { t.Log("Repo Ptr :", accountRepo) result := accountRepo.CreateAccount(ctx, "testpassphrases") + if accountRepo.IsNoRecord() { + t.Errorf("No Record Account: %v", accountRepo.RowsError()) + return + } if accountRepo.RowsError() != nil { t.Errorf("Error creating account: %v", accountRepo.RowsError()) return @@ -39,6 +43,10 @@ func TestGetAccountByPassPhraseRepository(t *testing.T) { passPhrase := "testpassphrases" result := accountRepo.GetAccountByPassPhrase(ctx, passPhrase) + if accountRepo.IsNoRecord() { + t.Errorf("No Record Account: %v", accountRepo.IsNoRecord()) + return + } if accountRepo.RowsError() != nil { t.Errorf("Error getting account: %v", accountRepo.RowsError()) return