This commit is contained in:
oneao committed 2026-03-08 22:17:01 +08:00
1 parent 26ff2381b7
commit 00a1cce5ac
22 files changed
+335 -215

No files matched your search

Binary file not shown.
+26 -30
View File
@@ -3,9 +3,10 @@ package config
import (
"allapp/db"
"allapp/utils/redisx"
"bytes"
_ "embed"
"fmt"
"os"
"path/filepath"
"strings"
"time"
@@ -83,50 +84,45 @@ func defaultConfig() *Config {
}
}
//go:embed resources/application.yaml
var defaultYaml []byte
//go:embed resources/application-dev.yaml
var devYaml []byte
//go:embed resources/application-build.yaml
var buildYaml []byte
func readConfigFile() error {
// 配置文件的目录
prefix := "resources"
env := GetEnv() // 获取当前环境
conf = defaultConfig()
env := GetEnv()
// 设置默认值
conf = defaultConfig() // 先给默认结构体
// 默认配置文件
confFileRelPath := filepath.Join(prefix, "application.yaml")
// 创建 viper 实例
v := viper.New()
// 首先加载默认的 application.yaml
v.SetConfigFile(confFileRelPath)
v.SetConfigType("yaml")
// 读取默认配置文件
if err := v.ReadInConfig(); err != nil {
panic(err)
// 读取默认配置
if err := v.ReadConfig(bytes.NewReader(defaultYaml)); err != nil {
return fmt.Errorf("read default config failed: %w", err)
}
// 根据环境加载不同的配置文件
// 根据环境合并
switch env {
case "dev":
v.SetConfigFile(filepath.Join(prefix, "application-dev.yaml"))
case "build":
v.SetConfigFile(filepath.Join(prefix, "application-build.yaml"))
if err := v.MergeConfig(bytes.NewReader(buildYaml)); err != nil {
return fmt.Errorf("merge build config failed: %w", err)
}
default: // dev 或其他值都使用 dev
if err := v.MergeConfig(bytes.NewReader(devYaml)); err != nil {
return fmt.Errorf("merge dev config failed: %w", err)
}
}
// 如果需要,合并环境特定的配置文件
if err := v.MergeInConfig(); err != nil {
return fmt.Errorf("merge env config failed: %w", err)
}
if err := v.Unmarshal(conf); err != nil {
if err := v.Unmarshal(&conf); err != nil {
return fmt.Errorf("unmarshal config failed: %w", err)
}
fmt.Println("✅️ 加载配置成功")
// 设置环境变量
conf.Env = env
fmt.Println("✅ 加载配置成功, 环境:", env)
return nil
}
@@ -0,0 +1,17 @@
postgres:
host: 127.0.0.1
port: 5432
user: postgres
password: zhang520..
dbname: app_online
timezone: Asia/Shanghai
max_open_conns: 10
max_idle_conns: 5
conn_max_lifetime: 30m
sll_mode: false
redis:
host: 127.0.0.1
port: 6379
password: zhang520..
db: 0
+41
View File
@@ -110,3 +110,44 @@ create table finance_record
update_by bigint not null,
update_time timestamp with time zone default now()
);
-- session
CREATE TABLE session (
id bigint not null primary key,
space_id bigint not null,
type smallint not null,
name varchar(255),
avatar varchar(255),
description text,
status smallint default 0,
create_time timestamp with time zone default now(),
update_time timestamp with time zone default now()
)
-- session_member
create table session_member
(
id bigint not null
primary key,
session_id bigint not null,
user_id bigint not null,
remark varchar(255),
is_top smallint default 0 not null,
unread_count integer default 0,
last_msg_id bigint,
status integer default 0,
create_time timestamp with time zone default now(),
update_time timestamp with time zone default now()
);
create table session_message
(
id bigint not null
primary key,
session_id bigint not null,
sender_id bigint not null,
content text not null,
content_type smallint default 0 not null,
create_time timestamp with time zone default now()
);
Whitespace-only changes.