实现Web静态文件分发

This commit is contained in:
GoEdgeLab
2020-09-26 11:21:38 +08:00
parent 7e5a446cb3
commit 37b5243eef
7 changed files with 94 additions and 56 deletions

View File

@@ -0,0 +1,17 @@
package serverconfigs
// Web文档目录配置
type HTTPRootConfig struct {
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否优先
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
Dir string `yaml:"dir" json:"dir"` // 目录
Indexes []string `yaml:"indexes" json:"indexes"` // 默认首页文件
StripPrefix string `yaml:"stripPrefix" json:"stripPrefix"` // 去除前缀
DecodePath bool `yaml:"decodePath" json:"decodePath"` // 是否对请求路径进行解码
IsBreak bool `yaml:"isBreak" json:"isBreak"` // 找不到文件的情况下是否终止
}
// 初始化
func (ths *HTTPRootConfig) Init() error {
return nil
}

View File

@@ -13,9 +13,8 @@ type HTTPWebConfig struct {
Shutdown *HTTPShutdownConfig `yaml:"shutdown" json:"shutdown"` // 临时关闭配置
Pages []*HTTPPageConfig `yaml:"pages" json:"pages"` // 特殊页面配置
RedirectToHttps *HTTPRedirectToHTTPSConfig `yaml:"redirectToHttps" json:"redirectToHttps"` // 是否自动跳转到Https
Root string `yaml:"root" json:"root"` // 资源根目录 TODO
Indexes []string `yaml:"indexes" json:"indexes"` // 默认首页文件
MaxRequestBodySize string `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 请求body最大尺寸
Root *HTTPRootConfig `yaml:"root" json:"root"` // 资源根目录 TODO
MaxRequestBodySize string `yaml:"maxRequestBodySize" json:"maxRequestBodySize"` // 请求body最大尺寸 TODO 需要实现
AccessLogRef *HTTPAccessLogRef `yaml:"accessLog" json:"accessLog"` // 访问日志配置
StatRef *HTTPStatRef `yaml:"statRef" json:"statRef"` // 统计配置
CacheRef *HTTPCacheRef `yaml:"cacheRef" json:"cacheRef"` // 缓存配置
@@ -33,6 +32,14 @@ type HTTPWebConfig struct {
}
func (this *HTTPWebConfig) Init() error {
// root
if this.Root != nil {
err := this.Root.Init()
if err != nil {
return err
}
}
// 路径规则
if len(this.Locations) > 0 {
for _, location := range this.Locations {

View File

@@ -21,16 +21,20 @@ type HTTPHeaderPolicy struct {
Expires *HTTPExpireHeaderConfig `yaml:"expires" json:"expires"` // TODO
addHeaderNames []string
setHeaderNames []string
deleteHeaderMap map[string]bool // header => bool
}
// 校验
func (this *HTTPHeaderPolicy) Init() error {
this.addHeaderNames = []string{}
for _, h := range this.AddHeaders {
err := h.Init()
if err != nil {
return err
}
this.addHeaderNames = append(this.addHeaderNames, strings.ToUpper(h.Name))
}
for _, h := range this.AddTrailers {
@@ -40,11 +44,13 @@ func (this *HTTPHeaderPolicy) Init() error {
}
}
this.setHeaderNames = []string{}
for _, h := range this.SetHeaders {
err := h.Init()
if err != nil {
return err
}
this.setHeaderNames = append(this.setHeaderNames, strings.ToUpper(h.Name))
}
for _, h := range this.ReplaceHeaders {
@@ -68,8 +74,25 @@ func (this *HTTPHeaderPolicy) IsEmpty() bool {
return len(this.AddHeaders) == 0 && len(this.AddTrailers) == 0 && len(this.SetHeaders) == 0 && len(this.ReplaceHeaders) == 0 && this.Expires == nil
}
// 判断Add和Set中是否包含某个Header
func (this *HTTPHeaderPolicy) ContainsHeader(name string) bool {
name = strings.ToUpper(name)
for _, n := range this.addHeaderNames {
if n == name {
return true
}
}
for _, n := range this.setHeaderNames {
if n == name {
return true
}
}
return false
}
// 判断删除列表中是否包含某个Header
func (this *HTTPHeaderPolicy) ContainsDeletedHeader(header string) bool {
_, ok := this.deleteHeaderMap[strings.ToUpper(header)]
func (this *HTTPHeaderPolicy) ContainsDeletedHeader(name string) bool {
_, ok := this.deleteHeaderMap[strings.ToUpper(name)]
return ok
}