实现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

@@ -30,9 +30,8 @@ type HTTPWeb struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
IsOn bool `protobuf:"varint,2,opt,name=isOn,proto3" json:"isOn,omitempty"`
Root string `protobuf:"bytes,3,opt,name=root,proto3" json:"root,omitempty"`
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
IsOn bool `protobuf:"varint,2,opt,name=isOn,proto3" json:"isOn,omitempty"`
}
func (x *HTTPWeb) Reset() {
@@ -81,23 +80,15 @@ func (x *HTTPWeb) GetIsOn() bool {
return false
}
func (x *HTTPWeb) GetRoot() string {
if x != nil {
return x.Root
}
return ""
}
var File_model_http_web_proto protoreflect.FileDescriptor
var file_model_http_web_proto_rawDesc = []byte{
0x0a, 0x14, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x5f, 0x68, 0x74, 0x74, 0x70, 0x5f, 0x77, 0x65, 0x62,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x22, 0x41, 0x0a, 0x07, 0x48, 0x54,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x22, 0x2d, 0x0a, 0x07, 0x48, 0x54,
0x54, 0x50, 0x57, 0x65, 0x62, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x18, 0x02, 0x20,
0x01, 0x28, 0x08, 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x6f, 0x6f,
0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x42, 0x06, 0x5a,
0x04, 0x2e, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x01, 0x28, 0x08, 0x52, 0x04, 0x69, 0x73, 0x4f, 0x6e, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x70,
0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@@ -35,7 +35,7 @@ type CreateHTTPWebRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Root string `protobuf:"bytes,1,opt,name=root,proto3" json:"root,omitempty"`
RootJSON []byte `protobuf:"bytes,1,opt,name=rootJSON,proto3" json:"rootJSON,omitempty"`
}
func (x *CreateHTTPWebRequest) Reset() {
@@ -70,11 +70,11 @@ func (*CreateHTTPWebRequest) Descriptor() ([]byte, []int) {
return file_service_http_web_proto_rawDescGZIP(), []int{0}
}
func (x *CreateHTTPWebRequest) GetRoot() string {
func (x *CreateHTTPWebRequest) GetRootJSON() []byte {
if x != nil {
return x.Root
return x.RootJSON
}
return ""
return nil
}
type CreateHTTPWebResponse struct {
@@ -320,8 +320,8 @@ type UpdateHTTPWebRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
WebId int64 `protobuf:"varint,1,opt,name=webId,proto3" json:"webId,omitempty"`
Root string `protobuf:"bytes,2,opt,name=root,proto3" json:"root,omitempty"`
WebId int64 `protobuf:"varint,1,opt,name=webId,proto3" json:"webId,omitempty"`
RootJSON []byte `protobuf:"bytes,2,opt,name=rootJSON,proto3" json:"rootJSON,omitempty"`
}
func (x *UpdateHTTPWebRequest) Reset() {
@@ -363,11 +363,11 @@ func (x *UpdateHTTPWebRequest) GetWebId() int64 {
return 0
}
func (x *UpdateHTTPWebRequest) GetRoot() string {
func (x *UpdateHTTPWebRequest) GetRootJSON() []byte {
if x != nil {
return x.Root
return x.RootJSON
}
return ""
return nil
}
// 更改Gzip配置
@@ -1049,31 +1049,32 @@ var file_service_http_web_proto_rawDesc = []byte{
0x65, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x02, 0x70, 0x62, 0x1a, 0x14, 0x6d, 0x6f,
0x64, 0x65, 0x6c, 0x5f, 0x68, 0x74, 0x74, 0x70, 0x5f, 0x77, 0x65, 0x62, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x1a, 0x12, 0x72, 0x70, 0x63, 0x5f, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x73,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x2a, 0x0a, 0x14, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12,
0x0a, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x6f,
0x6f, 0x74, 0x22, 0x2d, 0x0a, 0x15, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50,
0x57, 0x65, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x77,
0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49,
0x64, 0x22, 0x31, 0x0a, 0x19, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64,
0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14,
0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77,
0x65, 0x62, 0x49, 0x64, 0x22, 0x3b, 0x0a, 0x1a, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62,
0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
0x73, 0x65, 0x12, 0x1d, 0x0a, 0x03, 0x77, 0x65, 0x62, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32,
0x0b, 0x2e, 0x70, 0x62, 0x2e, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x03, 0x77, 0x65,
0x62, 0x22, 0x37, 0x0a, 0x1f, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64,
0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71,
0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20,
0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x22, 0x3c, 0x0a, 0x20, 0x46, 0x69,
0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62,
0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18,
0x0a, 0x07, 0x77, 0x65, 0x62, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52,
0x07, 0x77, 0x65, 0x62, 0x4a, 0x53, 0x4f, 0x4e, 0x22, 0x40, 0x0a, 0x14, 0x55, 0x70, 0x64, 0x61,
0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52,
0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x72, 0x6f, 0x6f, 0x74, 0x22, 0x4c, 0x0a, 0x18, 0x55, 0x70,
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x32, 0x0a, 0x14, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a,
0x0a, 0x08, 0x72, 0x6f, 0x6f, 0x74, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c,
0x52, 0x08, 0x72, 0x6f, 0x6f, 0x74, 0x4a, 0x53, 0x4f, 0x4e, 0x22, 0x2d, 0x0a, 0x15, 0x43, 0x72,
0x65, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f,
0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01,
0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x22, 0x31, 0x0a, 0x19, 0x46, 0x69, 0x6e,
0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x22, 0x3b, 0x0a, 0x1a,
0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x57,
0x65, 0x62, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1d, 0x0a, 0x03, 0x77, 0x65,
0x62, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x70, 0x62, 0x2e, 0x48, 0x54, 0x54,
0x50, 0x57, 0x65, 0x62, 0x52, 0x03, 0x77, 0x65, 0x62, 0x22, 0x37, 0x0a, 0x1f, 0x46, 0x69, 0x6e,
0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65, 0x64, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x43,
0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05,
0x77, 0x65, 0x62, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62,
0x49, 0x64, 0x22, 0x3c, 0x0a, 0x20, 0x46, 0x69, 0x6e, 0x64, 0x45, 0x6e, 0x61, 0x62, 0x6c, 0x65,
0x64, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x52, 0x65,
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x77, 0x65, 0x62, 0x4a, 0x53, 0x4f,
0x4e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x77, 0x65, 0x62, 0x4a, 0x53, 0x4f, 0x4e,
0x22, 0x48, 0x0a, 0x14, 0x55, 0x70, 0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65,
0x62, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49,
0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x12, 0x1a,
0x0a, 0x08, 0x72, 0x6f, 0x6f, 0x74, 0x4a, 0x53, 0x4f, 0x4e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c,
0x52, 0x08, 0x72, 0x6f, 0x6f, 0x74, 0x4a, 0x53, 0x4f, 0x4e, 0x22, 0x4c, 0x0a, 0x18, 0x55, 0x70,
0x64, 0x61, 0x74, 0x65, 0x48, 0x54, 0x54, 0x50, 0x57, 0x65, 0x62, 0x47, 0x7a, 0x69, 0x70, 0x52,
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x05, 0x77, 0x65, 0x62, 0x49, 0x64, 0x12, 0x1a, 0x0a, 0x08,

View File

@@ -6,5 +6,4 @@ package pb;
message HTTPWeb {
int64 id = 1;
bool isOn = 2;
string root = 3;
}

View File

@@ -58,7 +58,7 @@ service HTTPWebService {
// 创建Web配置
message CreateHTTPWebRequest {
string root = 1;
bytes rootJSON = 1;
}
message CreateHTTPWebResponse {
@@ -86,7 +86,7 @@ message FindEnabledHTTPWebConfigResponse {
// 更改Web配置
message UpdateHTTPWebRequest {
int64 webId = 1;
string root = 2;
bytes rootJSON = 2;
}
// 更改Gzip配置

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
}