From cc51d4b7bcd8cdd66218dc35a458bc0430e71cb5 Mon Sep 17 00:00:00 2001 From: "xixi.xv" <3269265964@qq.com> Date: Thu, 11 Jul 2024 09:21:33 +0800 Subject: [PATCH] init --- .env | 0 Dockerfile | 23 +++++++++++++ gin/router.go | 14 +++++--- go.mod | 5 +++ handler/school-handler.go | 7 ++++ handler/user-handler.go | 3 +- hello.rest | 2 ++ logrus.go | 72 +++++++++++++++++++++++++++++++++++++++ main.go | 33 ++++++++++++++++-- 9 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 .env create mode 100644 Dockerfile create mode 100644 handler/school-handler.go create mode 100644 hello.rest create mode 100644 logrus.go diff --git a/.env b/.env new file mode 100644 index 0000000..e69de29 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bb64b68 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM harbor.ks.easyj.top/zt/alpine:0.1 + +ENV APP_DIR=/app \ +LOGLEVEL=debug\ +MYSQL_DSN=root:root@tcp(mysql:3306)/sample?charset=utf8mb4&parseTime=True&loc=Local\ +MYSQL_MAXLIFETIME=2\ +MYSQL_MAXIDLECONNS=2\ +MYSQL_MAXOPENCONNS=200\ +MYSQL_INIT=true\ +REDIS_DSN=127.0.0.1:6379\ +REDIS_PWD=123456\ +REDIS_DB=0\ +CONSUL_ADDRESS=127.0.0.1:8500 + +COPY hello ${APP_DIR}/hello + +WORKDIR ${APP_DIR} + +RUN chmod +x hello + +EXPOSE 8080 + +CMD ["./hello"] \ No newline at end of file diff --git a/gin/router.go b/gin/router.go index 87174fc..ec70ad8 100644 --- a/gin/router.go +++ b/gin/router.go @@ -4,18 +4,22 @@ import ( "fmt" "github.com/gin-gonic/gin" + "myschools.me/xixi.xv/hello/handler" ) // 路由配置 func routerSetup(router *gin.Engine, rootpath *string) { router.Use(gin.Recovery()) - router.GET(`/health/check`) r := router.Group(fmt.Sprintf("/%s", *rootpath)) { - r.POST(`/register`) - r.GET(`/accountcheck/:accname`) - r.POST(`/login`) - r.POST(`/forgot`) + sch := r.Group(`/school`) + { + sch.GET(`/list`, handler.SchoolList) + } + usr := r.Group(`/user`) + { + usr.POST(`/login`, handler.UserLogin) + } } } diff --git a/go.mod b/go.mod index 68c2d0e..136c629 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.22.2 require ( github.com/gin-gonic/gin v1.10.0 github.com/gomodule/redigo v1.9.2 + github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible + github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 github.com/sirupsen/logrus v1.9.3 github.com/unrolled/secure v1.15.0 gorm.io/driver/mysql v1.5.7 @@ -26,13 +28,16 @@ require ( github.com/goccy/go-json v0.10.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/jonboulle/clockwork v0.4.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/lestrrat-go/strftime v1.0.6 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.8.0 // indirect diff --git a/handler/school-handler.go b/handler/school-handler.go new file mode 100644 index 0000000..d788623 --- /dev/null +++ b/handler/school-handler.go @@ -0,0 +1,7 @@ +package handler + +import "github.com/gin-gonic/gin" + +func SchoolList(c *gin.Context) { + c.Status(200) +} diff --git a/handler/user-handler.go b/handler/user-handler.go index 522c88a..0d5e0b9 100644 --- a/handler/user-handler.go +++ b/handler/user-handler.go @@ -8,7 +8,8 @@ import ( "myschools.me/xixi.xv/hello/service" ) -func UserHello(c *gin.Context) { +// user login +func UserLogin(c *gin.Context) { data := &model.User{} if err := c.ShouldBindJSON(data); err != nil { c.AbortWithStatus(http.StatusBadRequest) diff --git a/hello.rest b/hello.rest new file mode 100644 index 0000000..a8ba9b9 --- /dev/null +++ b/hello.rest @@ -0,0 +1,2 @@ +### sdfsafsad +GET http://127.0.0.1:8080/hello/school/list HTTP/1.1 diff --git a/logrus.go b/logrus.go new file mode 100644 index 0000000..6005df3 --- /dev/null +++ b/logrus.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "os" + "time" + + rotatelogs "github.com/lestrrat-go/file-rotatelogs" + "github.com/rifflock/lfshook" + "github.com/sirupsen/logrus" +) + +func init() { + //日志初始化 + level := os.Getenv("LOGLEVEL") + + switch level { + case "debug": + logrus.SetLevel(logrus.DebugLevel) + case "info": + logrus.SetLevel(logrus.InfoLevel) + case "warn": + logrus.SetLevel(logrus.WarnLevel) + case "error": + logrus.SetLevel(logrus.ErrorLevel) + case "fatal": + logrus.SetLevel(logrus.FatalLevel) + default: + logrus.SetLevel(logrus.PanicLevel) + } + + logrus.AddHook(newLfsHook(72)) +} + +func newLfsHook(maxRemainCnt uint) logrus.Hook { + //检查与创建日志文件夹 + _, err := os.Stat("logs") + if os.IsNotExist(err) { + os.Mkdir("logs", 0755) + } + + logName := fmt.Sprintf(`logs/%s`, "device-wg") + writer, err := rotatelogs.New( + logName+"%Y%m%d.log", + // WithLinkName为最新的日志建立软连接,以方便随着找到当前日志文件 + rotatelogs.WithLinkName(logName), + + // WithRotationTime设置日志分割的时间,这里设置为一小时分割一次 + rotatelogs.WithRotationTime(24*time.Hour), + + // WithMaxAge和WithRotationCount二者只能设置一个, + // WithMaxAge设置文件清理前的最长保存时间, + // WithRotationCount设置文件清理前最多保存的个数。 + //rotatelogs.WithMaxAge(time.Hour*24), + rotatelogs.WithRotationCount(maxRemainCnt), + ) + + if err != nil { + panic("config local file system for logger error: " + err.Error()) + } + + lfsHook := lfshook.NewHook(lfshook.WriterMap{ + logrus.DebugLevel: writer, + logrus.InfoLevel: writer, + logrus.WarnLevel: writer, + logrus.ErrorLevel: writer, + logrus.FatalLevel: writer, + logrus.PanicLevel: writer, + }, &logrus.TextFormatter{DisableColors: true}) + + return lfsHook +} diff --git a/main.go b/main.go index 4e2cb79..1a6fd18 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,36 @@ package main -import "fmt" +import ( + "context" + "os" + "os/signal" + "time" + + "github.com/sirupsen/logrus" + "myschools.me/xixi.xv/hello/gin" +) func main() { - fmt.Println("hello world.") + gin.Service(&gin.Config{ + RootPath: "hello", + Addr: "0.0.0.0", + Port: 8080, + Ssl: false, + }) + + logrus.WithFields(logrus.Fields{ + "func": "main", + }).Info("service runing ...") + + // 服务停止相应 + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + _, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + logrus.WithFields(logrus.Fields{ + "func": "main", + }).Info("service shutting down") + + os.Exit(0) }