-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
194 lines (158 loc) · 3.95 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"crypto/rand"
"flag"
"html"
"log"
"math/big"
"net"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"gopkg.in/yaml.v2"
"gorm.io/gorm"
"gorm.io/driver/sqlite"
)
type AlertType struct {
gorm.Model
Label string `yaml:"label"`
Message string `yaml:"message"`
}
type Config struct {
Username string `yaml:"username"`
WebhookURL string `yaml:"webhookURL"`
DBFilePath string `yaml:"dbFilePath"`
HellShakeYanoURL string `yaml:"hellShakeYanoURL"`
PuiPuiURL string `yaml:"puiPuiURL"`
}
var (
config Config
db *gorm.DB
)
func init() {
configPath := ""
flag.StringVar(&configPath, "config", "config.yaml", "--config config.yaml")
flag.Parse()
if configPath == "" {
log.Fatal("unexpected --config")
}
f, err := os.Open(configPath)
if err != nil {
log.Fatal(err.Error())
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(&config); err != nil {
f.Close()
log.Fatal(err.Error())
}
db, err = gorm.Open(sqlite.Open(config.DBFilePath), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
db.AutoMigrate(&AlertType{})
}
func main() {
r := gin.Default()
r.Static("/assets", "./assets")
r.LoadHTMLGlob("templates/*.tmpl")
r.GET("", index)
r.POST("/alert", alert)
r.GET("/create", create)
r.POST("/store", store)
r.POST("/delete", delete)
if err := r.Run(":8080"); err != nil {
log.Fatal(err.Error())
}
}
func index(ctx *gin.Context) {
alertTypes := []AlertType{}
db.Find(&alertTypes)
ctx.HTML(http.StatusOK, "index.tmpl", alertTypes)
}
func create(ctx *gin.Context) {
ctx.HTML(http.StatusOK, "create.tmpl", gin.H{})
}
func store(ctx *gin.Context) {
label := ctx.PostForm("label")
msg := ctx.PostForm("msg")
if len(msg) > 32 {
msg = msg[:32]
}
alertType := AlertType{
Label: label,
Message: html.EscapeString(msg),
}
if err := db.Create(&alertType).Error; err != nil {
}
ctx.Redirect(http.StatusFound, "")
}
type Attachment struct {
Fallback string `json:"fallback"`
AuthorName string `json:"author_name"`
AuthorLink string `json:"author_link"`
AuthorIcon string `json:"author_icon"`
Title string `json:"title"`
Footer string `json:"footer"`
FooterIcon string `json:"footer_icon"`
Text string `json:"text"`
TS int `json:"ts"`
}
type Webhook struct {
Username string `json:"username"`
IconURL string `json:"icon_url"`
Attachments []Attachment `json:"attachments"`
}
func alert(ctx *gin.Context) {
alertTypeID := ctx.PostForm("alertTypeID")
var alertType AlertType
db.First(&alertType, alertTypeID)
if alertType.Message != "" {
log.Println("webhook! ", alertType.Message)
c := resty.New()
isIncludeVia := ctx.PostForm("isIncludeVia")
src := ""
if isIncludeVia == "on" {
addr, err := net.LookupAddr(ctx.ClientIP())
if err != nil {
src = " | " + ctx.ClientIP()
} else {
src = " | " + ctx.ClientIP() + " -> " + addr[0]
}
}
name := "名無しの新卒"
msg := "その話題... " + alertType.Message + " かも..."
icon := config.PuiPuiURL
if n, err := rand.Int(rand.Reader, big.NewInt(100)); err == nil {
if n.Int64() == 8 {
name = "ヘルシェイク矢野"
icon = config.HellShakeYanoURL
}
}
b := Webhook{
Username: name,
IconURL: icon,
Attachments: []Attachment{
{
Fallback: msg,
Title: msg,
Footer: "<https://github.com/ophum/geekAlert|ophum/geekAlert>" + src,
FooterIcon: "https://slack-imgs.com/?c=1&o1=wi32.he32.si&url=https%3A%2F%2Fgithub.githubassets.com%2Ffavicon.ico",
//TS: int(time.Now().Unix()),
},
},
}
c.R().SetHeaders(map[string]string{
"Content-Type": "application/json",
}).SetBody(b).Post(config.WebhookURL)
}
ctx.Redirect(http.StatusFound, "")
}
func delete(ctx *gin.Context) {
id := ctx.PostForm("id")
var alertType AlertType
db.First(&alertType, id)
db.Delete(&alertType)
ctx.Redirect(http.StatusFound, "")
}