-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
71 lines (62 loc) · 1.34 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 (
"flag"
"fmt"
"log"
"github.com/TBXark/confstore"
"github.com/gin-gonic/gin"
)
var BuildVersion = "dev"
type Config struct {
Token string `json:"token"`
Oid string `json:"oid"`
Address string `json:"address"`
AppId string `json:"appid"`
}
func main() {
conf := flag.String("config", "config.json", "config file")
help := flag.Bool("help", false, "show help")
flag.Parse()
if *help {
fmt.Printf("version: %s\n", BuildVersion)
flag.Usage()
return
}
config, err := confstore.Load[Config](*conf)
if err != nil {
log.Fatal(err)
}
yzj := NewYunZhiJia(config.Token, config.Oid, config.AppId)
server := gin.Default()
status := map[string]ClockInTimeType{
"start": ClockInTimeTypeStart,
"end": ClockInTimeTypeEnd,
}
for k, v := range status {
t := v // fix: when use v in closure, it will always be the last value
server.GET("/"+k, func(c *gin.Context) {
ok, _ := yzj.IsClockInToday(t)
if ok {
c.String(200, "true")
} else {
c.String(200, "false")
}
})
}
if gin.Mode() == "debug" {
server.GET("/raw", func(c *gin.Context) {
flow, e := yzj.ClockInFlow()
if e != nil {
c.JSON(500, gin.H{
"error": e.Error(),
})
return
}
c.JSON(200, flow)
})
}
server.GET("/status", func(c *gin.Context) {
c.String(200, "ok")
})
_ = server.Run(config.Address)
}