添加渲染器和反向代理开关
This commit is contained in:
@@ -81,6 +81,8 @@ func (c *Config) NewPageServerOptions() (*pkg.ServerOptions, error) {
|
|||||||
MaxCacheSize: int(cacheSize),
|
MaxCacheSize: int(cacheSize),
|
||||||
HttpClient: http.DefaultClient,
|
HttpClient: http.DefaultClient,
|
||||||
MetaTTL: time.Minute,
|
MetaTTL: time.Minute,
|
||||||
|
EnableRender: c.Render.Enable,
|
||||||
|
EnableProxy: c.Proxy.Enable,
|
||||||
DefaultErrorHandler: c.ErrorHandler,
|
DefaultErrorHandler: c.ErrorHandler,
|
||||||
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
Cache: utils.NewCacheMemory(int(cacheMaxSize), int(cacheMaxSize)),
|
||||||
}
|
}
|
||||||
@@ -135,12 +137,11 @@ type ConfigPage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ConfigProxy struct {
|
type ConfigProxy struct {
|
||||||
Enable bool `yaml:"enable"` // 是否允许反向代理模型
|
Enable bool `yaml:"enable"` // 是否允许反向代理
|
||||||
DenyList []string `yaml:"deny"` // 反向代理黑名单
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigRender struct {
|
type ConfigRender struct {
|
||||||
Enable bool `yaml:"enable"` // 开启渲染器
|
Enable bool `yaml:"enable"` // 是否开启渲染器
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConfigCache struct {
|
type ConfigCache struct {
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ type ServerOptions struct {
|
|||||||
|
|
||||||
MetaTTL time.Duration
|
MetaTTL time.Duration
|
||||||
|
|
||||||
|
EnableRender bool
|
||||||
|
EnableProxy bool
|
||||||
|
|
||||||
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
|
DefaultErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +58,8 @@ func DefaultOptions(domain string) ServerOptions {
|
|||||||
MaxCacheSize: 1024 * 1024 * 10,
|
MaxCacheSize: 1024 * 1024 * 10,
|
||||||
HttpClient: http.DefaultClient,
|
HttpClient: http.DefaultClient,
|
||||||
MetaTTL: time.Minute,
|
MetaTTL: time.Minute,
|
||||||
|
EnableRender: true,
|
||||||
|
EnableProxy: true,
|
||||||
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
DefaultErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
http.Error(w, "page not found.", http.StatusNotFound)
|
http.Error(w, "page not found.", http.StatusNotFound)
|
||||||
@@ -119,22 +124,24 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for prefix, backend := range meta.Proxy {
|
if s.options.EnableProxy {
|
||||||
proxyPath := "/" + meta.Path
|
for prefix, backend := range meta.Proxy {
|
||||||
if strings.HasPrefix(proxyPath, prefix) {
|
proxyPath := "/" + meta.Path
|
||||||
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
if strings.HasPrefix(proxyPath, prefix) {
|
||||||
if !strings.HasPrefix(targetPath, "/") {
|
targetPath := strings.TrimPrefix(proxyPath, prefix)
|
||||||
targetPath = "/" + targetPath
|
if !strings.HasPrefix(targetPath, "/") {
|
||||||
|
targetPath = "/" + targetPath
|
||||||
|
}
|
||||||
|
u, _ := url.Parse(backend)
|
||||||
|
request.URL.Path = targetPath
|
||||||
|
request.RequestURI = request.URL.RequestURI()
|
||||||
|
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||||
|
proxy.Transport = s.options.HttpClient.Transport
|
||||||
|
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
|
||||||
|
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
|
||||||
|
proxy.ServeHTTP(writer, request)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
u, _ := url.Parse(backend)
|
|
||||||
request.URL.Path = targetPath
|
|
||||||
request.RequestURI = request.URL.RequestURI()
|
|
||||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
|
||||||
proxy.Transport = s.options.HttpClient.Transport
|
|
||||||
zap.L().Debug("命中反向代理", zap.Any("prefix", prefix), zap.Any("backend", backend),
|
|
||||||
zap.Any("path", proxyPath), zap.Any("target", fmt.Sprintf("%s%s", u, targetPath)))
|
|
||||||
proxy.ServeHTTP(writer, request)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果不是反向代理路由则跳过任何配置
|
// 如果不是反向代理路由则跳过任何配置
|
||||||
@@ -174,7 +181,7 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
|||||||
}
|
}
|
||||||
writer.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
writer.Header().Set("Content-Type", mime.TypeByExtension(".html"))
|
||||||
writer.WriteHeader(http.StatusNotFound)
|
writer.WriteHeader(http.StatusNotFound)
|
||||||
if render := meta.TryRender(meta.Path, "/404.html"); render != nil {
|
if render := meta.TryRender(meta.Path, "/404.html"); render != nil && s.options.EnableRender {
|
||||||
defer result.Close()
|
defer result.Close()
|
||||||
if err = render.Render(writer, request, result); err != nil {
|
if err = render.Render(writer, request, result); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -191,6 +198,9 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
|
|||||||
}
|
}
|
||||||
fileName := filepath.Base(meta.Path)
|
fileName := filepath.Base(meta.Path)
|
||||||
render := meta.TryRender(meta.Path)
|
render := meta.TryRender(meta.Path)
|
||||||
|
if !s.options.EnableRender {
|
||||||
|
render = nil
|
||||||
|
}
|
||||||
defer result.Close()
|
defer result.Close()
|
||||||
if reader, ok := result.(*utils.CacheContent); ok {
|
if reader, ok := result.(*utils.CacheContent); ok {
|
||||||
writer.Header().Add("X-Cache", "HIT")
|
writer.Header().Add("X-Cache", "HIT")
|
||||||
|
|||||||
Reference in New Issue
Block a user