From 8dce8278eefe94ed2c6c957d0dfe598d015f0593 Mon Sep 17 00:00:00 2001 From: zahra-sh Date: Sat, 4 Apr 2026 08:33:59 -0700 Subject: [PATCH] fix-pkgs --- benefactorapp/app.go | 10 +++---- benefactorapp/config.go | 2 +- .../1775190076-create-benefacotors-table.sql | 20 +++++++++++++ .../1775190077-create-addresses-table.sql | 22 ++++++++++++++ cmd/benefactor/command/root.go | 9 ++++-- cmd/benefactor/command/serve.go | 2 +- deploy/benefactor/development/Dockerfile | 2 ++ deploy/benefactor/development/config.yml | 30 +++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/cfg_loader/cfg_loader.go | 2 +- pkg/database/mysql/db.go | 4 +-- pkg/database/mysql/dbconfig.yml | 2 +- pkg/database/redis/otp/delete_code.go | 21 ------------- pkg/database/redis/otp/exist_phone_number.go | 21 ------------- pkg/database/redis/otp/get_code.go | 25 ---------------- pkg/database/redis/otp/save_code.go | 19 ------------ 17 files changed, 94 insertions(+), 100 deletions(-) create mode 100644 benefactorapp/repository/database/migration/1775190076-create-benefacotors-table.sql create mode 100644 benefactorapp/repository/database/migration/1775190077-create-addresses-table.sql delete mode 100644 pkg/database/redis/otp/delete_code.go delete mode 100644 pkg/database/redis/otp/exist_phone_number.go delete mode 100644 pkg/database/redis/otp/get_code.go delete mode 100644 pkg/database/redis/otp/save_code.go diff --git a/benefactorapp/app.go b/benefactorapp/app.go index 6c77ca9d..aa4beab4 100644 --- a/benefactorapp/app.go +++ b/benefactorapp/app.go @@ -2,7 +2,7 @@ package benefactorapp import ( "context" - benefactorHTTP "git.gocasts.ir/ebhomengo/niki/benefactorapp/delivery/http" + benefactorHttp "git.gocasts.ir/ebhomengo/niki/benefactorapp/delivery/http" repo "git.gocasts.ir/ebhomengo/niki/benefactorapp/repository/database" benefactor "git.gocasts.ir/ebhomengo/niki/benefactorapp/service" mySql "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql" @@ -12,9 +12,9 @@ import ( type Application struct { Config Config - HTTPServer benefactorHTTP.Server + HTTPServer benefactorHttp.Server BenefactorService benefactor.Service - BenefactorHandler benefactorHTTP.Handler + BenefactorHandler benefactorHttp.Handler BenefactorRepo benefactor.Repository DBConn mySql.DB } @@ -36,13 +36,13 @@ func Setup(ctx context.Context, config Config, DB mySql.DB) *Application { benefactorValidator := benefactor.NewValidator(benefactorRepo) benefactorSvc := benefactor.NewService(benefactorRepo, benefactorValidator) - benefactorHandler := benefactorHTTP.NewHandler(benefactorSvc) + benefactorHandler := benefactorHttp.NewHandler(benefactorSvc) hServer, hErr := httpserver.New(config.HTTPServer) if hErr != nil { log.Error("Http Server error: %v,\n", hErr) } - httpServer := benefactorHTTP.NewServer(*hServer, *benefactorHandler) + httpServer := benefactorHttp.NewServer(*hServer, *benefactorHandler) return &Application{ Config: config, diff --git a/benefactorapp/config.go b/benefactorapp/config.go index fb8f9376..f399aaba 100644 --- a/benefactorapp/config.go +++ b/benefactorapp/config.go @@ -11,7 +11,7 @@ type Config struct { HTTPServer httpserver.Config `koanf:"http_server"` // Database config - MySQLDB database.Config `koanf:"mysql_db"` + MySQLDB database.Config `koanf:"mariadb"` // Logger config Logger logger.Config `koanf:"logger"` diff --git a/benefactorapp/repository/database/migration/1775190076-create-benefacotors-table.sql b/benefactorapp/repository/database/migration/1775190076-create-benefacotors-table.sql new file mode 100644 index 00000000..1a9b03ed --- /dev/null +++ b/benefactorapp/repository/database/migration/1775190076-create-benefacotors-table.sql @@ -0,0 +1,20 @@ +-- +migrate Up +-- please read this article to understand why we use VARCHAR(191) +-- https://www.grouparoo.com/blog/varchar-191#why-varchar-and-not-text +CREATE TABLE `benefactors1` ( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `first_name` VARCHAR(191), + `last_name` VARCHAR(191), + `phone_number` VARCHAR(191) NOT NULL UNIQUE, + `description` TEXT, + `email` VARCHAR(191), + `gender` ENUM('male','female'), + `birth_date` TIMESTAMP, + `status` ENUM('active','inactive') NOT NULL DEFAULT 'active', + + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP +); + +-- +migrate Down +DROP TABLE `benefactors1`; \ No newline at end of file diff --git a/benefactorapp/repository/database/migration/1775190077-create-addresses-table.sql b/benefactorapp/repository/database/migration/1775190077-create-addresses-table.sql new file mode 100644 index 00000000..f5398634 --- /dev/null +++ b/benefactorapp/repository/database/migration/1775190077-create-addresses-table.sql @@ -0,0 +1,22 @@ +-- +migrate Up +CREATE TABLE `addresses1` ( + `id` INT PRIMARY KEY AUTO_INCREMENT, + `postal_code` VARCHAR(191) NOT NULL, + `address` TEXT NOT NULL, + `lat` FLOAT, + `lon` FLOAT, + `name` VARCHAR(191) NOT NULL, + `city_id` INT NOT NULL, + `province_id` INT NOT NULL, + `benefactor_id` INT NOT NULL, + + `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + `deleted_at` TIMESTAMP, + FOREIGN KEY (`province_id`) REFERENCES `provinces` (`id`), + FOREIGN KEY (`city_id`) REFERENCES `cities` (`id`), + FOREIGN KEY (`benefactor_id`) REFERENCES `benefactors1` (`id`) +); + +-- +migrate Down +DROP TABLE `addresses1`; \ No newline at end of file diff --git a/cmd/benefactor/command/root.go b/cmd/benefactor/command/root.go index 9d2a4ec0..5b3539b3 100644 --- a/cmd/benefactor/command/root.go +++ b/cmd/benefactor/command/root.go @@ -2,9 +2,10 @@ package command import ( "fmt" - benefactorapp "git.gocasts.ir/ebhomengo/niki/benefactorapp" + "git.gocasts.ir/ebhomengo/niki/benefactorapp" cfgloader "git.gocasts.ir/ebhomengo/niki/pkg/cfg_loader" "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql" + database "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql" serviceConfigMysql "git.gocasts.ir/ebhomengo/niki/pkg/database/mysql" "git.gocasts.ir/ebhomengo/niki/pkg/path" "github.com/spf13/cobra" @@ -30,11 +31,13 @@ func getMysqlConfig() mysql.Config { Host: cfg.MySQLDB.Host, DBName: cfg.MySQLDB.DBName, } + return mysqlConfig } -func getDB() *mysql.DB { - db := serviceConfigMysql.New(getMysqlConfig()) +func getDB(cfg database.Config) *mysql.DB { + + db := serviceConfigMysql.New(cfg) defer func() { if err := db.CloseStatements(); err != nil { fmt.Printf("Error closing statements: %v\n", err) diff --git a/cmd/benefactor/command/serve.go b/cmd/benefactor/command/serve.go index dd1c2f99..ca141307 100644 --- a/cmd/benefactor/command/serve.go +++ b/cmd/benefactor/command/serve.go @@ -26,7 +26,7 @@ func serve() { logger.Init(cfg.Logger) log := logger.L() - db := getDB() + db := getDB(cfg.MySQLDB) migrate() // Start the server diff --git a/deploy/benefactor/development/Dockerfile b/deploy/benefactor/development/Dockerfile index e69de29b..3d8f9272 100644 --- a/deploy/benefactor/development/Dockerfile +++ b/deploy/benefactor/development/Dockerfile @@ -0,0 +1,2 @@ +FROM golang:1.25-alpine + diff --git a/deploy/benefactor/development/config.yml b/deploy/benefactor/development/config.yml index e69de29b..084cbebd 100644 --- a/deploy/benefactor/development/config.yml +++ b/deploy/benefactor/development/config.yml @@ -0,0 +1,30 @@ +http_server: + host: "" + port: 1308 + shutdown_context_timeout: 10s + cors: + allow_origins: + - "*" + +logger: + level: "debug" # Can be `debug`, `info`, `warn`, `error` + file_path: "logs/benefactorapp/service.log" + use_local_time: true + file_max_size_in_mb: 10 + file_max_age_in_days: 7 + +database_retry: + max_retries: 3 + retry_delay: 100ms + +total_shutdown_timeout: 30m + +path_of_migration: "./benefactorapp/repository/migration" + +mariadb: + port: 3306 + host: localhost + db_name: niki_db + username: niki + password: nikiappt0lk2o20 + diff --git a/go.mod b/go.mod index 7b4af63e..f6c2fc41 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/golang-jwt/jwt/v4 v4.5.2 github.com/kavenegar/kavenegar-go v0.0.0-20240205151018-77039f51467d github.com/knadh/koanf v1.5.0 + github.com/knadh/koanf/v2 v2.3.0 github.com/labstack/echo-jwt/v4 v4.4.0 github.com/labstack/echo/v4 v4.15.1 github.com/ory/dockertest/v3 v3.12.0 diff --git a/go.sum b/go.sum index 47efff98..15f9342a 100644 --- a/go.sum +++ b/go.sum @@ -224,6 +224,8 @@ github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBF github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/knadh/koanf v1.5.0 h1:q2TSd/3Pyc/5yP9ldIrSdIz26MCcyNQzW0pEAugLPNs= github.com/knadh/koanf v1.5.0/go.mod h1:Hgyjp4y8v44hpZtPzs7JZfRAW5AhN7KfZcwv1RYggDs= +github.com/knadh/koanf/v2 v2.3.0 h1:Qg076dDRFHvqnKG97ZEsi9TAg2/nFTa9hCdcSa1lvlM= +github.com/knadh/koanf/v2 v2.3.0/go.mod h1:gRb40VRAbd4iJMYYD5IxZ6hfuopFcXBpc9bbQpZwo28= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= diff --git a/pkg/cfg_loader/cfg_loader.go b/pkg/cfg_loader/cfg_loader.go index 600e6824..21116577 100644 --- a/pkg/cfg_loader/cfg_loader.go +++ b/pkg/cfg_loader/cfg_loader.go @@ -8,7 +8,7 @@ import ( "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/env" "github.com/knadh/koanf/providers/file" - "github.com/knadh/koanf/v2" + //"github.com/knadh/koanf/v2" ) type Option struct { diff --git a/pkg/database/mysql/db.go b/pkg/database/mysql/db.go index eefe5508..dc2f3dad 100644 --- a/pkg/database/mysql/db.go +++ b/pkg/database/mysql/db.go @@ -4,10 +4,10 @@ import ( "context" "database/sql" "fmt" + querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql" + _ "github.com/go-sql-driver/mysql" "sync" "time" - - querier "git.gocasts.ir/ebhomengo/niki/pkg/query_transaction/sql" ) type Config struct { diff --git a/pkg/database/mysql/dbconfig.yml b/pkg/database/mysql/dbconfig.yml index ee6c6247..db89d8a3 100644 --- a/pkg/database/mysql/dbconfig.yml +++ b/pkg/database/mysql/dbconfig.yml @@ -1,5 +1,5 @@ production: dialect: mysql datasource: niki:nikiappt0lk2o20@(localhost:3306)/niki_db?parseTime=true - dir: repository/mysql/migration + dir: pkg/database/mysql/migration table: gorp_migrations diff --git a/pkg/database/redis/otp/delete_code.go b/pkg/database/redis/otp/delete_code.go deleted file mode 100644 index 6fffd6f0..00000000 --- a/pkg/database/redis/otp/delete_code.go +++ /dev/null @@ -1,21 +0,0 @@ -package redisotp - -import ( - "context" - - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (d DB) DeleteCodeByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) { - const op = "redisotp.GetCodeByPhoneNumber" - - success, err := d.adapter.Client().Del(ctx, phoneNumber).Result() - if err != nil { - return false, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - if success != 1 { - return false, nil - } - - return true, nil -} diff --git a/pkg/database/redis/otp/exist_phone_number.go b/pkg/database/redis/otp/exist_phone_number.go deleted file mode 100644 index d8a0fac2..00000000 --- a/pkg/database/redis/otp/exist_phone_number.go +++ /dev/null @@ -1,21 +0,0 @@ -package redisotp - -import ( - "context" - - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (d DB) IsExistPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) { - const op = "redisotp.IsExistPhoneNumber" - - isExist, err := d.adapter.Client().Exists(ctx, phoneNumber).Result() - if err != nil { - return false, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - if isExist == 0 { - return false, nil - } - - return true, nil -} diff --git a/pkg/database/redis/otp/get_code.go b/pkg/database/redis/otp/get_code.go deleted file mode 100644 index 99534e08..00000000 --- a/pkg/database/redis/otp/get_code.go +++ /dev/null @@ -1,25 +0,0 @@ -package redisotp - -import ( - "context" - "errors" - - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" - "github.com/redis/go-redis/v9" -) - -func (d DB) GetCodeByPhoneNumber(ctx context.Context, phoneNumber string) (string, error) { - const op = "redisotp.GetCodeByPhoneNumber" - - value, err := d.adapter.Client().Get(ctx, phoneNumber).Result() - if err != nil { - rErr := redis.Nil - if errors.As(err, &rErr) { - return "", nil - } - - return "", richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return value, nil -} diff --git a/pkg/database/redis/otp/save_code.go b/pkg/database/redis/otp/save_code.go deleted file mode 100644 index 8674421e..00000000 --- a/pkg/database/redis/otp/save_code.go +++ /dev/null @@ -1,19 +0,0 @@ -package redisotp - -import ( - "context" - "time" - - richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" -) - -func (d DB) SaveCodeWithPhoneNumber(ctx context.Context, phoneNumber, code string, expireTime time.Duration) error { - const op = "redisotp.SaveCodeWithPhoneNumber" - - err := d.adapter.Client().Set(ctx, phoneNumber, code, expireTime).Err() - if err != nil { - return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) - } - - return nil -}