Skip to content

Commit

Permalink
feat(config): add config docs
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Apr 4, 2023
1 parent 0eb2c08 commit d65a2f8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 39 deletions.
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,67 @@ DATABASE_URL=postgres://postgres:password@localhost/gosd?sslmode=disable \

## Environment Variables

Read Environment Variables, Support `.env` file

```bash
cp dot.env .env
```

### Auth

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`SECRET` | JWT Secret | `falling-cats-and-dogs`
`API_KEY` | Admin Api. http header `X-Api-Key`, unset is disable | `the-elephant-in-the-room`
`BASIC_AUTH` | A auth plugin. support http base auth | `ture`

### Service URL

Dependent services

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`LISTEN_ADDR` | Address to listen on (use absolute path for Unix socket) | `0.0.0.0:8000`
`DEBUG` | Set the value to `1` to enable debug logs | `false`
`DEMO_MODE` | Auto Run `database migrate`, `database seed`, `node sync`| `false`
`DATABASE_URL` | Postgresql connection parameters | `postgres://postgres:password@localhost/gosd?sslmode=disable`
`STORAGE_URL` | File storage path | `data/storage`
`BASE_URL` | Base URL to generate API links and base path | `http://localhost:8000/gosd/api/v3`
`MQTT_URL` | MQTT broker Server address | `mqtt://admin:public@localhost:1883`
`REDIS_URL` | Redis Server URL | `redis://localhost:6379/0`
`STORAGE_URL` | File storage path | `data/storage`
`DATABASE_URL` | Postgresql connection parameters | `postgres://postgres:password@localhost/gosd?sslmode=disable`

### Public URL

Provide services, example: `nginx` gateway need change `BASE_URL`

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`BASE_URL` | Base URL to generate API links and base path | `http://localhost:8000/gosd/api/v3`
`CLIENT_URL` | Only `gosd client` use `BASE_URL` | `http://localhost:8000/gosd/api/v3`
`API_MQTT_WS` | MQTT broker Websocket server address | `ws://admin:public@localhost:1883`
`LISTEN_ADDR` | Address to listen on (use absolute path for Unix socket) | `0.0.0.0:8000`

### Feature Flags

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`SCHEDULE` | Only Single Node. **Not support cluster** | `true`
`LOG_FILE` | Log File | `STDOUT`
`LOG_LEVEL` | Log Level: `panic`, `fatal`, `error`, `warn`, `info`, `debug`, `trace` | `info`
`LUA_FILE` | Task lua > `LUA_FILE` > System Default (`luavm/lua/default.lua`) | `default.lua`
`EMQX_AUTH` | Use Emqx redis auth plugin. If Mosquitto, Set `false` | `false`
`LUA_FILE` | Task lua > `LUA_FILE` > Default(`luavm/lua/default.lua`) | `default.lua`

### Custom Config

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`INSTANCE` | Instance name | `gosd`
`LANGUAGE` | Language | `en_US`
`TIMEZONE` | Timezone | `Asia/Shanghai`
`LOG_LEVEL`| `panic`, `fatal`, `error`, `warn`, `info`, `debug`, `trace` | `info`

### Developer Flags

This System Default User have a `user.Id == 1`, `team.Id == 1` and `sess.Id == 1`.
The `SINGLE_USER` flag enable System Default User

Variable Name | Description | Default
-------------- | -------------------------------------------------------- | -------------------------------------------------------------
`DEBUG` | Set the value to `true` to enable debug logs | `false`
`DEMO_MODE` | Auto Run `database migrate`, `database seed`, `node sync`| `false`
`SINGLE_USER` | System Only One User. All belong user.Id == `1` | `false`

11 changes: 11 additions & 0 deletions app/cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ import (
)

