-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (134 loc) · 3.5 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"io/ioutil"
)
type Message struct {
Sdp string `json:"sdp"`
}
func main() {
errlf := godotenv.Load(".env")
if errlf != nil {
log.Fatal("Error loading .env file")
}
gin.SetMode(os.Getenv("GIM_MODE"))
// Logging to a file.
if os.Getenv("GIM_MODE") == "release" {
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f)
}
serverKey := os.Getenv("SENTRY_KEY")
serverPem := os.Getenv("SENTRY_PEM")
// init API
fmt.Println("Starting the application ...<3")
// Start Gin
port := os.Getenv("SENTRY_PORT")
router := gin.New()
router.Use(gin.Logger())
router.Use(gin.Recovery())
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"PUT", "PATCH", "POST", "GET"},
AllowHeaders: []string{"*"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return origin == "https://github.com"
},
MaxAge: 31536000000, //12 * time.Hour,
}))
// if port is not define
if port == "" {
port = "8000"
}
router.GET("/getMessage", GetMessage)
router.GET("/getServerMessage", GetServerMessage)
router.POST("/setMessage", SetMessage)
router.POST("/SetServerMessage", SetServerMessage)
if len(serverPem) > 0 {
if err := http.ListenAndServeTLS(":"+port, serverPem, serverKey, router); err != nil {
log.Fatal(err)
}
} else {
if err := http.ListenAndServe(":"+port, router); err != nil {
log.Fatal(err)
}
}
}
func GetMessage(c *gin.Context) {
// read the whole file at once
body, err := ioutil.ReadFile("sdp.txt")
if err != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
} else {
if len(body) > 0 {
// truncate file
err2 := ioutil.WriteFile("sdp.txt", []byte(""), 0644)
if err2 != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
}
// truncate file
c.JSON(http.StatusOK, string(body))
} else {
c.JSON(http.StatusFailedDependency, "Error")
}
}
}
func SetMessage(c *gin.Context) {
var messageRequest Message
if err := c.BindJSON(&messageRequest); err != nil {
c.JSON(400, gin.H{"Error": "Wrong data"})
return
}
messageUpdate := Message{
Sdp: messageRequest.Sdp,
}
err := ioutil.WriteFile("sdp.txt", []byte(messageUpdate.Sdp), 0644)
if err != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
} else {
c.JSON(http.StatusOK, "ok")
}
}
func GetServerMessage(c *gin.Context) {
// read the whole file at once
body, err := ioutil.ReadFile("serverResponse.txt")
if err != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
} else {
if len(body) > 0 {
// truncate file
err2 := ioutil.WriteFile("serverResponse.txt", []byte(""), 0644)
if err2 != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
}
// truncate file
c.JSON(http.StatusOK, string(body))
} else {
c.JSON(http.StatusFailedDependency, "Error")
}
}
}
func SetServerMessage(c *gin.Context) {
var messageRequest Message
if err := c.BindJSON(&messageRequest); err != nil {
c.JSON(400, gin.H{"Error": "Wrong data"})
return
}
messageUpdate := Message{
Sdp: messageRequest.Sdp,
}
err := ioutil.WriteFile("serverResponse.txt", []byte(messageUpdate.Sdp), 0644)
if err != nil {
c.IndentedJSON(http.StatusFailedDependency, gin.H{"error": err})
} else {
c.JSON(http.StatusOK, "ok")
}
}