优化http路由规则

This commit is contained in:
duanhf2012
2019-12-13 17:52:59 +08:00
parent ac435cc156
commit 7c37cba487
5 changed files with 282 additions and 161 deletions

View File

@@ -1,8 +1,11 @@
package main package main
import ( import (
"fmt"
"github.com/duanhf2012/origin/originnode" "github.com/duanhf2012/origin/originnode"
"github.com/duanhf2012/origin/service" "github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/sysservice/originhttp"
) )
type InputData struct { type InputData struct {
@@ -21,6 +24,10 @@ func init() {
//OnInit ... //OnInit ...
func (ws *SubNet1_Service1) OnInit() error { func (ws *SubNet1_Service1) OnInit() error {
originhttp.Post(" / aaa/bb/ :user/:pass/", ws.HTTP_UserIntegralInfo)
originhttp.Post(" /aaa/bbb", ws.Test)
originhttp.Get(" /aaa/bbb", ws.HTTP_UserIntegralInfo)
originhttp.SetStaticResource(originhttp.METHOD_GET, "/file/", "d:\\")
return nil return nil
} }
@@ -33,8 +40,16 @@ func (ws *SubNet1_Service1) OnRun() bool {
//RPC_MethodName(arg *DataType1, ret *DataType2) error //RPC_MethodName(arg *DataType1, ret *DataType2) error
//如果不符合规范,在加载服务时,该函数将不会被映射,其他服务将不允能调用。 //如果不符合规范,在加载服务时,该函数将不会被映射,其他服务将不允能调用。
func (slf *SubNet1_Service1) RPC_Add(arg *InputData, ret *int) error { func (slf *SubNet1_Service1) RPC_Add(arg *InputData, ret *int) error {
*ret = arg.A1 + arg.A2 *ret = arg.A1 + arg.A2
return nil
}
func (slf *SubNet1_Service1) Test(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
return nil
}
func (slf *SubNet1_Service1) HTTP_UserIntegralInfo(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
ret, ok := request.Query("a")
fmt.Print(ret, ok)
return nil return nil
} }

View File

@@ -4,7 +4,7 @@ import (
"github.com/duanhf2012/origin/cluster" "github.com/duanhf2012/origin/cluster"
"github.com/duanhf2012/origin/originnode" "github.com/duanhf2012/origin/originnode"
"github.com/duanhf2012/origin/sysservice" "github.com/duanhf2012/origin/sysservice/originhttp"
) )
func main() { func main() {
@@ -15,7 +15,7 @@ func main() {
} }
nodeCfg, _ := cluster.ReadNodeConfig("./config/nodeconfig.json", cluster.GetNodeId()) nodeCfg, _ := cluster.ReadNodeConfig("./config/nodeconfig.json", cluster.GetNodeId())
httpserver := sysservice.NewHttpServerService(nodeCfg.HttpPort) // http服务 httpserver := originhttp.NewHttpServerService(nodeCfg.HttpPort) // http服务
for _, ca := range nodeCfg.CAFile { for _, ca := range nodeCfg.CAFile {
httpserver.SetHttps(ca.CertFile, ca.KeyFile) httpserver.SetHttps(ca.CertFile, ca.KeyFile)
} }

View File

@@ -1,22 +1,20 @@
package sysservice package originhttp
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"reflect" "reflect"
"runtime"
"strings" "strings"
"time" "time"
"github.com/duanhf2012/origin/sysmodule" "github.com/duanhf2012/origin/sysmodule"
"github.com/duanhf2012/origin/util/uuid"
"github.com/duanhf2012/origin/rpc" "github.com/duanhf2012/origin/rpc"
"github.com/gorilla/mux"
"github.com/gotoxu/cors"
"github.com/duanhf2012/origin/cluster" "github.com/duanhf2012/origin/cluster"
"github.com/duanhf2012/origin/network" "github.com/duanhf2012/origin/network"
@@ -26,64 +24,242 @@ import (
type HttpRequest struct { type HttpRequest struct {
Header http.Header Header http.Header
Body string Body string
ParamStr string
mapParam map[string]string
} }
type HttpRespone struct { type HttpRespone struct {
Respone []byte Respone []byte
} }
type ServeHTTPRouterMux struct {
}
type ControllerMapsType map[string]reflect.Value type ControllerMapsType map[string]reflect.Value
type HttpServerService struct { type HttpServerService struct {
service.BaseService service.BaseService
httpserver network.HttpServer httpserver network.HttpServer
port uint16 port uint16
PrintRequestTime bool
controllerMaps ControllerMapsType controllerMaps ControllerMapsType
certfile string certfile string
keyfile string keyfile string
ishttps bool ishttps bool
httpfiltrateList []HttpFiltrate httpfiltrateList []HttpFiltrate
resourcedir string //静态资源文件夹绝对路径 }
type RouterMatchData struct {
callpath string
matchURL string
routerType int8 //0表示函数调用 1表示静态资源
}
type RouterStaticResoutceData struct {
localpath string
method string
}
type HTTP_METHOD int
const (
METHOD_GET HTTP_METHOD = iota
METHOD_POST HTTP_METHOD = 1
//METHOD_PUT HTTP_METHOD = 2
)
var bPrintRequestTime bool
var postAliasUrl map[string]map[string]RouterMatchData //url地址对应本service地址
var staticRouterResource map[string]RouterStaticResoutceData
func init() {
postAliasUrl = make(map[string]map[string]RouterMatchData)
postAliasUrl["GET"] = make(map[string]RouterMatchData)
postAliasUrl["POST"] = make(map[string]RouterMatchData)
staticRouterResource = make(map[string]RouterStaticResoutceData)
}
type HttpHandle func(request *HttpRequest, resp *HttpRespone) error
func AnalysisRouterUrl(url string) (string, error) {
//替换所有空格
url = strings.ReplaceAll(url, " ", "")
if len(url) <= 1 || url[0] != '/' {
return "", fmt.Errorf("url %s format is error!", url)
}
//去掉尾部的/
return strings.Trim(url, "/"), nil
}
func (slf *HttpRequest) Query(key string) (string, bool) {
if slf.mapParam == nil {
slf.mapParam = make(map[string]string)
//分析字符串
slf.ParamStr = strings.Trim(slf.ParamStr, "/")
paramStrList := strings.Split(slf.ParamStr, "&")
for _, val := range paramStrList {
param := strings.Split(val, "=")
if len(param) == 2 {
slf.mapParam[param[0]] = param[1]
}
}
}
ret, ok := slf.mapParam[key]
return ret, ok
}
func Request(method HTTP_METHOD, url string, handle HttpHandle) error {
fnpath := runtime.FuncForPC(reflect.ValueOf(handle).Pointer()).Name()
fmt.Print(fnpath)
sidx := strings.LastIndex(fnpath, "*")
if sidx == -1 {
return errors.New(fmt.Sprintf("http post func path is error, %s", fnpath))
}
eidx := strings.LastIndex(fnpath, "-fm")
if sidx == -1 {
return errors.New(fmt.Sprintf("http post func path is error, %s", fnpath))
}
callpath := fnpath[sidx+1 : eidx]
ridx := strings.LastIndex(callpath, ")")
if ridx == -1 {
return errors.New(fmt.Sprintf("http post func path is error, %s", fnpath))
}
hidx := strings.LastIndex(callpath, "HTTP_")
if hidx == -1 {
return errors.New(fmt.Sprintf("http post func not contain HTTP_, %s", fnpath))
}
callpath = strings.ReplaceAll(callpath, ")", "")
var r RouterMatchData
var matchURL string
var err error
r.routerType = 0
r.callpath = "_" + callpath
matchURL, err = AnalysisRouterUrl(url)
if err != nil {
return err
}
var strMethod string
if method == METHOD_GET {
strMethod = "GET"
} else if method == METHOD_POST {
strMethod = "POST"
} else {
return fmt.Errorf("not support method.")
}
postAliasUrl[strMethod][matchURL] = r
return nil
}
func Post(url string, handle HttpHandle) error {
return Request(METHOD_POST, url, handle)
}
func Get(url string, handle HttpHandle) error {
return Request(METHOD_GET, url, handle)
} }
func (slf *HttpServerService) OnInit() error { func (slf *HttpServerService) OnInit() error {
// slf.httpserver.Init(slf.port, &ServeHTTPRouterMux{}, 10*time.Second, 10*time.Second)
slf.httpserver.Init(slf.port, slf.initRouterHandler(), 10*time.Second, 10*time.Second)
if slf.ishttps == true { if slf.ishttps == true {
slf.httpserver.SetHttps(slf.certfile, slf.keyfile) slf.httpserver.SetHttps(slf.certfile, slf.keyfile)
} }
return nil return nil
} }
func (slf *ServeHTTPRouterMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
methodRouter, bok := postAliasUrl[r.Method]
if bok == false {
writeRespone(w, http.StatusNotFound, fmt.Sprint("Can not support method."))
return
}
url := strings.Trim(r.URL.Path, "/")
var strCallPath string
matchData, ok := methodRouter[url]
if ok == true {
strCallPath = matchData.callpath
} else {
//如果是资源处理
for k, v := range staticRouterResource {
idx := strings.Index(url, k)
if idx != -1 {
staticServer(k, v, w, r)
return
}
}
// 拼接得到rpc服务的名称
vstr := strings.Split(url, "/")
if len(vstr) != 3 {
writeRespone(w, http.StatusNotFound, "Cannot find path.")
return
}
strCallPath = "_" + vstr[1] + ".HTTP_" + vstr[2]
}
defer r.Body.Close()
msg, err := ioutil.ReadAll(r.Body)
if err != nil {
writeRespone(w, http.StatusBadRequest, "")
return
}
request := HttpRequest{r.Header, string(msg), r.URL.RawQuery, nil}
var resp HttpRespone
timeFuncStart := time.Now()
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
timeFuncPass := time.Since(timeFuncStart)
if bPrintRequestTime {
service.GetLogger().Printf(service.LEVER_INFO, "HttpServer Time : %s url : %s", timeFuncPass, strCallPath)
}
if err != nil {
writeRespone(w, http.StatusBadRequest, fmt.Sprint(err))
} else {
writeRespone(w, http.StatusOK, string(resp.Respone))
}
}
// CkResourceDir 检查静态资源文件夹路径 // CkResourceDir 检查静态资源文件夹路径
func (slf *HttpServerService) CkResourceDir(dirname string) error { func SetStaticResource(method HTTP_METHOD, urlpath string, dirname string) error {
_, err := os.Stat(dirname) _, err := os.Stat(dirname)
if err != nil { if err != nil {
return err return err
} }
slf.resourcedir = dirname matchURL, berr := AnalysisRouterUrl(urlpath)
if berr != nil {
return berr
}
var routerData RouterStaticResoutceData
if method == METHOD_GET {
routerData.method = "GET"
} else if method == METHOD_POST {
routerData.method = "POST"
}
routerData.localpath = dirname
staticRouterResource[matchURL] = routerData
return nil return nil
} }
func (slf *HttpServerService) initRouterHandler() http.Handler { func writeRespone(w http.ResponseWriter, status int, msg string) {
r := mux.NewRouter() w.Header().Set("Content-Type", "text/plain; charset=utf-8")
r.HandleFunc("/{server:[a-zA-Z0-9]+}/{method:[a-zA-Z0-9]+}", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status)
slf.httpHandler(w, r) w.Write([]byte(msg))
})
//获取静态文件资源
if slf.resourcedir != "" {
relativeDirIndex := strings.LastIndex(slf.resourcedir, "/")
relativeDirName := slf.resourcedir[relativeDirIndex:]
r.HandleFunc(relativeDirName+"/{filename:.*}", func(w http.ResponseWriter, r *http.Request) {
slf.staticServer(w, r)
})
}
cors := cors.AllowAll()
//return cors.Handler(gziphandler.GzipHandler(r))
return cors.Handler(r)
} }
type HttpFiltrate func(path string, w http.ResponseWriter, r *http.Request) error type HttpFiltrate func(path string, w http.ResponseWriter, r *http.Request) error
@@ -119,30 +295,23 @@ func (slf *HttpServerService) OnRemoveService(iservice service.IService) {
return return
} }
func (slf *HttpServerService) IsPrintRequestTime() bool {
if slf.PrintRequestTime == true {
return true
}
return false
}
func (slf *HttpServerService) SetPrintRequestTime(isPrint bool) { func (slf *HttpServerService) SetPrintRequestTime(isPrint bool) {
slf.PrintRequestTime = isPrint bPrintRequestTime = isPrint
} }
func (slf *HttpServerService) staticServer(w http.ResponseWriter, r *http.Request) { func staticServer(routerUrl string, routerData RouterStaticResoutceData, w http.ResponseWriter, r *http.Request) {
upath := r.URL.Path
idx := strings.Index(upath, routerUrl)
subPath := strings.Trim(upath[idx+len(routerUrl):], "/")
destLocalPath := routerData.localpath + subPath
writeResp := func(status int, msg string) { writeResp := func(status int, msg string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8") w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(status) w.WriteHeader(status)
w.Write([]byte(msg)) w.Write([]byte(msg))
} }
//设置静态文件夹路径
upath := r.URL.Path
relativeFileIndex := strings.LastIndex(upath, "/")
relativeFileName := upath[relativeFileIndex:]
destLocalPath := slf.resourcedir + relativeFileName
switch r.Method { switch r.Method {
//获取资源 //获取资源
case "GET": case "GET":
@@ -156,121 +325,57 @@ func (slf *HttpServerService) staticServer(w http.ResponseWriter, r *http.Reques
} }
//上传资源 //上传资源
case "POST": case "POST":
/*
// 在这儿处理例外路由接口 // 在这儿处理例外路由接口
var errRet error var errRet error
for _, filter := range slf.httpfiltrateList { for _, filter := range slf.httpfiltrateList {
ret := filter(r.URL.Path, w, r) ret := filter(r.URL.Path, w, r)
if ret == nil { if ret == nil {
errRet = nil errRet = nil
break break
} else { } else {
errRet = ret errRet = ret
}
} }
}
if errRet != nil { if errRet != nil {
w.Write([]byte(errRet.Error())) w.Write([]byte(errRet.Error()))
return return
} }
r.ParseMultipartForm(32 << 20) // max memory is set to 32MB r.ParseMultipartForm(32 << 20) // max memory is set to 32MB
resourceFile, resourceFileHeader, err := r.FormFile("file") resourceFile, resourceFileHeader, err := r.FormFile("file")
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
writeResp(http.StatusNotFound, err.Error()) writeResp(http.StatusNotFound, err.Error())
return return
} }
defer resourceFile.Close() defer resourceFile.Close()
//重新拼接文件名 //重新拼接文件名
imgFormat := strings.Split(resourceFileHeader.Filename, ".") imgFormat := strings.Split(resourceFileHeader.Filename, ".")
if len(imgFormat) != 2 { if len(imgFormat) != 2 {
writeResp(http.StatusNotFound, "not a file") writeResp(http.StatusNotFound, "not a file")
return return
} }
filePrefixName := uuid.Rand().HexEx() filePrefixName := uuid.Rand().HexEx()
fileName := filePrefixName + "." + imgFormat[1] fileName := filePrefixName + "." + imgFormat[1]
//创建文件 //创建文件
localpath := fmt.Sprintf("%s%s", destLocalPath, fileName) localpath := fmt.Sprintf("%s%s", destLocalPath, fileName)
localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666) localfd, err := os.OpenFile(localpath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
writeResp(http.StatusNotFound, "upload fail") writeResp(http.StatusNotFound, "upload fail")
return return
} }
defer localfd.Close() defer localfd.Close()
io.Copy(localfd, resourceFile) io.Copy(localfd, resourceFile)
writeResp(http.StatusOK, upath+fileName) writeResp(http.StatusOK, upath+fileName)*/
} }
} }
func (slf *HttpServerService) httpHandler(w http.ResponseWriter, r *http.Request) {
writeError := func(status int, msg string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(status)
w.Write([]byte(msg))
}
if r.Method != "POST" {
//writeError(http.StatusMethodNotAllowed, "rpc: POST method required, received "+r.Method)
//return
}
if r.Method == "OPTIONS" {
return
}
defer r.Body.Close()
msg, err := ioutil.ReadAll(r.Body)
if err != nil {
writeError(http.StatusBadRequest, "rpc: ioutil.ReadAll "+err.Error())
return
}
// 在这儿处理例外路由接口
var errRet error
for _, filter := range slf.httpfiltrateList {
ret := filter(r.URL.Path, w, r)
if ret == nil {
errRet = nil
break
} else {
errRet = ret
}
}
if errRet != nil {
w.Write([]byte(errRet.Error()))
return
}
// 拼接得到rpc服务的名称
vstr := strings.Split(r.URL.Path, "/")
if len(vstr) != 3 {
writeError(http.StatusBadRequest, "rpc: ioutil.ReadAll "+err.Error())
return
}
strCallPath := "_" + vstr[1] + ".HTTP_" + vstr[2]
request := HttpRequest{r.Header, string(msg)}
var resp HttpRespone
TimeFuncStart := time.Now()
err = cluster.InstanceClusterMgr().Call(strCallPath, &request, &resp)
TimeFuncPass := time.Since(TimeFuncStart)
if slf.IsPrintRequestTime() {
service.GetLogger().Printf(service.LEVER_INFO, "HttpServer Time : %s url : %s", TimeFuncPass, strCallPath)
}
w.Header().Set("Content-Type", "application/json;charset=utf-8")
if err != nil {
resp.Respone = []byte(fmt.Sprint(err))
}
w.Write([]byte(resp.Respone))
}
func (slf *HttpServerService) GetMethod(strCallPath string) (*reflect.Value, error) { func (slf *HttpServerService) GetMethod(strCallPath string) (*reflect.Value, error) {
value, ok := slf.controllerMaps[strCallPath] value, ok := slf.controllerMaps[strCallPath]
if ok == false { if ok == false {

View File

@@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/duanhf2012/origin/service" "github.com/duanhf2012/origin/service"
"github.com/duanhf2012/origin/sysservice/originhttp"
) )
type PProfService struct { type PProfService struct {
@@ -17,7 +18,7 @@ type PProfService struct {
type ProfileData struct { type ProfileData struct {
Name string Name string
Count int Count int
} }
type Profilestruct struct { type Profilestruct struct {
Fisttime int Fisttime int
@@ -27,7 +28,7 @@ type Profilestruct struct {
func (slf *PProfService) OnInit() error { func (slf *PProfService) OnInit() error {
slf.fisttime = (int)(time.Now().UnixNano()) slf.fisttime = (int)(time.Now().UnixNano())
return nil return nil
} }
func (slf *PProfService) GetPprof() ([]byte, error) { func (slf *PProfService) GetPprof() ([]byte, error) {
var pfiles Profilestruct var pfiles Profilestruct
@@ -43,7 +44,7 @@ func (slf *PProfService) GetPprof() ([]byte, error) {
return json.Marshal(pfiles) return json.Marshal(pfiles)
} }
func (slf *PProfService) HTTP_DebugPProf(request *HttpRequest, resp *HttpRespone) error { func (slf *PProfService) HTTP_DebugPProf(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
var err error var err error
resp.Respone, err = slf.GetPprof() resp.Respone, err = slf.GetPprof()
if err != nil { if err != nil {
@@ -65,7 +66,7 @@ func (slf *PProfService) RPC_DebugPProf(arg *string, ret *Profilestruct) error {
return nil return nil
} }
func (slf *PProfService) HTTP_Test(request *HttpRequest, resp *HttpRespone) error { func (slf *PProfService) HTTP_Test(request *originhttp.HttpRequest, resp *originhttp.HttpRespone) error {
resp.Respone = []byte(request.Body) resp.Respone = []byte(request.Body)
return nil return nil

View File

@@ -13,7 +13,7 @@ type WSServerService struct {
port uint16 port uint16
messageReciver network.IMessageReceiver messageReciver network.IMessageReceiver
bEnableCompression bool bEnableCompression bool
} }
func (ws *WSServerService) OnInit() error { func (ws *WSServerService) OnInit() error {