-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (67 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"github.com/gin-gonic/gin"
wework "github.com/go-laoji/wecom-go-sdk"
"github.com/go-laoji/wecom-go-sdk/pkg/svr/middleware"
"github.com/joho/godotenv"
"log"
"net/http"
"os"
"os/signal"
"time"
"wecom.dev/audit/api"
"wecom.dev/audit/archives"
"wecom.dev/audit/logger"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ww := wework.NewWeWork(wework.WeWorkConfig{})
ww.SetAppSecretFunc(func(corpId uint) (corpid string, secret string, customizedApp bool) {
return os.Getenv("CorpId"), os.Getenv("Secret"), true
})
client, err := archives.InitSdk(os.Getenv("CorpId"), os.Getenv("Secret"))
err = client.Sync()
logger.Surgar.Info("初始化启动时同步完成,开启web服务监听")
router := gin.Default()
callbackCtl := api.CallBackCtl{Audit: client}
callback := router.Group("/callback")
{
callback.GET("", callbackCtl.Get)
callback.POST("", callbackCtl.Post)
}
apiAudit := router.Group("/api/audit")
apiAudit.Use(middleware.InjectSdk(ww))
auditCtl := api.AuditCtl{}
{
apiAudit.POST("groupchat", auditCtl.GroupChat)
apiAudit.POST("checkagree", auditCtl.CheckSingleAgree)
apiAudit.POST("permituser", auditCtl.GetPermitUserList)
}
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "ok"})
})
srv01 := &http.Server{
Addr: "127.0.0.1:8080",
Handler: router,
ReadTimeout: 15 * time.Second,
WriteTimeout: 15 * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := srv01.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Surgar.Fatal(err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := srv01.Shutdown(ctx); err != nil {
logger.Surgar.Fatal("server shutdown:", err)
}
}