forked from mtusi2026/mtusi2026lab2
122 lines
3.0 KiB
Go
122 lines
3.0 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func (c *Config) applyEnv(lookup func(string) (string, bool)) error {
|
|
get := func(key string) (string, bool) {
|
|
v, ok := lookup(key)
|
|
v = strings.TrimSpace(v)
|
|
return v, ok && v != ""
|
|
}
|
|
|
|
if v, ok := get("APP_ENV"); ok {
|
|
c.Env = v
|
|
}
|
|
if v, ok := get("APP_VERSION"); ok {
|
|
c.Version = v
|
|
}
|
|
if v, ok := get("APP_LOG_LEVEL"); ok {
|
|
c.Logger.Level = v
|
|
}
|
|
if v, ok := get("DATABASE_URL_FILE"); ok {
|
|
c.Database.URLFile = v
|
|
}
|
|
if v, ok := get("DATABASE_URL"); ok {
|
|
c.Database.URL = v
|
|
}
|
|
if v, ok := get("CACHE_URL_FILE"); ok {
|
|
c.Cache.URLFile = v
|
|
}
|
|
if v, ok := get("CACHE_URL"); ok {
|
|
c.Cache.URL = v
|
|
}
|
|
if v, ok := get("SESSION_STORE"); ok {
|
|
c.Session.Store = strings.ToLower(v)
|
|
}
|
|
if v, ok := get("HISTORY_CACHE"); ok {
|
|
c.HistoryCache.Mode = strings.ToLower(v)
|
|
}
|
|
if v, ok := get("HISTORY_CACHE_URL_FILE"); ok {
|
|
c.HistoryCache.URLFile = v
|
|
}
|
|
if v, ok := get("HISTORY_CACHE_URL"); ok {
|
|
c.HistoryCache.URL = v
|
|
}
|
|
|
|
host, port, err := net.SplitHostPort(c.HTTP.Addr)
|
|
if err != nil {
|
|
return fmt.Errorf("default addr %q: %w", c.HTTP.Addr, err)
|
|
}
|
|
if v, ok := get("APP_HOST"); ok {
|
|
host = v
|
|
}
|
|
if v, ok := get("APP_PORT"); ok {
|
|
port = v
|
|
}
|
|
if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 {
|
|
return fmt.Errorf("APP_PORT must be in [1, 65535], got %q", port)
|
|
}
|
|
c.HTTP.Addr = net.JoinHostPort(host, port)
|
|
|
|
ints := []struct {
|
|
key string
|
|
dst *int
|
|
}{
|
|
{"APP_READ_TIMEOUT_MS", &c.HTTP.ReadTimeoutMs},
|
|
{"APP_WRITE_TIMEOUT_MS", &c.HTTP.WriteTimeoutMs},
|
|
{"APP_IDLE_TIMEOUT_MS", &c.HTTP.IdleTimeoutMs},
|
|
{"APP_SHUTDOWN_TIMEOUT_MS", &c.HTTP.ShutdownTimeoutMs},
|
|
{"APP_MAX_INFLIGHT", &c.Limits.MaxInFlight},
|
|
{"APP_MAX_MESSAGES", &c.Limits.MaxMessagesGlobal},
|
|
{"DB_CONNECT_TIMEOUT_MS", &c.Database.ConnectTimeoutMs},
|
|
{"DB_QUERY_TIMEOUT_MS", &c.Database.QueryTimeoutMs},
|
|
{"CACHE_POOL_SIZE", &c.Cache.PoolSize},
|
|
{"CACHE_TIMEOUT_MS", &c.Cache.TimeoutMs},
|
|
{"SESSION_TTL_SECONDS", &c.Session.TTLSeconds},
|
|
{"HISTORY_CACHE_TTL_SECONDS", &c.HistoryCache.PageTTLSeconds},
|
|
{"HISTORY_CACHE_TIMEOUT_MS", &c.HistoryCache.TimeoutMs},
|
|
{"HISTORY_CACHE_POOL_SIZE", &c.HistoryCache.PoolSize},
|
|
{"HISTORY_CACHE_FILL_DELAY_MS", &c.HistoryCache.FillDelayMs},
|
|
}
|
|
for _, it := range ints {
|
|
v, ok := get(it.key)
|
|
if !ok {
|
|
continue
|
|
}
|
|
n, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: not an integer: %q", it.key, v)
|
|
}
|
|
*it.dst = n
|
|
}
|
|
|
|
if v, ok := get("APP_MAX_BODY_BYTES"); ok {
|
|
n, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("APP_MAX_BODY_BYTES: not an integer: %q", v)
|
|
}
|
|
c.Limits.MaxBodyBytes = n
|
|
}
|
|
if v, ok := get("DB_MAX_CONNS"); ok {
|
|
n, err := strconv.ParseInt(v, 10, 32)
|
|
if err != nil {
|
|
return fmt.Errorf("DB_MAX_CONNS: not an integer: %q", v)
|
|
}
|
|
c.Database.MaxConns = int32(n)
|
|
}
|
|
if v, ok := get("APP_MAX_HEAP_BYTES"); ok {
|
|
n, err := strconv.ParseUint(v, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("APP_MAX_HEAP_BYTES: not a non-negative integer: %q", v)
|
|
}
|
|
c.Limits.MaxHeapBytes = n
|
|
}
|
|
|
|
return nil
|
|
}
|