Files

48 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"os"
)
// Config 存放运行期配置, 所有值均可通过环境变量覆盖.
type Config struct {
Port string // HTTP 监听端口
DBPath string // SQLite 数据库路径
Token string // 管理员访问 token
LogLevel string
// 微软 OAuth个人 Outlook/HotmailMP_MS_CLIENT_ID 非空则启用
MSClientID string
MSClientSecret string
MSRedirectURL string // 须与 Azure 应用重定向 URI 完全一致,默认本机回调
FrontendURL string // OAuth 完成后浏览器跳回的前端根地址
// CORSMP_CORS_ORIGINS 为空或 * 时允许任意 Origin否则为英文逗号分隔的来源列表
CORSAllowOrigins string
}
// Load 读取环境变量生成配置, 未设置时使用默认值.
func Load() *Config {
return &Config{
Port: env("MP_PORT", "8787"),
DBPath: env("MP_DB", "mengpost.db"),
Token: env("MP_TOKEN", "shumengya520"),
LogLevel: env("MP_LOG", "info"),
MSClientID: env("MP_MS_CLIENT_ID", ""),
MSClientSecret: env("MP_MS_CLIENT_SECRET", ""),
MSRedirectURL: env("MP_MS_OAUTH_REDIRECT", "http://127.0.0.1:8787/api/oauth/microsoft/callback"),
FrontendURL: env("MP_FRONTEND_URL", "http://127.0.0.1:5173"),
CORSAllowOrigins: env("MP_CORS_ORIGINS", "*"),
}
}
// MicrosoftOAuthEnabled 是否配置了微软 OAuth需同时配置 Client ID.
func (c *Config) MicrosoftOAuthEnabled() bool {
return c.MSClientID != ""
}
func env(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}