代码拉取完成,页面将自动刷新
package rui
import (
"fmt"
"net/http"
"path"
"runtime"
"strings"
"time"
"gitee.com/ruige_fun/util/rlog"
"github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter"
)
type routerGroup struct {
prevPath string
handlers []func(ctx Context)
app *application
}
func (rg *routerGroup) newHandler(handlers []func(ctx Context), fx func(ctx Context)) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
now := time.Now()
ctx := newContext(w, r, p, rg.app.vd)
defer func() {
if e := recover(); e != nil {
rlog.PanicP(rlog.ModelNoPath, "ServeHTTP崩溃了:", r.Method, r.URL, " 崩溃信息:", e)
}
var Code = ctx.statusCode
var BodyLength = ctx.bodySize
since := time.Since(now)
s := ""
if since > time.Second {
s = fmt.Sprint(Code, "|", since.Seconds(), "s|", getIP(r), "|", BodyLength, "byte|", r.Method, ":", r.URL.Path)
} else if since > time.Millisecond {
s = fmt.Sprint(Code, "|", since.Milliseconds(), "ms|", getIP(r), "|", BodyLength, "byte|", r.Method, ":", r.URL.Path)
} else {
s = fmt.Sprint(Code, "|", since.Microseconds(), "μs|", getIP(r), "|", BodyLength, "byte|", r.Method, ":", r.URL.Path)
}
if Code == 200 && rg.app.logLevel <= int(rlog.LogLevelInfo) {
rlog.InfoP(rlog.ModelNoPath, s)
} else if Code >= 300 && Code < 400 && rg.app.logLevel <= int(rlog.LogLevelWarn) {
rlog.WarnP(rlog.ModelNoPath, s)
} else if Code >= 400 && Code < 500 && rg.app.logLevel <= int(rlog.LogLevelError) {
rlog.ErrorP(rlog.ModelNoPath, s)
} else if Code >= 500 && Code < 600 && rg.app.logLevel <= int(rlog.LogLevelPanic) {
rlog.PanicP(rlog.ModelNoPath, s)
} else if rg.app.logLevel <= int(rlog.LogLevelDebug) {
rlog.DebugP(rlog.ModelNoPath, s)
}
}()
for _, d := range handlers {
d(ctx)
if ctx.stop {
w.WriteHeader(ctx.statusCode)
w.Header().Del("Content-Type")
w.Header().Set("Content-Type", ctx.contentType)
size, err := w.Write(ctx.bodyBytes)
if err == nil {
ctx.bodySize = uint64(size)
}
return
}
}
fx(ctx)
w.WriteHeader(ctx.statusCode)
w.Header().Del("Content-Type")
w.Header().Set("Content-Type", ctx.contentType)
size, err := w.Write(ctx.bodyBytes)
if err == nil {
ctx.bodySize = uint64(size)
}
}
}
func (rg *routerGroup) handler(fx func(Context)) httprouter.Handle {
return rg.newHandler(rg.handlers, fx)
}
func (rg *routerGroup) Group(urlPath string) RouterGroup {
return &routerGroup{
prevPath: path.Join(rg.prevPath, urlPath),
handlers: rg.handlers,
app: rg.app,
}
}
func (rg *routerGroup) Use(handle ...func(Context)) {
rg.handlers = append(rg.handlers, handle...)
}
func (rg *routerGroup) GET(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "GET",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.GET(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) HEAD(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "HEAD",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.HEAD(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) OPTIONS(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "OPTIONS",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.OPTIONS(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) options(urlPath string, fx func(Context)) {
rg.app.router.OPTIONS(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) POST(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "POST",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.POST(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) PUT(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "PUT",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.PUT(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) PATCH(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "PATCH",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.PATCH(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) DELETE(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "DELETE",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.DELETE(path.Join(rg.prevPath, urlPath), rg.handler(fx))
}
func (rg *routerGroup) Any(urlPath string, fx func(Context)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "Any",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
handler := rg.handler(fx)
rg.app.router.GET(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.HEAD(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.OPTIONS(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.POST(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.PUT(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.PATCH(path.Join(rg.prevPath, urlPath), handler)
rg.app.router.DELETE(path.Join(rg.prevPath, urlPath), handler)
}
// WebSocket WebSocket链接,CheckOrigin是否允许跨域访问
func (rg *routerGroup) WebSocket(urlPath string, CheckOrigin bool, fx func(conn *websocket.Conn)) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "WebSocket",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
var upgrade = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return CheckOrigin
},
}
rg.app.router.HandlerFunc("GET", path.Join(rg.prevPath, urlPath), func(w http.ResponseWriter, r *http.Request) {
var (
conn *websocket.Conn
err error
)
if conn, err = upgrade.Upgrade(w, r, nil); err != nil {
return
}
go fx(conn)
})
}
// StaticFile 一个文件的静态文件服务
// 比如index.html,StaticFile("/", "./index.html")
// 比如favicon.ico,StaticFile("/favicon.ico", "./favicon.ico")
func (rg *routerGroup) StaticFile(urlPath string, filePath string) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "StaticFile",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.Handle("GET", path.Join(rg.prevPath, urlPath), func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
http.ServeFile(w, r, filePath)
})
}
// ServeFiles 多个文件的静态文件服务
// 比如:urlPath="/static/*filepath" root=http.Dir("/var/www")
func (rg *routerGroup) ServeFiles(urlPath string, root http.FileSystem) {
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: "ServeFiles",
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.ServeFiles(path.Join(rg.prevPath, urlPath), root)
}
// Handler 原生http响应
// 注意,原生http响应,是不过中间件的。
func (rg *routerGroup) Handler(method string, urlPath string, handler http.Handler) {
method = strings.ToUpper(method)
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: method,
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.Handler(method, path.Join(rg.prevPath, urlPath), handler)
}
// HandlerFunc 原生http响应函数
// 注意,原生http响应,是不过中间件的。
func (rg *routerGroup) HandlerFunc(method string, urlPath string, handler http.HandlerFunc) {
method = strings.ToUpper(method)
_, file, line, _ := runtime.Caller(1)
rg.app.all = append(rg.app.all, RouterItem{
URL: path.Join(rg.prevPath, urlPath),
Method: method,
CodeRow: fmt.Sprint(file[rlog.GetCropPathLength():], ":", line),
})
rg.app.router.HandlerFunc(method, path.Join(rg.prevPath, urlPath), handler)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。