1 Star 0 Fork 0

Leng Shanshu / etris

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
demo_server.go 2.88 KB
一键复制 编辑 原始数据 按行查看 历史
Leng Shanshu 提交于 2021-12-08 19:55 . add MDNSxxxPlus
package main
import (
stdCtx "context"
"fmt"
"gitee.com/lengss/boost/msar"
"github.com/kataras/iris/v12"
"log"
"path/filepath"
"strings"
"time"
)
var discovery msar.DiscoveryPlugin
func demo_server_run() error {
fName := "demo"
app := iris.New()
//1. 错误自定义接管处理, 404页面, crash( panic ), 500 捕获
app.OnErrorCode(iris.StatusNotFound, notFound)
app.Use(MyRecover())
ac := makeAccessLog(fName)
// ac := makeRotateAccessLog()
defer ac.Close()
// Register the middleware (UseRouter to catch http errors too).
app.UseRouter(ac.Handler)
// 2. log日志打印设置 :
lgfile := makeLogger(app, fName)
// makeRotateLogger(app)
app.Logger().SetLevel("debug")
tmpl := iris.HTML(filepath.Join(g_Configs.WebPath, "./demo"), ".html").Layout("layouts/layout.html")
tmpl.Reload(true)
app.RegisterView(tmpl) // iris.HTML("./demo", ".html"))
app.Get("/", demo_index)
// Register resources based on the version.
v1 := app.Party("v1")
v1.Get("/hello", demo_v1_hello)
v1.Get("/world", demo_v1_world)
v2 := app.Party("v2")
v2.Get("/hello", demo_v2_hello)
v2.Get("/world", demo_v2_world)
// OR register to a specific Route before its main handler.
// my := app.Party("/other").Layout("layouts/layout2.html")
// 5. 注册静态文件
app.HandleDir("/static", filepath.Join(g_Configs.WebPath, "static"))
app.Favicon(filepath.Join(g_Configs.WebPath, "static/favicon.ico"))
//其它初始化操作,比如数据库,seesion初始化
/////////////////////////////////////////////////////////////////////////////////////
//
//
target := "service/api"
var err error
if len(g_Configs.Etcd) > 0 {
etcd_clusters := strings.Split(g_Configs.Etcd, ",")
// etcd_clusters := []string{"http://127.0.0.1:7039", "http://127.0.0.1:7049"}
discovery, err = msar.NewEtcdDiscovery(etcd_clusters, target, lgfile, irisConf.LogLevel)
} else {
discovery, err = msar.NewMDNSDiscovery(target, lgfile, irisConf.LogLevel)
}
if err != nil {
log.Panic(err)
}
//
//
/////////////////////////////////////////////////////////////////////////////////////
// 6. 关机时间设置,graceful shutdown
iris.RegisterOnInterrupt(func() {
discovery.Close()
app.Logger().Info("||--- demo : close now ---||")
ctx, cancel := stdCtx.WithTimeout(stdCtx.Background(), 30*time.Second)
defer cancel()
app.Shutdown(ctx)
})
// 7. 启动服务
addr := fmt.Sprintf(":%d", g_Configs.ListenAddr+1)
return app.Run(
// 启动服务在8081端口
iris.Addr(addr), // ":8081"),
// 启动时禁止检测框架版本差异
//iris.WithoutVersionChecker,
//忽略服务器错误
iris.WithoutServerError(iris.ErrServerClosed),
//让程序自身尽可能的优化
iris.WithOptimizations,
iris.WithCharset("UTF-8"), // 国际化
iris.WithoutInterruptHandler, // graceful shutdown
// iris.WithoutStartupLog,
//iris.WithPathIntelligence,
//iris.WithoutPathCorrectionRedirection,
)
}
Go
1
https://gitee.com/lengss/etris.git
git@gitee.com:lengss/etris.git
lengss
etris
etris
master

搜索帮助