From cec47a960834634fd97faf902a1b7b33121cd861 Mon Sep 17 00:00:00 2001 From: liuchangshun <961347548@qq.com> Date: Wed, 22 Nov 2023 09:18:58 +0800 Subject: [PATCH] 123 --- README.md | 32 ++++++++++++++++++++++++++++++ lxrun/run.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 README.md create mode 100644 lxrun/run.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..aa00445 --- /dev/null +++ b/README.md @@ -0,0 +1,32 @@ +Getting Started +=============== + +## Installing + +To start using LxUtils, install Go and run `go get`: + +```sh +$ go get -u git.listensoft.net/tool/lxutils +``` + +## Get a value +Get searches json for the specified path. A path is in dot syntax, such as "name.last" or "age". When the value is found it's returned immediately. + +```go +package main + +import ( + "git.listensoft.net/tool/lxutils" + "git.listensoft.net/tool/lxutils/lxlog" + "git.listensoft.net/tool/lxutils/lxzap" +) + +func main() { + lxutils.Aa() + lxlog.InitLog() + err := lxzap.InitLogger(lxzap.LogConfig{}) + if err != nil { + panic(err.Error()) + } +} +``` \ No newline at end of file diff --git a/lxrun/run.go b/lxrun/run.go new file mode 100644 index 0000000..81c29d2 --- /dev/null +++ b/lxrun/run.go @@ -0,0 +1,55 @@ +package lxrun + +import ( + "context" + "fmt" + "git.listensoft.net/tool/lxutils/lxlog" + "git.listensoft.net/tool/lxutils/lxzap" + "github.com/gin-gonic/gin" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" +) + +func Run(env string, port string, fun func(), r func(router *gin.Engine)) { + _ = lxlog.InitLog() + app := gin.New() + // 注册zap相关中间件 + if env == "dev" { + app.Use(gin.Logger(), gin.Recovery()) + } else { + app.Use(lxzap.GinLogger(), lxzap.GinRecovery(true)) + } + fun() + r(app) + srv := http.Server{ + Addr: ":" + port, + Handler: app, + } + // make sure idle connections returned + processed := make(chan struct{}) + go func() { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + <-c + fmt.Println("ctrl + c") + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) + defer cancel() + if err := srv.Shutdown(ctx); nil != err { + log.Fatalf("server shutdown failed, err: %v\n", err) + } + log.Println("server gracefully shutdown") + + processed <- struct{}{} + }() + // serve + err := srv.ListenAndServe() + if http.ErrServerClosed != err { + log.Fatalf("server not gracefully shutdown, err :%v\n", err) + } + // waiting for goroutine above processed + <-processed +}