This commit is contained in:
tcq 2023-05-04 14:20:55 +08:00
parent 3eade1c2c0
commit 0faca2fcb2
4 changed files with 26 additions and 24 deletions

4
Makefile Normal file
View File

@ -0,0 +1,4 @@
release1:
go mod tidy
GOOS=linux go build -o demo2 .
scp demo2 ubuntu@192.168.0.214:~/

View File

@ -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 {

View File

@ -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)

41
main.go
View File

@ -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)
}