func NewHandler(ctx context.Context, cfg *config.Config) http.Handler {
LevelMap := map[string]log.Level{
"panic": log.PanicLevel,
"fatal": log.FatalLevel,
"error": log.ErrorLevel,
"warn": log.WarnLevel,
"info": log.InfoLevel,
"debug": log.DebugLevel,
"trace": log.TraceLevel,
}
log.SetLevel(LevelMap[cfg.LogLevel])

log.Debugf("%+v\n", cfg)

orm, err := gorm.Open(postgres.Open(cfg.DatabaseURL), &gorm.Config{
Expand Down
78 changes: 48 additions & 30 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,70 @@ func init() {
}

type Config struct {
ListenAddr string `env:"LISTEN_ADDR"`
Instance string `env:"INSTANCE"`
BaseURL string `env:"BASE_URL"`
// Auth
Secret string `env:"SECRET"`
ApiKey string `env:"API_KEY"`
BasicAuth bool `env:"BASIC_AUTH"`

// Service
MqttURL string `env:"MQTT_URL"`
RedisURL string `env:"REDIS_URL"`
ClientURL string `env:"CLIENT_URL"`
DatabaseURL string `env:"DATABASE_URL"`
StorageURL string `env:"STORAGE_URL"`
LuaFilePath string `env:"LUA_FILE"`
Debug bool `env:"DEBUG"`
SingleUser bool `env:"SINGLE_USER"`
BasicAuth bool `env:"BASIC_AUTH"`
DemoMode bool `env:"DEMO_MODE"`
DatabaseURL string `env:"DATABASE_URL"`

// Public
BaseURL string `env:"BASE_URL"`
ClientURL string `env:"CLIENT_URL"`
ApiMqttWs string `env:"API_MQTT_WS"`
ListenAddr string `env:"LISTEN_ADDR"`

// Feature Flags
Schedule bool `env:"SCHEDULE"`
EmqxAuth bool `env:"EMQX_AUTH"`
Language string `env:"LANGUAGE"`
Timezone string `env:"TIMEZONE"`
ApiKey string `env:"API_KEY"`
ApiMqtt string `env:"API_MQTT"`
ApiMqttWs string `env:"API_MQTT_WS"`
Secret string `env:"SECRET"`
LuaFilePath string `env:"LUA_FILE"`
LogLevel string `env:"LOG_LEVEL"`

// Custom
Instance string `env:"INSTANCE"`
Language string `env:"LANGUAGE"`
Timezone string `env:"TIMEZONE"`

// Developer
Debug bool `env:"DEBUG"`
DemoMode bool `env:"DEMO_MODE"`
SingleUser bool `env:"SINGLE_USER"`
}

var opts = DefaultConfig()

func DefaultConfig() *Config {
return &Config{
ListenAddr: "0.0.0.0:8000",
Instance: "gosd.0",
BaseURL: "http://localhost:8000/gosd/api/v3",
Secret: "falling-cats-and-dogs",
ApiKey: "the-elephant-in-the-room",
BasicAuth: true,

MqttURL: "mqtt://admin:public@localhost:1883",
RedisURL: "redis://localhost:6379/1",
ClientURL: "http://localhost:8000/gosd/api/v3",
DatabaseURL: "postgres://postgres:password@localhost:5432/gosd?sslmode=disable&TimeZone=Asia/Shanghai",
StorageURL: "data/storage",
LuaFilePath: "default.lua",
Debug: true,
SingleUser: true,
BasicAuth: true,
DemoMode: false,
DatabaseURL: "postgres://postgres:password@localhost:5432/gosd?sslmode=disable&TimeZone=Asia/Shanghai",

BaseURL: "http://localhost:8000/gosd/api/v3",
ClientURL: "http://localhost:8000/gosd/api/v3",
ApiMqttWs: "ws://localhost:8083/mqtt",
ListenAddr: "0.0.0.0:8000",

Schedule: true,
EmqxAuth: false,
Language: "en_US",
Timezone: "Asia/Shanghai",
ApiMqtt: "mqtt://localhost:1883",
ApiMqttWs: "ws://localhost:8083/mqtt",
LuaFilePath: "default.lua",

Instance: "gosd",
Language: "en_US",
Timezone: "Asia/Shanghai",
LogLevel: "info",

Debug: true,
DemoMode: false,
SingleUser: true,
}
}

Expand Down

0 comments on commit d65a2f8

Please sign in to comment.