-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (105 loc) · 2.48 KB
/
main.go
File metadata and controls
124 lines (105 loc) · 2.48 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/foolin/goview/supports/ginview"
"github.com/gin-gonic/gin"
"github.com/sudhabindu1/wtf1/models"
"github.com/sudhabindu1/wtf1/modules"
"io/ioutil"
"math/rand"
"net/http"
"os"
"time"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
r := gin.Default()
r.HTMLRender = ginview.Default()
r.GET("/", indexHandler)
r.GET("/message/:uid", indexHandlerWithUid)
r.GET("/json/:uid", messageHandler)
r.POST("/insert", insertMessage)
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
_ = r.Run() // listen and serve on 0.0.0.0:8080
}
func indexHandler(c *gin.Context) {
m, err := modules.FindMessage()
if err != nil {
c.String(http.StatusNotFound, err.Error())
return
}
permalink := fmt.Sprintf("/message/%s", m.Uid)
c.HTML(http.StatusOK, "index.html", gin.H{
"message": m.Message,
"link": m.Link,
"permalink": permalink,
"color": m.Color,
})
return
}
func indexHandlerWithUid(c *gin.Context) {
uid := c.Param("uid")
m, err := modules.FindMessageWithId(uid)
if err != nil {
c.String(http.StatusNotFound, err.Error())
return
}
c.HTML(http.StatusOK, "index.html", gin.H{
"message": m.Message,
"link": m.Link,
"color": m.Color,
})
return
}
func insertMessage(c *gin.Context) {
token := c.Request.Header.Get("token")
if token != os.Getenv("AUTH_TOKEN") {
c.String(http.StatusUnauthorized, "User is not authorized")
return
}
if c.Request.Body != nil {
b, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
m := models.RadioMessage{}
err = json.Unmarshal(b, &m)
if err != nil {
c.String(http.StatusBadRequest, "uid should not be sent")
}
if m.Uid != "" {
c.String(http.StatusBadRequest, "uid should not be sent")
}
m.Uid = RandStringRunes(10)
err = modules.InsertMessage(&m)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
c.String(http.StatusOK, fmt.Sprintf("created. uid: %v", m.Uid))
}
}
func messageHandler(c *gin.Context) {
uid := c.Param("uid")
m, err := modules.FindMessageWithId(uid)
if err != nil {
c.String(http.StatusNotFound, err.Error())
return
}
c.JSON(http.StatusOK, *m)
return
}
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}