fix: refactor layout init
This commit is contained in:
parent
ba075b67fd
commit
0100134527
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/ardeman/project-legalgo-go/config"
|
||||||
"github.com/ardeman/project-legalgo-go/database"
|
"github.com/ardeman/project-legalgo-go/database"
|
||||||
repository "github.com/ardeman/project-legalgo-go/internal/accessor"
|
repository "github.com/ardeman/project-legalgo-go/internal/accessor"
|
||||||
internalhttp "github.com/ardeman/project-legalgo-go/internal/api/http"
|
internalhttp "github.com/ardeman/project-legalgo-go/internal/api/http"
|
||||||
@ -19,6 +20,8 @@ func init() {
|
|||||||
if err := godotenv.Load(); err != nil {
|
if err := godotenv.Load(); err != nil {
|
||||||
log.Fatal("cannot load environment file")
|
log.Fatal("cannot load environment file")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config.InitEnv()
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(lc fx.Lifecycle, db *database.DB, apiRouter chi.Router) {
|
func run(lc fx.Lifecycle, db *database.DB, apiRouter chi.Router) {
|
||||||
|
|||||||
23
config/conf.go
Normal file
23
config/conf.go
Normal 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", "")
|
||||||
|
}
|
||||||
@ -2,11 +2,8 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
staffmodel "github.com/ardeman/project-legalgo-go/database/staff"
|
"github.com/ardeman/project-legalgo-go/config"
|
||||||
subsmodel "github.com/ardeman/project-legalgo-go/database/subscribe"
|
|
||||||
usermodel "github.com/ardeman/project-legalgo-go/database/user"
|
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
@ -16,16 +13,15 @@ type DB struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewDB() (*DB, error) {
|
func NewDB() (*DB, error) {
|
||||||
var (
|
dsn := fmt.Sprintf(
|
||||||
host = os.Getenv("DB_HOST")
|
"host=%s user=%s password=%s dbname=%s port=%v sslmode=disable",
|
||||||
user = os.Getenv("DB_USER")
|
config.DB_HOST,
|
||||||
password = os.Getenv("DB_PASSWORD")
|
config.DB_USER,
|
||||||
dbName = os.Getenv("DB_NAME")
|
config.DB_PASSWORD,
|
||||||
dbPort = os.Getenv("DB_PORT")
|
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 == "" {
|
if dsn == "" {
|
||||||
return nil, fmt.Errorf("DATABASE_URL environment is not set")
|
return nil, fmt.Errorf("DATABASE_URL environment is not set")
|
||||||
}
|
}
|
||||||
@ -42,9 +38,9 @@ func NewDB() (*DB, error) {
|
|||||||
func (db *DB) Migrate() error {
|
func (db *DB) Migrate() error {
|
||||||
// Auto Migrate the User model
|
// Auto Migrate the User model
|
||||||
return db.AutoMigrate(
|
return db.AutoMigrate(
|
||||||
&staffmodel.Staff{},
|
&Staff{},
|
||||||
&subsmodel.SubscribePlan{},
|
&SubscribePlan{},
|
||||||
&subsmodel.Subscribe{},
|
&Subscribe{},
|
||||||
&usermodel.User{},
|
&User{},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
19
database/news_model.go
Normal file
19
database/news_model.go
Normal 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"`
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package staffmodel
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
type Staff struct {
|
type Staff struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
||||||
|
Username string `gorm:"default:null" json:"username"`
|
||||||
Email string `gorm:"unique,not null" json:"email"`
|
Email string `gorm:"unique,not null" json:"email"`
|
||||||
Password string `gorm:"not null" json:"password"`
|
Password string `gorm:"not null" json:"password"`
|
||||||
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package subsmodel
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
15
database/tags_module.go
Normal file
15
database/tags_module.go
Normal 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"`
|
||||||
|
}
|
||||||
@ -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
18
database/user_model.go
Normal 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"`
|
||||||
|
}
|
||||||
7
internal/accessor/staff/update.go
Normal file
7
internal/accessor/staff/update.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package staffrepository
|
||||||
|
|
||||||
|
func (ur *StaffRepository) Update() error {
|
||||||
|
// if err := ur.DB.Update(column string, value interface{})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
5
internal/api/http/content/module.go
Normal file
5
internal/api/http/content/module.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package contenthttp
|
||||||
|
|
||||||
|
import "go.uber.org/fx"
|
||||||
|
|
||||||
|
var Module = fx.Module("content-api", fx.Invoke())
|
||||||
@ -8,6 +8,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/ardeman/project-legalgo-go/config"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
@ -28,14 +29,14 @@ func Router(apiRouter chi.Router) {
|
|||||||
group, groupCtx := errgroup.WithContext(mainCtx)
|
group, groupCtx := errgroup.WithContext(mainCtx)
|
||||||
|
|
||||||
group.Go(func() error {
|
group.Go(func() error {
|
||||||
logrus.Infof("Listening to port %d", APP_PORT)
|
logrus.Infof("Listening to port %d", config.APP_PORT)
|
||||||
return svr.ListenAndServe()
|
return svr.ListenAndServe()
|
||||||
})
|
})
|
||||||
|
|
||||||
group.Go(func() error {
|
group.Go(func() error {
|
||||||
<-groupCtx.Done()
|
<-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()
|
defer cancel()
|
||||||
|
|
||||||
svr.Shutdown(ctxTimeout)
|
svr.Shutdown(ctxTimeout)
|
||||||
|
|||||||
12
internal/domain/news/request.go
Normal file
12
internal/domain/news/request.go
Normal 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"`
|
||||||
|
}
|
||||||
34
internal/utilities/utils/get_or_default.go
Normal file
34
internal/utilities/utils/get_or_default.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user