Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Env configuration #8

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ROOT_FS_STORAGE_DSN=
# VM_STORAGE_DSN=
FUNCTION_STATE_STORAGE_DSN='host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable TimeZone=Asia/Shanghai'
JWT_SECRET="I am so secret! Hopefully someone doesn't commit me.."
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# Grobuzin
# Grobuzin - ゴロブジ

## Cache dependencies / optimize go.mod

```
```bash
go mod tidy
```

## Build

```
```bash
go build
```

## Run

```
```bash
cp .env.example .env

# Change your environment variables accordingly
vim .env

go run .
```
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
14 changes: 7 additions & 7 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
services:
functionState:
VMState:
image: bitnami/redis:latest
environment:
- REDIS_PASSWORD=password
- REDIS_DATABASE=functionState
ports:
- 6379:6379
volumes:
- function_data:/bitnami/redis/data
- vm_data:/bitnami/redis/data

VMState:
functionState:
image: bitnami/postgresql:latest
environment:
- POSTGRESQL_PASSWORD=password
- POSTGRESQL_DATABASE=VMState
- POSTGRESQL_USERNAME=user
- POSTGRESQL_PASSWORD=postgres
- POSTGRESQL_DATABASE=postgres
- POSTGRESQL_USERNAME=postgres
ports:
- 5432:5432
volumes:
- vm_data:/bitnami/postgresql/data
- function_data:/bitnami/postgresql


FNStorage:
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ 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
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
golang.org/x/sync v0.6.0 // indirect
Expand Down
4 changes: 4 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 Expand Up @@ -46,6 +48,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down
27 changes: 18 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
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()
import _ "github.com/joho/godotenv/autoload"

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,notEmpty" 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 {
panic(err)
}

}
Loading