forked from mtusi2026/mtusi2026lab6
177 lines
4.5 KiB
Go
177 lines
4.5 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_INSTANCE_ID"); ok {
|
|
c.InstanceID = 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("S3_ENDPOINT"); ok {
|
|
c.S3.Endpoint = v
|
|
}
|
|
if v, ok := get("S3_REGION"); ok {
|
|
c.S3.Region = v
|
|
}
|
|
if v, ok := get("S3_BUCKET"); ok {
|
|
c.S3.Bucket = v
|
|
}
|
|
if v, ok := get("S3_ACCESS_KEY_ID_FILE"); ok {
|
|
c.S3.AccessKeyIDFile = v
|
|
}
|
|
if v, ok := get("S3_SECRET_ACCESS_KEY_FILE"); ok {
|
|
c.S3.SecretAccessKeyFile = 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
|
|
}
|
|
|
|
if v, ok := get("JANITOR_DRY_RUN"); ok {
|
|
dryRun, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return fmt.Errorf("JANITOR_DRY_RUN: not a boolean: %q", v)
|
|
}
|
|
c.Janitor.DryRun = dryRun
|
|
}
|
|
|
|
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_SHUTDOWN_DRAIN_MS", &c.HTTP.ShutdownDrainMs},
|
|
{"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},
|
|
{"S3_TIMEOUT_MS", &c.S3.TimeoutMs},
|
|
{"MAX_CONCURRENT_UPLOADS", &c.Attachments.MaxConcurrentUploads},
|
|
{"JANITOR_GRACE_SECONDS", &c.Janitor.GraceSeconds},
|
|
{"JANITOR_INTERVAL_SECONDS", &c.Janitor.IntervalSeconds},
|
|
{"JANITOR_MAX_DELETES", &c.Janitor.MaxDeletes},
|
|
{"CACHE_POOL_SIZE", &c.Cache.PoolSize},
|
|
{"CACHE_TIMEOUT_MS", &c.Cache.TimeoutMs},
|
|
{"SESSION_TTL_SECONDS", &c.Session.TTLSeconds},
|
|
{"IDEMPOTENCY_TTL_SECONDS", &c.Idempotency.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
|
|
}
|
|
for _, it := range []struct {
|
|
key string
|
|
dst *int64
|
|
}{
|
|
{"MAX_UPLOAD_BYTES", &c.Attachments.MaxUploadBytes},
|
|
{"MAX_IMAGE_PIXELS", &c.Attachments.MaxImagePixels},
|
|
} {
|
|
if v, ok := get(it.key); ok {
|
|
n, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
return fmt.Errorf("%s: not an integer: %q", it.key, v)
|
|
}
|
|
*it.dst = 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("DB_ENABLE_BITMAPSCAN"); ok {
|
|
b, err := strconv.ParseBool(v)
|
|
if err != nil {
|
|
return fmt.Errorf("DB_ENABLE_BITMAPSCAN: not a boolean: %q", v)
|
|
}
|
|
c.Database.EnableBitmapScan = b
|
|
}
|
|
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
|
|
}
|