Skip to content

Commit

Permalink
fix(upload): add config option to solve request entity too large issue (
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Apr 18, 2024
1 parent 0b16c96 commit 6e13152
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions conf/artalk.example.simple.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ db:
charset: utf8mb4
ssl: false
prepare_stmt: true
http:
body_limit: 100
log:
enabled: true
filename: ./data/artalk.log
Expand Down
4 changes: 4 additions & 0 deletions conf/artalk.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ db:
# Prepared Statement
prepare_stmt: true

# Web server
http:
# Body size limit (unit: MB)
body_limit: 100
# Logging
log:
# Enable logging
Expand Down
4 changes: 4 additions & 0 deletions conf/artalk.example.zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ db:
# 预编译语句
prepare_stmt: true

# 服务器
http:
# 请求体大小限制 (单位:MB)
body_limit: 100
# 日志
log:
# 启用日志
Expand Down
5 changes: 5 additions & 0 deletions internal/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func (conf *Config) normalPatch() {
conf.ImgUpload.Path = "./data/artalk-img/"
log.Warn("[Image Upload] img_upload.path is not configured, using the default value: " + strconv.Quote(conf.ImgUpload.Path))
}

// HTTP 配置默认值
if conf.HTTP.BodyLimit <= 0 {
conf.HTTP.BodyLimit = 100
}
}

// 多语言配置修补
Expand Down
13 changes: 13 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
Host string `koanf:"host" json:"host"` // HTTP Server 监听 IP
Port int `koanf:"port" json:"port"` // HTTP Server 监听 Port
DB DBConf `koanf:"db" json:"db"` // 数据库配置
HTTP HTTPConf `koanf:"http" json:"http"` // HTTP 配置
Cache CacheConf `koanf:"cache" json:"cache"` // 缓存
Log LogConf `koanf:"log" json:"log"` // 日志文件
TrustedDomains []string `koanf:"trusted_domains" json:"trusted_domains"` // 可信任的域名 (新)
Expand Down Expand Up @@ -42,6 +43,18 @@ type Config struct {
cfgFile string
}

type HTTPConf struct {
BodyLimit int `koanf:"body_limit" json:"body_limit"` // 请求体大小限制 MB
ProxyHeader *string `koanf:"proxy_header" json:"proxy_header"` // 代理头
}

func (c *HTTPConf) GetProxyHeader() string {
if c.ProxyHeader == nil {
return ""
}
return *c.ProxyHeader
}

type DBConf struct {
Type DBType `koanf:"type" json:"type"`
Dsn string `koanf:"dsn" json:"dsn"` // 最高优先级
Expand Down
7 changes: 3 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ func Serve(app *core.App) (*fiber.App, error) {
// @see https://github.com/gofiber/fiber/issues/185
Immutable: true,

// TODO add config to set ProxyHeader
ProxyHeader: "X-Forwarded-For",
ErrorHandler: common.ErrorHandler,
BodyLimit: app.Conf().HTTP.BodyLimit * 1024 * 1024, // MB
StreamRequestBody: true,
EnableIPValidation: true,

ErrorHandler: common.ErrorHandler,
})

reqID(fb)
Expand Down

0 comments on commit 6e13152

Please sign in to comment.