Skip to content

Commit

Permalink
feat: use env to handle env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
WoodenMaiden committed Apr 11, 2024
1 parent dab2082 commit edbb274
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
7 changes: 1 addition & 6 deletions database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ var (
models = []interface{}{&User{}}
)

func Init() *gorm.DB {

// Code to initialize database connection

dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"

func Init(dsn string) *gorm.DB {
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
)

require (
github.com/caarlos0/env/v10 v10.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA=
github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
Expand Down
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package main

import (
"os"
"log"

"github.com/do4-2022/grobuzin/database"
"github.com/do4-2022/grobuzin/routes"

"github.com/caarlos0/env/v10"
)

func main() {
db := database.Init()
type Config struct {
// rootFsStorageDSN string `env:"ROOT_FS_STORAGE_DSN,notEmpty"`
// VMStorageDSN string `env:"VM_STORAGE_DSN,notEmpty"`
funtionStateStorageDSN string `env:"FUNCTION_STATE_STORAGE_DSN" envDefault:"host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai"`
JWTSecret string `env:"JWT_SECRET,notEmpty"`
}

// get from env
JWTSecret := os.Getenv("JWT_SECRET")
if JWTSecret == "" {
panic("JWT_SECRET is not set")
func main() {
cfg := Config{}
if err := env.Parse(&cfg); err != nil {
log.Fatalf("%+v\n", err)
}

r := routes.GetRoutes(db, JWTSecret)
db := database.Init(cfg.funtionStateStorageDSN)
r := routes.GetRoutes(db, cfg.JWTSecret)

err := r.Run()

if err != nil {
Expand Down

0 comments on commit edbb274

Please sign in to comment.