-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
375 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package webapi | ||
|
||
import ( | ||
"github.com/farseer-go/fs/asyncLocal" | ||
"github.com/farseer-go/fs/container" | ||
"github.com/farseer-go/fs/flog" | ||
"github.com/farseer-go/fs/trace" | ||
"github.com/farseer-go/webapi/context" | ||
"net/http" | ||
) | ||
|
||
func HttpHandler(route *context.HttpRoute) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// 解析报文、组装httpContext | ||
httpContext := context.NewHttpContext(route, w, r) | ||
// 创建链路追踪上下文 | ||
trackContext := container.Resolve[trace.IManager]().EntryWebApi(httpContext.URI.Host, httpContext.URI.Url, httpContext.Method, httpContext.ContentType, httpContext.Header.ToMap(), httpContext.URI.GetRealIp()) | ||
// 结束链路追踪 | ||
defer trackContext.End() | ||
// 记录出入参 | ||
defer func() { | ||
trackContext.SetBody(httpContext.Request.BodyString, httpContext.Response.GetHttpCode(), string(httpContext.Response.BodyBytes)) | ||
}() | ||
httpContext.Data.Set("Trace", trackContext) | ||
|
||
// 设置到routine,可用于任意子函数获取 | ||
context.RoutineHttpContext.Set(httpContext) | ||
// 执行第一个中间件 | ||
route.HttpMiddleware.Invoke(httpContext) | ||
// 记录异常 | ||
if httpContext.Exception != nil { | ||
trackContext.Error(httpContext.Exception) | ||
_ = flog.Errorf("[%s]%s 发生错误:%s", httpContext.Method, httpContext.URI.Url, httpContext.Exception.Error()) | ||
} | ||
asyncLocal.Release() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/farseer-go/webapi/context" | ||
"net/http" | ||
) | ||
|
||
type Websocket struct { | ||
context.IMiddleware | ||
} | ||
|
||
func (receiver *Websocket) Invoke(httpContext *context.HttpContext) { | ||
httpContext.Response.SetHttpCode(http.StatusOK) | ||
httpContext.Response.SetStatusCode(http.StatusOK) | ||
|
||
// 下一步:exceptionMiddleware | ||
receiver.IMiddleware.Invoke(httpContext) | ||
|
||
_ = httpContext.WebsocketConn.WriteClose(httpContext.Response.GetHttpCode()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/farseer-go/fs" | ||
"github.com/farseer-go/webapi" | ||
) | ||
|
||
func init() { | ||
fs.Initialize[webapi.Module]("demo") | ||
//fs.Initialize[webapi.Module]("demo") | ||
} |
Oops, something went wrong.