From 0faca2fcb235a3540e8f3dcedccf3f31d7c69fbc Mon Sep 17 00:00:00 2001 From: tcq Date: Thu, 4 May 2023 14:20:55 +0800 Subject: [PATCH] demo2 --- Makefile | 4 ++++ gin/gin.go | 1 + gin/router.go | 4 ++-- main.go | 41 +++++++++++++++++++---------------------- 4 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0eab537 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +release1: + go mod tidy + GOOS=linux go build -o demo2 . + scp demo2 ubuntu@192.168.0.214:~/ \ No newline at end of file diff --git a/gin/gin.go b/gin/gin.go index 3c56504..910db7c 100644 --- a/gin/gin.go +++ b/gin/gin.go @@ -24,6 +24,7 @@ func Service(conf *Config) *Config { } go func() { router := gin.New() + router.Use(gin.Logger(), gin.Recovery()) routerSetup(router, &conf.RootPath) if conf.Ssl { diff --git a/gin/router.go b/gin/router.go index e1b8eb8..a64474e 100644 --- a/gin/router.go +++ b/gin/router.go @@ -20,13 +20,13 @@ func routerSetup(router *gin.Engine, rootpath *string) { r.POST(`/forgot`) } - ug := router.Group(`/user`) + ug := r.Group(`/user`) { ug.GET(`/choose/:orgid`) ug.GET(`/detail`) ug.POST(`/update`) } - user := router.Group("user") + user := r.Group("user") { user.GET("login", handler.UserLogin) diff --git a/main.go b/main.go index 980870a..3080196 100644 --- a/main.go +++ b/main.go @@ -2,50 +2,47 @@ package main import ( "context" + "flag" "fmt" "os" "os/signal" "time" - "myschools.me/campus/campus-core/exceptionless" "myschools.me/campus/demo-2/consul" "myschools.me/campus/demo-2/gin" ) func main() { - // 日志初始化 - // exceptionless.Init(&exceptionless.Config{ - // ApiKey: os.Getenv("EXCEPTIONLESS_KEY"), - // ServerURL: os.Getenv("EXCEPTIONLESS_URL"), - // UpdateSettingsWhenIdleInterval: 0, - // }) - //gin初始化,基于docker部署,不再需要调整默认值(80) + appname := flag.String("appname", "demo2", "app name for current") + port := flag.Int("port", 8080, "port for gin") + addr := flag.String("addr", "localhost", "ip address for gin") + flag.Parse() - consul.Init(&consul.Config{ - Address: "192.168.0.214:8500", + consul.Init(nil) + + gin.Service(&gin.Config{ + RootPath: `/demo2`, + Addr: *addr, + Port: *port, }) - cnf := gin.Service(&gin.Config{ - RootPath: "/", - Addr: "0.0.0.0", - Port: 8082, - Ssl: false, - SslPem: "server.pem", - SslKey: "server.key", - }) - - if err := consul.RegisterAPI("demo2", "192.168.0.214", cnf.Port, ""); err != nil { + traefikTag := fmt.Sprintf("traefik.http.routers.%s.rule=PathPrefix(`/demo2`)", *appname) + if err := consul.RegisterAPI(*appname, *addr, *port, traefikTag); err != nil { fmt.Println("注册失败", err.Error()) } - // exceptionless.SubmitLog(fmt.Sprintf("campus-srv[%s:%d%s] service is running...", conf.Addr, conf.Port, conf.RootPath), "info") // 服务停止相应 c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) <-c _, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() - exceptionless.SubmitLog("campus-demo2 service shutting down", "info") + //取消注册 + err := consul.DeRegister(*appname, *addr, *port) + if err != nil { + fmt.Println("Error: ", err) + } + // exceptionless.SubmitLog("campus-demo2 service shutting down", "info") os.Exit(0) }