-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (77 loc) · 1.98 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
package main
import (
"log"
"os"
"strconv"
"strings"
"github.com/gofiber/fiber/v3"
"github.com/joho/godotenv"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/telegram"
"github.com/spf13/viper"
)
var notifys = make(map[string]*notify.Notify)
func init() {
godotenv.Load()
viper.AutomaticEnv()
//env: NT_SYNOLOGY=token|chatid
// fmt.Println(viper.GetString("NT_SYNOLOGY"))
for _, v := range os.Environ() {
variable := strings.Split(v, "=")
envKey := variable[0]
if strings.HasPrefix(strings.ToLower(envKey), "nt_") {
key := strings.Replace(strings.ToLower(envKey), "nt_", "", -1)
cfg := strings.Split(viper.GetString(envKey), "|")
if len(cfg) < 2 {
log.Fatalf("failed to parse %s=%s", envKey, v)
continue
}
token := cfg[0]
receiver, err := strconv.ParseInt(cfg[1], 10, 64)
if err != nil {
log.Fatalf("strconv error: %v", err)
continue
}
telegramService, _ := telegram.New(token)
telegramService.AddReceivers(receiver)
n := notify.New()
n.UseServices(telegramService)
notifys[key] = n
log.Printf("notify service %s is loaded", key)
}
}
if len(notifys) == 0 {
log.Panic("notify service is not configured")
}
}
type Msg struct {
Message string `json:"message" xml:"message" form:"message" query:"message"`
}
func main() {
app := fiber.New()
api := app.Group("/:domain", withNotify())
{
api.All("", func(c fiber.Ctx) error {
noti := c.Locals("notify").(*notify.Notify)
var msg Msg
if err := c.Bind().Body(&msg); err != nil {
if err := c.Bind().Query(&msg); err != nil {
return err
}
}
if err := noti.Send(c.Context(), c.Locals("domain").(string), msg.Message); err != nil {
return err
}
return c.SendString("ok")
})
}
log.Fatal(app.Listen(":" + os.Getenv("PORT")))
}
func withNotify() fiber.Handler {
return func(c fiber.Ctx) error {
domain := strings.ToLower(c.Params("domain"))
c.Locals("notify", notifys[domain])
c.Locals("domain", domain)
return c.Next()
}
}