forked from shelly-tools/ShellyActionRouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShellyActionRouter.go
403 lines (354 loc) · 8.94 KB
/
ShellyActionRouter.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
* @package ShellyActionRouter
* @copyright Thorsten Eurich
* @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html)
* @version 0.1
*
* @todo lots of documentation
*
* Simple Action Proxy written in Golang which makes it possible to execute multiple actions
*/
package main
import (
"database/sql"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/ini.v1"
)
type ActionRoute struct {
Id int
Name string
Action []Action
}
type ActionUrl struct {
Id int
Url string
Name string
}
type Action struct {
Id int
ActionType string
Content string
IdUrl int
}
var netClient = &http.Client{
Timeout: time.Second * 5,
}
type Unreachable struct {
Status string `json:"status"`
}
var tmpl = template.Must(template.ParseGlob("views/*"))
func dbConn() (db *sql.DB) {
db, err := sql.Open("sqlite3", "./ActionRouter.db")
if err != nil {
panic(err.Error())
}
return db
}
func Index(w http.ResponseWriter, r *http.Request) {
IpAddress := ""
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
port := cfg.Section("server").Key("port").String()
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
IpAddress = "http://" + ipv4.String() + ":" + port
}
}
db := dbConn()
selDBIndex, err := db.Query("SELECT idurl, urlname FROM urls")
if err != nil {
panic(err.Error())
}
emp := ActionUrl{}
res := []ActionUrl{}
for selDBIndex.Next() {
var idurl int
var urlname string
err = selDBIndex.Scan(&idurl, &urlname)
if err != nil {
panic(err.Error())
}
emp.Id = idurl
emp.Url = IpAddress + "/api/action/" + urlname
emp.Name = urlname
res = append(res, emp)
}
tmpl.ExecuteTemplate(w, "Index", res)
defer db.Close()
}
func AddUrl(w http.ResponseWriter, r *http.Request) {
tmpl.ExecuteTemplate(w, "AddUrl", nil)
}
func DeleteUrl(w http.ResponseWriter, r *http.Request) {
db := dbConn()
emp := r.URL.Query().Get("idurl")
delForm, err := db.Prepare("DELETE FROM urls WHERE idurl=?")
if err != nil {
panic(err.Error())
}
delForm.Exec(emp)
log.Println("deleted URL id" + emp)
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func InsertUrl(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
urlname := r.FormValue("urlname")
insForm, err := db.Prepare("INSERT INTO urls (urlname) VALUES(?)")
if err != nil {
panic(err.Error())
}
insForm.Exec(urlname)
log.Println("INSERT: URL: " + urlname + "")
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func EditUrl(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("idurl")
selDB, err := db.Query("SELECT * FROM urls WHERE idurl=?", nId)
if err != nil {
panic(err.Error())
}
emp := ActionUrl{}
for selDB.Next() {
var idurl int
var urlname string
err = selDB.Scan(&idurl, &urlname)
if err != nil {
panic(err.Error())
}
emp.Id = idurl
emp.Name = urlname
}
tmpl.ExecuteTemplate(w, "EditUrl", emp)
defer db.Close()
}
func ShowUrl(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("idurl")
selDB, err := db.Query("SELECT * FROM urls WHERE idurl=?", nId)
if err != nil {
panic(err.Error())
}
empindex := ActionRoute{}
for selDB.Next() {
var idurl int
var urlname string
err = selDB.Scan(&idurl, &urlname)
if err != nil {
panic(err.Error())
}
empindex.Id = idurl
empindex.Name = urlname
selDBAction, err := db.Query("SELECT * FROM actions WHERE idurl=?", nId)
if err != nil {
panic(err.Error())
}
emp := Action{}
res := []Action{}
for selDBAction.Next() {
var id int
var actiontype string
var content string
var idurl int
err = selDBAction.Scan(&id, &actiontype, &content, &idurl)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.ActionType = actiontype
emp.IdUrl = idurl
emp.Content = content
res = append(res, emp)
}
empindex.Action = res
}
tmpl.ExecuteTemplate(w, "ShowUrl", empindex)
defer db.Close()
}
func UpdateUrl(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
urlname := r.FormValue("urlname")
idurl := r.FormValue("uid")
insForm, err := db.Prepare("UPDATE urls SET urlname=? WHERE idurl=?")
if err != nil {
panic(err.Error())
}
insForm.Exec(urlname, idurl)
log.Println("UPDATE URL: " + urlname + " ")
}
defer db.Close()
http.Redirect(w, r, "/", 301)
}
func AddAction(w http.ResponseWriter, r *http.Request) {
idurl := r.URL.Query().Get("idurl")
tmpl.ExecuteTemplate(w, "AddAction", idurl)
}
func InsertAction(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
actiontype := r.FormValue("actiontype")
content := r.FormValue("content")
idurl := r.FormValue("idurl")
insForm, err := db.Prepare("INSERT INTO actions (actiontype, content, idurl) VALUES(?, ?, ?)")
if err != nil {
panic(err.Error())
}
insForm.Exec(actiontype, content, idurl)
log.Println("INSERT Action: " + content + "")
}
defer db.Close()
http.Redirect(w, r, "/url/show?idurl="+r.FormValue("idurl"), 301)
}
func DeleteAction(w http.ResponseWriter, r *http.Request) {
db := dbConn()
id := r.URL.Query().Get("id")
idurl := r.URL.Query().Get("idurl")
delForm, err := db.Prepare("DELETE FROM actions WHERE id=?")
if err != nil {
panic(err.Error())
}
delForm.Exec(id)
log.Println("deleted Action id" + id)
defer db.Close()
http.Redirect(w, r, "/url/show?idurl="+idurl, 301)
}
func EditAction(w http.ResponseWriter, r *http.Request) {
db := dbConn()
nId := r.URL.Query().Get("id")
selDB, err := db.Query("SELECT * FROM actions WHERE id=?", nId)
if err != nil {
panic(err.Error())
}
emp := Action{}
for selDB.Next() {
var id int
var actiontype string
var content string
var idurl int
err = selDB.Scan(&id, &actiontype, &content, &idurl)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.ActionType = actiontype
emp.Content = content
emp.IdUrl = idurl
}
tmpl.ExecuteTemplate(w, "EditAction", emp)
defer db.Close()
}
func UpdateAction(w http.ResponseWriter, r *http.Request) {
db := dbConn()
url := r.URL.Query().Get("idurl")
if r.Method == "POST" {
id := r.FormValue("id")
//idurl := r.FormValue("idurl")
content := r.FormValue("content")
actiontype := r.FormValue("actiontype")
insForm, err := db.Prepare("UPDATE actions SET actiontype=?, content=? WHERE id=?")
if err != nil {
panic(err.Error())
}
insForm.Exec(actiontype, content, id)
log.Println("UPDATE Action " + id + " ")
}
defer db.Close()
http.Redirect(w, r, "/url/show?idurl="+url, 301)
}
func main() {
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read file: %v", err)
os.Exit(1)
}
port := cfg.Section("server").Key("port").String()
r := mux.NewRouter()
staticDir := "/assets/"
// Create the route
r.PathPrefix(staticDir).Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir("."+staticDir))))
// URLs handler
r.HandleFunc("/", Index)
r.HandleFunc("/url/add", AddUrl)
r.HandleFunc("/url/delete", DeleteUrl)
r.HandleFunc("/url/edit", EditUrl)
r.HandleFunc("/url/insert", InsertUrl)
r.HandleFunc("/url/update", UpdateUrl)
r.HandleFunc("/url/show", ShowUrl)
// Actions handler
r.HandleFunc("/action/add", AddAction)
r.HandleFunc("/action/insert", InsertAction)
r.HandleFunc("/action/delete", DeleteAction)
r.HandleFunc("/action/edit", EditAction)
r.HandleFunc("/action/update", UpdateAction)
// API-Handler
r.HandleFunc("/api/action/{title}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
title := vars["title"]
db := dbConn()
selDB, err := db.Query("SELECT a.id, a.actiontype, a.content, a.idurl FROM actions AS a LEFT JOIN urls AS u ON (a.idurl = u.idurl) WHERE urlname = ?", title)
if err != nil {
panic(err.Error())
}
emp := Action{}
res := []Action{}
for selDB.Next() {
var id int
var actiontype string
var content string
var idurl int
err = selDB.Scan(&id, &actiontype, &content, &idurl)
if err != nil {
panic(err.Error())
}
emp.Id = id
emp.ActionType = actiontype
emp.Content = content
emp.IdUrl = idurl
res = append(res, emp)
}
defer db.Close()
for _, v := range res {
if v.ActionType == "sleep" {
sl, _ := strconv.Atoi(v.Content)
time.Sleep(time.Duration(sl) * time.Millisecond)
} else {
curl := fmt.Sprintf("%s", v.Content)
res, err := netClient.Get(curl)
if err != nil {
emp := Unreachable{Status: "offline"}
var jsonData []byte
jsonData, _ = json.Marshal(emp)
w.Header().Set("Content-Type", "application/json")
w.Write(jsonData)
log.Println(curl + " unreachable")
} else {
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Fprintf(w, string(body))
}
}
}
})
http.ListenAndServe(":"+port, r)
}