2018-04-05 15:15:29 +00:00
|
|
|
package fasthttp
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-06 03:17:55 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
logging "git.loafle.net/commons/logging-go"
|
2018-04-04 04:01:26 +00:00
|
|
|
"git.loafle.net/commons/server-go"
|
2018-04-05 15:15:29 +00:00
|
|
|
"git.loafle.net/commons/server-go/web"
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServerHandler interface {
|
2018-04-05 15:15:29 +00:00
|
|
|
web.ServerHandler
|
2018-04-04 04:01:26 +00:00
|
|
|
|
2018-04-06 04:06:05 +00:00
|
|
|
OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError)
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
RegisterServlet(path string, servlet Servlet)
|
2018-04-04 16:51:34 +00:00
|
|
|
Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet
|
2018-04-04 04:01:26 +00:00
|
|
|
|
|
|
|
CheckOrigin(ctx *fasthttp.RequestCtx) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
2018-04-05 15:15:29 +00:00
|
|
|
web.ServerHandlers
|
2018-04-04 04:01:26 +00:00
|
|
|
|
2018-04-06 03:17:55 +00:00
|
|
|
NotFoundServelt Servlet
|
|
|
|
|
|
|
|
// path = context only.
|
|
|
|
// ex) /auth => /auth, /auth/member => /auth
|
2018-04-04 04:01:26 +00:00
|
|
|
servlets map[string]Servlet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
|
|
|
|
if err := sh.ServerHandlers.Init(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != sh.servlets {
|
|
|
|
for _, servlet := range sh.servlets {
|
|
|
|
if err := servlet.Init(serverCtx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
|
|
|
|
if nil != sh.servlets {
|
|
|
|
for _, servlet := range sh.servlets {
|
|
|
|
servlet.Destroy(serverCtx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sh.ServerHandlers.Destroy(serverCtx)
|
|
|
|
}
|
|
|
|
|
2018-04-06 04:06:05 +00:00
|
|
|
func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx, err *web.WebError) {
|
|
|
|
ctx.Error(err.Message, err.Code)
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-06 03:17:55 +00:00
|
|
|
func (sh *ServerHandlers) RegisterServlet(contextPath string, servlet Servlet) {
|
2018-04-04 04:01:26 +00:00
|
|
|
if nil == sh.servlets {
|
|
|
|
sh.servlets = make(map[string]Servlet)
|
|
|
|
}
|
2018-04-06 03:17:55 +00:00
|
|
|
sh.servlets[contextPath] = servlet
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 16:51:34 +00:00
|
|
|
func (sh *ServerHandlers) Servlet(serverCtx server.ServerCtx, ctx *fasthttp.RequestCtx) Servlet {
|
2018-04-06 03:17:55 +00:00
|
|
|
contextPath, err := getContextPath(string(ctx.Path()))
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Warnf("%v", err)
|
|
|
|
return sh.NotFoundServelt
|
|
|
|
}
|
2018-04-04 16:51:34 +00:00
|
|
|
|
2018-04-04 04:01:26 +00:00
|
|
|
var servlet Servlet
|
2018-04-06 03:45:40 +00:00
|
|
|
if servlet = sh.servlets[contextPath]; nil == servlet {
|
2018-04-06 03:17:55 +00:00
|
|
|
return sh.NotFoundServelt
|
2018-04-04 04:01:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return servlet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) CheckOrigin(ctx *fasthttp.RequestCtx) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Validate() error {
|
|
|
|
if err := sh.ServerHandlers.Validate(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-04-06 03:17:55 +00:00
|
|
|
|
|
|
|
func getContextPath(path string) (string, error) {
|
|
|
|
p := strings.TrimSpace(path)
|
|
|
|
|
|
|
|
if !strings.HasPrefix(p, "/") {
|
|
|
|
return "", fmt.Errorf("The path[%s] must started /", path)
|
|
|
|
}
|
2018-04-06 03:39:41 +00:00
|
|
|
p = p[1:]
|
2018-04-06 03:17:55 +00:00
|
|
|
|
|
|
|
if strings.HasSuffix(p, "/") {
|
|
|
|
cpl := len(p) - 1
|
|
|
|
p = p[:cpl]
|
|
|
|
}
|
|
|
|
|
|
|
|
components := strings.Split(p, "/")
|
|
|
|
if 0 == len(components) {
|
|
|
|
return "", fmt.Errorf("The path[%s] is not invalid", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("/%s", components[0]), nil
|
|
|
|
}
|