mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-19 14:22:00 +08:00
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:
parent
88c86787a0
commit
1fc1069ec1
@ -27,8 +27,8 @@ COPY --from=builder /app/public ./public
|
|||||||
# 复制 public 目录到一个临时位置
|
# 复制 public 目录到一个临时位置
|
||||||
COPY --from=builder /app/public /tmp/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
|
EXPOSE 5003
|
||||||
|
|
||||||
|
@ -2,8 +2,10 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,6 +41,60 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Load(configFile string) error {
|
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)
|
file, err := os.Open(configFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -50,6 +106,7 @@ func Load(configFile string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果环境变量设置了 BASE_URL,则覆盖配置文件中的设置
|
||||||
if envBaseURL := os.Getenv(EnvBaseURL); envBaseURL != "" {
|
if envBaseURL := os.Getenv(EnvBaseURL); envBaseURL != "" {
|
||||||
cfg.API.BaseURL = envBaseURL
|
cfg.API.BaseURL = envBaseURL
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "5003:5003"
|
- "5003:5003"
|
||||||
volumes:
|
volumes:
|
||||||
- ./public:/root/public
|
|
||||||
- ./logs:/var/log/random-api
|
|
||||||
- ./data:/root/data
|
- ./data:/root/data
|
||||||
environment:
|
environment:
|
||||||
- TZ=Asia/Shanghai
|
- TZ=Asia/Shanghai
|
||||||
|
@ -21,7 +21,7 @@ func New() *Router {
|
|||||||
|
|
||||||
func (r *Router) Setup(h Handler) {
|
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(
|
r.mux.Handle("/", middleware.Chain(
|
||||||
middleware.Recovery,
|
middleware.Recovery,
|
||||||
middleware.MetricsMiddleware,
|
middleware.MetricsMiddleware,
|
||||||
|
8
start.sh
8
start.sh
@ -1,9 +1,13 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
# 如果挂载的 public 目录为空,则从临时位置复制文件
|
# 如果挂载的 public 目录为空,则从临时位置复制文件
|
||||||
if [ ! "$(ls -A /root/public)" ]; then
|
if [ ! "$(ls -A /root/data/public)" ]; then
|
||||||
cp -r /tmp/public/* /root/public/
|
mkdir -p /root/data/public
|
||||||
|
cp -r /tmp/public/* /root/data/public/
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# 创建其他必要的目录
|
||||||
|
mkdir -p /root/data/logs
|
||||||
|
|
||||||
# 启动应用
|
# 启动应用
|
||||||
./random-api
|
./random-api
|
||||||
|
Loading…
x
Reference in New Issue
Block a user