generated from wuhan005/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
96 lines (82 loc) · 2.26 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
package main
import (
gocontext "context"
"io/fs"
"net/http"
"os"
"github.com/flamego/flamego"
"github.com/robfig/cron/v3"
log "unknwon.dev/clog/v2"
"github.com/asoul-sig/asoul-video/frontend"
"github.com/asoul-sig/asoul-video/internal/context"
"github.com/asoul-sig/asoul-video/internal/db"
"github.com/asoul-sig/asoul-video/internal/route"
)
var (
BuildTime string
BuildCommit string
)
func main() {
defer log.Stop()
err := log.NewConsole()
if err != nil {
panic(err)
}
if err := db.Init(); err != nil {
log.Fatal("Failed to connect to database: %v", err)
}
// Register cron task.
ctx := gocontext.Background()
if err := db.Videos.Refresh(ctx); err != nil {
log.Error("Failed to refresh materialized views: %v", err)
}
c := cron.New()
if _, err := c.AddFunc("@every 1h", func() {
if err := db.Videos.Refresh(ctx); err != nil {
log.Error("Failed to refresh materialized views: %v", err)
}
log.Trace("Refresh materialized views.")
}); err != nil {
log.Fatal("Failed to add cron function: %v", err)
}
c.Start()
f := flamego.Classic()
f.Use(func(ctx flamego.Context) {
ctx.ResponseWriter().Header().Set("Access-Control-Allow-Methods", http.MethodGet)
ctx.ResponseWriter().Header().Set("Access-Control-Max-Age", "600")
ctx.ResponseWriter().Header().Set("Access-Control-Allow-Origin", "*")
})
f.Use(context.Contexter())
fe, err := fs.Sub(frontend.FS, "dist")
if err != nil {
log.Fatal("Failed to sub filesystem: %v", err)
}
f.Use(flamego.Static(flamego.StaticOptions{
FileSystem: http.FS(fe),
}))
f.Group("/api", func() {
member := route.NewMemberHandler()
f.Get("/members", member.List)
f.Get("/member/{secUID}", member.GetBySecUID)
video := route.NewVideoHandler()
f.Get("/videos", video.List)
f.Group("/video", func() {
f.Get("/{id}", video.GetByID)
f.Get("/random", video.Random)
})
})
// Crawler report service.
source := route.NewSourceHandler()
f.Group("/source", func() {
f.Post("/report", source.Report)
f.Get("/video_urls", source.VideoURLs)
f.Get("/video_ids", source.VideoIDs)
}, source.VerifyKey(os.Getenv("SOURCE_REPORT_KEY")))
f.Get("/ping", func(ctx context.Context) {
ctx.Success(map[string]interface{}{
"build_time": BuildTime,
"build_commit": BuildCommit,
})
})
f.Run()
}