-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (117 loc) · 3.18 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"encoding/json"
"flag"
"log"
"os"
"scf/config"
"scf/handler/alwd"
"scf/handler/biliweekly"
xhttp "scf/handler/http"
"scf/handler/setu"
"time"
"github.com/robfig/cron/v3"
"github.com/tencentyun/scf-go-lib/cloudfunction"
)
type EventTimer struct {
Type string
TriggerName string
Time time.Time
Message string
}
type EventHttp struct {
HTTPMethod string `json:"httpMethod"`
Path string `json:"path"`
Headers Headers `json:"headers"`
PathParameters interface{} `json:"pathParameters"`
QueryString interface{} `json:"queryString"`
QueryStringParameters interface{} `json:"queryStringParameters"`
HeaderParameters interface{} `json:"headerParameters"`
RequestContext RequestContext `json:"requestContext"`
IsBase64Encoded bool `json:"isBase64Encoded"`
}
type Headers struct {
Accept string `json:"accept"`
Host string `json:"host"`
RequestSource string `json:"requestsource"`
UserAgent string `json:"user-agent"`
XAPIRequestID string `json:"x-api-requestid"`
XAPIScheme string `json:"x-api-scheme"`
XB3TraceId string `json:"x-b3-traceid"`
XQualifier string `json:"x-qualifier"`
}
type RequestContext struct {
HTTPMethod string `json:"httpMethod"`
Identity interface{} `json:"identity"`
Path string `json:"path"`
ServiceId string `json:"serviceId"`
SourceIp string `json:"sourceIp"`
Stage string `json:"stage"`
}
func _handler(ctx context.Context, event interface{}) (resp interface{}, err error) {
os.Setenv("APP_ENV", "prod")
config.Init()
var (
eHttp EventHttp
eTimer EventTimer
success = func() bool {
return err == nil
}
)
j, _ := json.Marshal(event)
err = json.Unmarshal(j, &eHttp)
if success() && eHttp.HTTPMethod != "" {
log.Println("Method", eHttp.HTTPMethod, "Path", eHttp.Path)
switch eHttp.Path {
case "/gogo/bilibili_weekly_remind":
return biliweekly.Handler(ctx, event)
case "/gogo/http":
return xhttp.Handler(ctx, event)
case "/gogo/alwd":
return alwd.Handler(ctx, event)
case "/gogo/setu":
return setu.Handler(ctx, event)
default:
log.Printf("unknown method: %+v", eHttp)
return
}
}
err = json.Unmarshal(j, &eTimer)
if success() && eTimer.TriggerName != "" {
log.Println("TriggerName", eTimer.TriggerName, "Type", eTimer.Type)
switch eTimer.TriggerName {
case "bilibili_weekly_remind":
return biliweekly.Handler(ctx, event)
case "setu":
return setu.Handler(ctx, event)
default:
log.Printf("unknown timer: %+v", eTimer)
return
}
}
log.Printf("unknown event: %+v", event)
return
}
func main() {
var (
app = flag.String("app", "", "loli")
)
flag.Parse()
switch *app {
case "setu":
log.Println("app=setu")
c := cron.New()
_, err := c.AddFunc("0 10-19/4 * * *", func() { _handler(context.Background(), EventTimer{TriggerName: "setu"}) })
if err != nil {
log.Fatal(err)
}
c.Start()
default:
log.Println("app=default")
// Make the handler available for Remote Procedure Call by Cloud Function
cloudfunction.Start(_handler)
return
}
select {}
}