This commit is contained in:
xixi.xv 2024-07-11 09:21:33 +08:00
parent f87d425a37
commit cc51d4b7bc
9 changed files with 151 additions and 8 deletions

0
.env Normal file
View File

23
Dockerfile Normal file
View File

@ -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"]

View File

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

5
go.mod
View File

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

View File

@ -0,0 +1,7 @@
package handler
import "github.com/gin-gonic/gin"
func SchoolList(c *gin.Context) {
c.Status(200)
}

View File

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

2
hello.rest Normal file
View File

@ -0,0 +1,2 @@
### sdfsafsad
GET http://127.0.0.1:8080/hello/school/list HTTP/1.1

72
logrus.go Normal file
View File

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

33
main.go
View File

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