123
This commit is contained in:
parent
2937bf6579
commit
cec47a9608
32
README.md
Normal file
32
README.md
Normal file
@ -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())
|
||||
}
|
||||
}
|
||||
```
|
||||
55
lxrun/run.go
Normal file
55
lxrun/run.go
Normal file
@ -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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user