gohttp/server/server.go

269 lines
6.5 KiB
Go
Raw Permalink Normal View History

2023-12-07 22:42:27 +08:00
package server
import (
2023-12-11 23:46:40 +08:00
"context"
2023-12-13 09:22:05 +08:00
"fmt"
2023-12-07 22:42:27 +08:00
"net/http"
2023-12-12 00:42:21 +08:00
"sort"
2023-12-07 22:42:27 +08:00
"strings"
2023-12-13 09:22:05 +08:00
handler "git.pyer.club/kingecg/gohttpd/hander"
"git.pyer.club/kingecg/gohttpd/model"
2023-12-13 09:22:05 +08:00
"git.pyer.club/kingecg/gologger"
logger "git.pyer.club/kingecg/gologger"
2023-12-07 22:42:27 +08:00
)
2023-12-11 23:46:40 +08:00
type RequestCtxKey string
2023-12-12 00:42:21 +08:00
type Route struct {
Method string
Path string
Handler http.Handler
matcher *UrlParamMatcher
2023-12-12 21:44:35 +08:00
middles *MiddlewareLink
2023-12-12 00:42:21 +08:00
}
func (route *Route) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2023-12-12 21:44:35 +08:00
if route.middles.Len() > 0 && !route.middles.ServeHTTP(w, r) {
return
2023-12-12 00:42:21 +08:00
}
2023-12-12 21:44:35 +08:00
route.Handler.ServeHTTP(w, r)
2023-12-12 00:42:21 +08:00
}
func (route *Route) Match(r *http.Request) bool {
2023-12-13 09:22:05 +08:00
l := gologger.GetLogger("Route")
l.Debug(fmt.Sprintf("match route: %s %s", r.Method, r.URL.Path))
2023-12-12 00:42:21 +08:00
if route.Method != "" && route.Method != r.Method {
2023-12-13 09:22:05 +08:00
l.Debug("method not match")
2023-12-12 00:42:21 +08:00
return false
}
2023-12-13 09:22:05 +08:00
if route.matcher != nil && route.matcher.Reg != nil {
2023-12-12 00:42:21 +08:00
params, matched := MatchUrlParam(r.URL.Path, route.matcher)
if matched {
ctx := r.Context()
data := ctx.Value(RequestCtxKey("data")).(map[string]interface{})
for _, p := range route.matcher.Params {
data[p] = params[p]
}
return true
}
2023-12-13 09:22:05 +08:00
l.Debug("Not match matcher reg")
2023-12-12 00:42:21 +08:00
return false
}
if route.Path == "" {
return true
}
return strings.HasPrefix(r.URL.Path, route.Path)
}
2023-12-12 21:44:35 +08:00
func (route *Route) Add(m Middleware) {
route.middles.Add(m)
}
2023-12-12 22:58:30 +08:00
// NewRoute 返回一个新的Route实例
// 参数:
// - method: 请求方法
// - path: 请求路径
// - handleFn: http.Handler处理函数
// 返回值:
// - *Route: 一个指向Route的指针
2023-12-12 21:44:35 +08:00
func NewRoute(method string, path string, handleFn http.Handler) *Route {
ret := &Route{
Method: method,
Path: path,
middles: NewMiddlewareLink(),
}
p := ParseUrl(path)
2023-12-12 22:58:30 +08:00
// 使用handleFn构建handler
2023-12-12 21:44:35 +08:00
ret.Handler = handleFn
ret.matcher = &p
return ret
}
2023-12-12 00:42:21 +08:00
type Routes []*Route
func (rs Routes) Less(i, j int) bool {
iPaths := strings.Split(rs[i].Path, "/")
jPaths := strings.Split(rs[j].Path, "/")
if len(iPaths) > len(jPaths) {
return true
}
if len(iPaths) < len(jPaths) {
return false
}
for k := 0; k < len(iPaths); k++ {
if iPaths[k] != jPaths[k] {
return iPaths[k] < jPaths[k]
}
}
return false
}
func (rs Routes) Len() int {
return len(rs)
}
func (rs Routes) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
2023-12-07 22:42:27 +08:00
// 可以嵌套的Rest http server mux
type RestMux struct {
2023-12-11 18:15:29 +08:00
Path string
2023-12-12 00:42:21 +08:00
routes Routes
2023-12-12 21:44:35 +08:00
middlewares *MiddlewareLink
2023-12-07 22:42:27 +08:00
}
2023-12-11 18:15:29 +08:00
func (mux *RestMux) Use(m Middleware) {
2023-12-12 21:44:35 +08:00
mux.middlewares.Add(m)
2023-12-11 18:15:29 +08:00
}
2023-12-07 22:42:27 +08:00
func (mux *RestMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2023-12-12 21:44:35 +08:00
2023-12-11 23:46:40 +08:00
c := r.Context()
data := map[string]interface{}{}
cn := context.WithValue(c, RequestCtxKey("data"), data)
newRequest := r.WithContext(cn)
2023-12-12 21:44:35 +08:00
if mux.middlewares.Len() > 0 && !mux.middlewares.ServeHTTP(w, newRequest) {
return
2023-12-11 18:15:29 +08:00
}
2023-12-12 00:42:21 +08:00
// 根据r 从routes中找到匹配的路由
for _, route := range mux.routes {
if route.Match(newRequest) {
route.ServeHTTP(w, newRequest)
return
}
}
2023-12-07 22:42:27 +08:00
2023-12-12 00:42:21 +08:00
http.NotFound(w, r)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) HandleFunc(method string, path string, f func(http.ResponseWriter, *http.Request)) *Route {
r := NewRoute(method, path, http.HandlerFunc(f))
2023-12-12 00:42:21 +08:00
mux.routes = append(mux.routes, r)
sort.Sort(mux.routes)
2023-12-12 21:44:35 +08:00
return r
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Get(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("GET", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Post(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("POST", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Put(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("PUT", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Delete(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("DELETE", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Patch(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("PATCH", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Head(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("HEAD", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) Option(path string, f func(http.ResponseWriter, *http.Request)) *Route {
return mux.HandleFunc("OPTION", path, f)
2023-12-07 22:42:27 +08:00
}
2023-12-12 21:44:35 +08:00
func (mux *RestMux) HandleMux(nmux *RestMux) *Route {
2023-12-07 22:42:27 +08:00
p := nmux.Path
if !strings.HasSuffix(p, "/") {
p = p + "/"
}
2023-12-12 21:44:35 +08:00
r := NewRoute("", p, nmux)
mux.routes = append(mux.routes, r)
2023-12-12 00:42:21 +08:00
sort.Sort(mux.routes)
2023-12-12 21:44:35 +08:00
return r
2023-12-07 22:42:27 +08:00
}
func NewRestMux(path string) *RestMux {
2023-12-12 00:42:21 +08:00
ret := &RestMux{
Path: path,
routes: Routes{},
2023-12-12 21:44:35 +08:00
middlewares: NewMiddlewareLink(),
2023-12-07 22:42:27 +08:00
}
2023-12-12 00:42:21 +08:00
return ret
2023-12-07 22:42:27 +08:00
}
type ServerMux struct {
http.Handler
directiveHandlers *MiddlewareLink
handlers map[string]http.Handler
paths []string
}
func (s *ServerMux) Handle(pattern string, handler http.Handler) {
if s.handlers == nil {
s.handlers = make(map[string]http.Handler)
}
s.handlers[pattern] = handler
s.paths = append(s.paths, pattern)
// 自定义比较函数排序s.paths
sort.Slice(s.paths, func(i, j int) bool {
return len(s.paths[i]) > len(s.paths[j]) || s.paths[i] > s.paths[j]
})
}
func (s *ServerMux) AddDirective(directiveStr string) {
2023-12-15 21:45:04 +08:00
l := logger.GetLogger("ServerMux")
strs := strings.Split(directiveStr, " ")
directiveName := strs[0]
params := strs[1:]
directive, ok := DirectiveMap[directiveName]
if ok {
2023-12-15 21:45:04 +08:00
l.Debug(fmt.Sprintf("add directive: %s", directiveName))
s.directiveHandlers.Add(directive(params...))
2023-12-15 21:45:04 +08:00
} else {
l.Error(fmt.Sprintf("directive not found: %s", directiveName))
}
}
func (s *ServerMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2023-12-15 21:45:04 +08:00
l := logger.GetLogger("ServerMux")
for _, p := range s.paths {
if strings.HasPrefix(r.URL.Path, p) {
2023-12-15 21:45:04 +08:00
l.Info(fmt.Sprintf("match path: %s", p))
s.directiveHandlers.ServeHTTP(w, r)
s.handlers[p].ServeHTTP(w, r)
return
}
}
2023-12-15 21:45:04 +08:00
l.Info(fmt.Sprintf("not match path: %s", r.URL.Path))
http.NotFound(w, r)
}
func NewServeMux(c *model.HttpServerConfig) *ServerMux {
l := logger.GetLogger("ServerMux")
l.Debug("NewServeMux")
s := &ServerMux{
directiveHandlers: NewMiddlewareLink(),
handlers: make(map[string]http.Handler),
paths: []string{},
}
for _, directive := range c.Directives {
s.AddDirective(string(directive))
}
for _, httpPath := range c.Paths {
if httpPath.Root != "" {
fileHandler := handler.NewFileHandler(handler.FileHandler{
Root: httpPath.Root,
Default: httpPath.Default,
})
s.Handle(httpPath.Path, fileHandler)
} else if len(httpPath.Upstreams) != 0 {
proxyHandler := handler.NewProxyHandler(&httpPath)
s.Handle(httpPath.Path, proxyHandler)
} else {
l.Error("Not supportted server path type :", httpPath.Path)
}
}
return s
}