diff --git a/cmd/legalgo/main.go b/cmd/legalgo/main.go index e828503..b8e560c 100644 --- a/cmd/legalgo/main.go +++ b/cmd/legalgo/main.go @@ -5,6 +5,7 @@ import ( "fmt" "log" + "github.com/ardeman/project-legalgo-go/config" "github.com/ardeman/project-legalgo-go/database" repository "github.com/ardeman/project-legalgo-go/internal/accessor" internalhttp "github.com/ardeman/project-legalgo-go/internal/api/http" @@ -19,6 +20,8 @@ func init() { if err := godotenv.Load(); err != nil { log.Fatal("cannot load environment file") } + + config.InitEnv() } func run(lc fx.Lifecycle, db *database.DB, apiRouter chi.Router) { diff --git a/config/conf.go b/config/conf.go new file mode 100644 index 0000000..1723f54 --- /dev/null +++ b/config/conf.go @@ -0,0 +1,23 @@ +package config + +import "github.com/ardeman/project-legalgo-go/internal/utilities/utils" + +var ( + APP_PORT int + GRACEFULL_TIMEOUT int + + // DB + DB_HOST, + DB_USER, + DB_PASSWORD, + DB_NAME, + DB_PORT string +) + +func InitEnv() { + DB_HOST = utils.GetOrDefault("DB_HOST", "localhost") + DB_USER = utils.GetOrDefault("DB_USER", "") + DB_PASSWORD = utils.GetOrDefault("DB_PASSWORD", "") + DB_NAME = utils.GetOrDefault("DB_NAME", "") + DB_PORT = utils.GetOrDefault("DB_PORT", "") +} diff --git a/database/new_db.go b/database/new_db.go index 7e93bbc..45d1a71 100644 --- a/database/new_db.go +++ b/database/new_db.go @@ -2,11 +2,8 @@ package database import ( "fmt" - "os" - staffmodel "github.com/ardeman/project-legalgo-go/database/staff" - subsmodel "github.com/ardeman/project-legalgo-go/database/subscribe" - usermodel "github.com/ardeman/project-legalgo-go/database/user" + "github.com/ardeman/project-legalgo-go/config" "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -16,16 +13,15 @@ type DB struct { } func NewDB() (*DB, error) { - var ( - host = os.Getenv("DB_HOST") - user = os.Getenv("DB_USER") - password = os.Getenv("DB_PASSWORD") - dbName = os.Getenv("DB_NAME") - dbPort = os.Getenv("DB_PORT") + dsn := fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%v sslmode=disable", + config.DB_HOST, + config.DB_USER, + config.DB_PASSWORD, + config.DB_NAME, + config.DB_PORT, ) - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%v sslmode=disable", host, user, password, dbName, dbPort) - if dsn == "" { return nil, fmt.Errorf("DATABASE_URL environment is not set") } @@ -42,9 +38,9 @@ func NewDB() (*DB, error) { func (db *DB) Migrate() error { // Auto Migrate the User model return db.AutoMigrate( - &staffmodel.Staff{}, - &subsmodel.SubscribePlan{}, - &subsmodel.Subscribe{}, - &usermodel.User{}, + &Staff{}, + &SubscribePlan{}, + &Subscribe{}, + &User{}, ) } diff --git a/database/news_model.go b/database/news_model.go new file mode 100644 index 0000000..378f3d1 --- /dev/null +++ b/database/news_model.go @@ -0,0 +1,19 @@ +package database + +import ( + "time" + + "github.com/google/uuid" +) + +type News struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` + AuthorID uuid.UUID `gorm:"type:uuid;not null" json:"author_id"` + Title string `gorm:"default:null" json:"title"` + Content string `gorm:"default:null" json:"content"` + IsPremium bool `gorm:"default:false" json:"is_premium"` + Slug string `gorm:"default:null" json:"slug"` + FeaturedImage string `gorm:"default:null" json:"featured_image"` + CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` + UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"` +} diff --git a/database/staff/model.go b/database/staff_model.go similarity index 83% rename from database/staff/model.go rename to database/staff_model.go index fee793f..566aaa3 100644 --- a/database/staff/model.go +++ b/database/staff_model.go @@ -1,4 +1,4 @@ -package staffmodel +package database import ( "time" @@ -8,6 +8,7 @@ import ( type Staff struct { ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` + Username string `gorm:"default:null" json:"username"` Email string `gorm:"unique,not null" json:"email"` Password string `gorm:"not null" json:"password"` CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` diff --git a/database/subscribe/model.go b/database/subscribe_model.go similarity index 98% rename from database/subscribe/model.go rename to database/subscribe_model.go index cdfe980..745b0a7 100644 --- a/database/subscribe/model.go +++ b/database/subscribe_model.go @@ -1,4 +1,4 @@ -package subsmodel +package database import ( "time" diff --git a/database/tags_module.go b/database/tags_module.go new file mode 100644 index 0000000..f00c3ba --- /dev/null +++ b/database/tags_module.go @@ -0,0 +1,15 @@ +package database + +import ( + "time" + + "github.com/google/uuid" +) + +type Tags struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey;not null" json:"id"` + Code string `gorm:"not null" json:"code"` + Name string `gorm:"not null" json:"name"` + CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` + UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"` +} diff --git a/database/user/model.go b/database/user/model.go deleted file mode 100644 index 70294ad..0000000 --- a/database/user/model.go +++ /dev/null @@ -1,19 +0,0 @@ -package usermodel - -import ( - "time" - - subsmodel "github.com/ardeman/project-legalgo-go/database/subscribe" - "github.com/google/uuid" -) - -type User struct { - ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` - SubscribeID string `gorm:"not null" json:"subscribe_id"` - Email string `gorm:"unique,not null" json:"email"` - Password string `gorm:"not null" json:"password"` - Phone string `gorm:"default:null" json:"phone"` - Subscribe subsmodel.Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"` - CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` - UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"` -} diff --git a/database/user_model.go b/database/user_model.go new file mode 100644 index 0000000..fe21c5f --- /dev/null +++ b/database/user_model.go @@ -0,0 +1,18 @@ +package database + +import ( + "time" + + "github.com/google/uuid" +) + +type User struct { + ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"` + SubscribeID string `gorm:"not null" json:"subscribe_id"` + Email string `gorm:"unique,not null" json:"email"` + Password string `gorm:"not null" json:"password"` + Phone string `gorm:"default:null" json:"phone"` + Subscribe Subscribe `gorm:"foreignKey:SubscribeID;constraint:OnDelete:CASCADE"` + CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"` + UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"` +} diff --git a/internal/accessor/staff/create_staff.go b/internal/accessor/staff/create.go similarity index 100% rename from internal/accessor/staff/create_staff.go rename to internal/accessor/staff/create.go diff --git a/internal/accessor/staff/get_staff.go b/internal/accessor/staff/get_all.go similarity index 100% rename from internal/accessor/staff/get_staff.go rename to internal/accessor/staff/get_all.go diff --git a/internal/accessor/staff/update.go b/internal/accessor/staff/update.go new file mode 100644 index 0000000..3165d57 --- /dev/null +++ b/internal/accessor/staff/update.go @@ -0,0 +1,7 @@ +package staffrepository + +func (ur *StaffRepository) Update() error { + // if err := ur.DB.Update(column string, value interface{}) + + return nil +} diff --git a/internal/api/http/content/module.go b/internal/api/http/content/module.go new file mode 100644 index 0000000..c1f603c --- /dev/null +++ b/internal/api/http/content/module.go @@ -0,0 +1,5 @@ +package contenthttp + +import "go.uber.org/fx" + +var Module = fx.Module("content-api", fx.Invoke()) diff --git a/internal/config/chi_router.go b/internal/config/chi_router.go index d197f88..203c8bd 100644 --- a/internal/config/chi_router.go +++ b/internal/config/chi_router.go @@ -8,6 +8,7 @@ import ( "syscall" "time" + "github.com/ardeman/project-legalgo-go/config" "github.com/go-chi/chi/v5" "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" @@ -28,14 +29,14 @@ func Router(apiRouter chi.Router) { group, groupCtx := errgroup.WithContext(mainCtx) group.Go(func() error { - logrus.Infof("Listening to port %d", APP_PORT) + logrus.Infof("Listening to port %d", config.APP_PORT) return svr.ListenAndServe() }) group.Go(func() error { <-groupCtx.Done() - ctxTimeout, cancel := context.WithTimeout(mainCtx, GRACEFULL_TIMEOUT*time.Second) + ctxTimeout, cancel := context.WithTimeout(mainCtx, time.Duration(config.GRACEFULL_TIMEOUT)*time.Second) defer cancel() svr.Shutdown(ctxTimeout) diff --git a/internal/domain/news/request.go b/internal/domain/news/request.go new file mode 100644 index 0000000..c679d3b --- /dev/null +++ b/internal/domain/news/request.go @@ -0,0 +1,12 @@ +package newsdomain + +type News struct { + Title string `json:"title"` + Content string `json:"content"` + FeaturedImage string `json:"featured_image"` + Tags []string `json:"tags"` + Categories []string `json:"categories"` + IsPremium bool `json:"is_premium"` + Slug string `json:"slug"` + Author string `json:"author"` +} diff --git a/internal/utilities/utils/get_or_default.go b/internal/utilities/utils/get_or_default.go new file mode 100644 index 0000000..4e0ab06 --- /dev/null +++ b/internal/utilities/utils/get_or_default.go @@ -0,0 +1,34 @@ +package utils + +import ( + "os" + "reflect" + "strconv" +) + +func GetOrDefault[T any](key string, defaultValue T) T { + value, exist := os.LookupEnv(key) + + if !exist { + return defaultValue + } + + var valueInstance T + kind := reflect.ValueOf(valueInstance).Kind() + switch kind { + case reflect.String: + return any(value).(T) + case reflect.Int: + if val, err := strconv.Atoi(value); err == nil { + return any(val).(T) + } + return any(defaultValue).(T) + case reflect.Bool: + if val, err := strconv.ParseBool(value); err == nil { + return any(val).(T) + } + return any(defaultValue).(T) + default: + return any(defaultValue).(T) + } +}