refactor(docker, config): streamline directory structure and enhance configuration management

- Removed unnecessary volume mounts for public and logs in docker-compose.yml.
- Updated Dockerfile to create necessary directories under /root/data.
- Modified start.sh to copy files to the new public directory location.
- Enhanced config.go to create default configuration and directory structure if not present.
- Adjusted router.go to serve static files from the new public directory path.
This commit is contained in:
wood chen 2024-11-30 23:34:45 +08:00
parent 88c86787a0
commit 1fc1069ec1
5 changed files with 66 additions and 7 deletions

View File

@ -27,8 +27,8 @@ COPY --from=builder /app/public ./public
# 复制 public 目录到一个临时位置
COPY --from=builder /app/public /tmp/public
# 创建日志目录并设置权限
RUN mkdir -p /var/log/random-api && chmod 755 /var/log/random-api
# 创建必要的目录
RUN mkdir -p /root/data/logs /root/data/public
EXPOSE 5003

View File

@ -2,8 +2,10 @@ package config
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"path/filepath"
"time"
)
@ -39,6 +41,60 @@ var (
)
func Load(configFile string) error {
// 尝试创建配置目录
configDir := filepath.Dir(configFile)
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("failed to create config directory: %w", err)
}
// 检查配置文件是否存在
if _, err := os.Stat(configFile); os.IsNotExist(err) {
// 创建默认配置
defaultConfig := Config{
Server: struct {
Port string `json:"port"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
MaxHeaderBytes int `json:"max_header_bytes"`
}{
Port: ":5003",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
},
Storage: struct {
DataDir string `json:"data_dir"`
StatsFile string `json:"stats_file"`
LogFile string `json:"log_file"`
}{
DataDir: "/root/data",
StatsFile: "/root/data/stats.json",
LogFile: "/root/data/logs/server.log",
},
API: struct {
BaseURL string `json:"base_url"`
RequestTimeout time.Duration `json:"request_timeout"`
}{
BaseURL: "",
RequestTimeout: 10 * time.Second,
},
}
// 将默认配置写入文件
data, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal default config: %w", err)
}
if err := os.WriteFile(configFile, data, 0644); err != nil {
return fmt.Errorf("failed to write default config: %w", err)
}
cfg = defaultConfig
return nil
}
// 读取现有配置文件
file, err := os.Open(configFile)
if err != nil {
return err
@ -50,6 +106,7 @@ func Load(configFile string) error {
return err
}
// 如果环境变量设置了 BASE_URL则覆盖配置文件中的设置
if envBaseURL := os.Getenv(EnvBaseURL); envBaseURL != "" {
cfg.API.BaseURL = envBaseURL
}

View File

@ -5,8 +5,6 @@ services:
ports:
- "5003:5003"
volumes:
- ./public:/root/public
- ./logs:/var/log/random-api
- ./data:/root/data
environment:
- TZ=Asia/Shanghai

View File

@ -21,7 +21,7 @@ func New() *Router {
func (r *Router) Setup(h Handler) {
// 静态文件服务
fileServer := http.FileServer(http.Dir("./public"))
fileServer := http.FileServer(http.Dir("/root/data/public"))
r.mux.Handle("/", middleware.Chain(
middleware.Recovery,
middleware.MetricsMiddleware,

View File

@ -1,9 +1,13 @@
#!/bin/sh
# 如果挂载的 public 目录为空,则从临时位置复制文件
if [ ! "$(ls -A /root/public)" ]; then
cp -r /tmp/public/* /root/public/
if [ ! "$(ls -A /root/data/public)" ]; then
mkdir -p /root/data/public
cp -r /tmp/public/* /root/data/public/
fi
# 创建其他必要的目录
mkdir -p /root/data/logs
# 启动应用
./random-api