fix: refactor layout init

This commit is contained in:
ericprd 2025-02-26 22:28:19 +08:00
parent ba075b67fd
commit 0100134527
16 changed files with 154 additions and 39 deletions

View File

@ -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) {

23
config/conf.go Normal file
View File

@ -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", "")
}

View File

@ -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{},
)
}

19
database/news_model.go Normal file
View File

@ -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"`
}

View File

@ -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"`

View File

@ -1,4 +1,4 @@
package subsmodel
package database
import (
"time"

15
database/tags_module.go Normal file
View File

@ -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"`
}

View File

@ -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"`
}

18
database/user_model.go Normal file
View File

@ -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"`
}

View File

@ -0,0 +1,7 @@
package staffrepository
func (ur *StaffRepository) Update() error {
// if err := ur.DB.Update(column string, value interface{})
return nil
}

View File

@ -0,0 +1,5 @@
package contenthttp
import "go.uber.org/fx"
var Module = fx.Module("content-api", fx.Invoke())

View File

@ -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)

View File

@ -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"`
}

View File

@ -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)
}
}