1 Star 0 Fork 0

瑞哥 / util

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Application.go 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
瑞哥 提交于 2022-09-09 12:28 . 重新设计rlog,go版本更新到1.19
package rui
import (
"fmt"
"net/http"
"path"
"github.com/go-playground/validator/v10"
"gitee.com/ruige_fun/util/rlog"
"github.com/julienschmidt/httprouter"
)
type application struct {
router *httprouter.Router //路由器
all []RouterItem //所有请求信息
logLevel int //日志打印等级
routerGroup //分组路由器
vd *validator.Validate //验证器
}
// New 新建一个服务
// logLevel 日志打印等级:调试 0;信息 1;警告 2;错误 3;恐慌 4;不打印 5
func New(logLevel int) Application {
app := application{
router: httprouter.New(),
all: make([]RouterItem, 0, 10),
logLevel: logLevel,
routerGroup: routerGroup{
prevPath: "/",
handlers: make([]func(Context), 0, 10),
app: nil,
},
}
app.routerGroup.app = &app
return &app
}
func (app *application) Group(urlPath string) RouterGroup {
return &routerGroup{
prevPath: path.Join(app.routerGroup.prevPath, urlPath),
handlers: app.routerGroup.handlers,
app: app,
}
}
func (app *application) Run(host string, port uint) (err error) {
rlog.InfoP(rlog.ModelNoPath, "启动服务", getLocationIP("http", host, port))
return http.ListenAndServe(fmt.Sprint(host, ":", port), app.router)
}
func (app *application) RunTLS(host string, port uint, certFile string, keyFile string) error {
rlog.InfoP(rlog.ModelNoPath, "启动服务", getLocationIP("https", host, port))
return http.ListenAndServeTLS(fmt.Sprint(host, ":", port), certFile, keyFile, app.router)
}
// OpenValidator 开启验证器
// 具体的验证规则,请查看:https://pkg.go.dev/github.com/go-playground/validator/v10#hdr-Multiple_Validators
func (app *application) OpenValidator() {
app.vd = validator.New()
}
type RouterItem struct {
URL string `json:"url"` //请求路径
Method string `json:"method"` //请求方法
CodeRow string `json:"code_row"` //注册代码行
}
func (app *application) AllHandleFunc() []RouterItem {
return app.all
}
func (app *application) PrintAllRequest() {
fmt.Println("所有请求信息打印开始===================================================================================")
list := app.AllHandleFunc()
for _, d := range list {
fmt.Printf("%-12s %-40s %s\n", d.Method, d.URL, d.CodeRow)
}
fmt.Println("所有请求信息打印结束===================================================================================")
}
Go
1
https://gitee.com/ruige_fun/util.git
git@gitee.com:ruige_fun/util.git
ruige_fun
util
util
v0.0.22

搜索帮助