2023-12-14 22:02:31 +08:00
|
|
|
package server
|
|
|
|
|
2023-12-15 21:45:04 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-12-21 18:36:51 +08:00
|
|
|
"path/filepath"
|
2023-12-15 21:45:04 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.pyer.club/kingecg/gologger"
|
|
|
|
)
|
2023-12-14 22:02:31 +08:00
|
|
|
|
|
|
|
type Directive func(args ...string) Middleware
|
|
|
|
|
|
|
|
var Add_Header Directive = func(args ...string) Middleware {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
2023-12-15 21:45:04 +08:00
|
|
|
l := gologger.GetLogger("Directive")
|
|
|
|
p := args[1:]
|
|
|
|
params := strings.Join(p, " ")
|
|
|
|
l.Debug(fmt.Sprintf("Add-Header %s:%s", args[0], params))
|
2023-12-14 22:02:31 +08:00
|
|
|
w.Header().Add(args[0], args[1])
|
2023-12-14 23:47:04 +08:00
|
|
|
next()
|
2023-12-14 22:02:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var Set_Header Directive = func(args ...string) Middleware {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
2023-12-15 21:45:04 +08:00
|
|
|
l := gologger.GetLogger("Directive")
|
|
|
|
p := args[1:]
|
|
|
|
params := strings.Join(p, " ")
|
|
|
|
l.Debug(fmt.Sprintf("Set-Header %s:%s", args[0], params))
|
|
|
|
w.Header().Set(args[0], params)
|
2023-12-14 23:47:04 +08:00
|
|
|
next()
|
2023-12-14 22:02:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 18:36:51 +08:00
|
|
|
var Gzip_Response Directive = func(args ...string) Middleware {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, next func()) {
|
|
|
|
l := gologger.GetLogger("Directive")
|
|
|
|
l.Debug("Gzip-Response")
|
|
|
|
if filepath.Ext(r.URL.Path) != "" {
|
|
|
|
ctx := r.Context()
|
|
|
|
m := ctx.Value(RequestCtxKey("data")).(map[string]interface{})
|
|
|
|
m["Tg"] = "gzip"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
2023-12-14 22:02:31 +08:00
|
|
|
var DirectiveMap = map[string]Directive{
|
2023-12-21 18:36:51 +08:00
|
|
|
"Set-Header": Set_Header,
|
|
|
|
"Add-Header": Add_Header,
|
|
|
|
"Gzip_Response": Gzip_Response,
|
2023-12-14 22:02:31 +08:00
|
|
|
}
|