This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
health.go
96 lines (86 loc) · 2.06 KB
/
health.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 (
"time"
"github.com/JasonKhew96/biliroaming-go-server/database"
"github.com/JasonKhew96/biliroaming-go-server/entity"
"github.com/valyala/fasthttp"
)
// newHealth assign new health json
func newHealth() *entity.Health {
return &entity.Health{
Code: 0,
Message: "0",
Data: entity.HealthData{
LastCheck: time.Now(),
},
}
}
func (b *BiliroamingGo) updateHealth(health *entity.Health, code int, message string) {
if health == nil {
return
}
health.Data.LastCheck = time.Now()
health.Message = message
if code == 0 {
health.Data.Counter = 0
health.Code = 0
return
}
health.Data.Counter++
if health.Data.Counter > 3 {
health.Code = code
}
}
func (b *BiliroamingGo) getPlayUrlHealth(area string) *entity.Health {
areaCode := getAreaCode(area)
switch areaCode {
case database.AreaCN:
return b.HealthPlayUrlCN
case database.AreaHK:
return b.HealthPlayUrlHK
case database.AreaTW:
return b.HealthPlayUrlTW
case database.AreaTH:
return b.HealthPlayUrlTH
default:
return nil
}
}
func (b *BiliroamingGo) getSearchHealth(area string) *entity.Health {
areaCode := getAreaCode(area)
switch areaCode {
case database.AreaCN:
return b.HealthSearchCN
case database.AreaHK:
return b.HealthSearchHK
case database.AreaTW:
return b.HealthSearchTW
case database.AreaTH:
return b.HealthSearchTH
default:
return nil
}
}
func (b *BiliroamingGo) handleApiHealth(ctx *fasthttp.RequestCtx) {
queryArgs := ctx.URI().QueryArgs()
argArea := string(queryArgs.PeekBytes([]byte("area")))
argType := string(queryArgs.PeekBytes([]byte("type")))
if argArea == "" {
writeErrorJSON(ctx, ERROR_CODE_MISSING_AREA, MSG_ERROR_MISSING_AREA)
return
}
if argType == "" {
writeErrorJSON(ctx, ERROR_CODE_MISSING_TYPE, MSG_ERROR_MISSING_TYPE)
return
}
switch argType {
case "playurl":
writeHealthJSON(ctx, b.getPlayUrlHealth(argArea))
case "search":
writeHealthJSON(ctx, b.getSearchHealth(argArea))
case "season":
writeHealthJSON(ctx, b.HealthSeasonTH)
default:
writeErrorJSON(ctx, ERROR_CODE_PARAMETERS, MSG_ERROR_PARAMETERS)
}
}