-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathweb.go
200 lines (192 loc) · 4.63 KB
/
web.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
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"sync"
"text/template"
)
const WEB_PAGE_TPL = `
<html lang="en">
<head>
<meta charset="utf-8">
<title>smscsim web page</title>
<style>
html, body {
padding: 0;
margin: 0;
font-size: 20px;
font-family: sans-serif;
background: #f0f0f0;
}
#container {
margin: 40px auto;
width: 560px;
padding: 10px 40px;
border-radius: 6px;
box-shadow: 0 0 7px #dfdfdf;
background: #fff;
}
#title {
color: #3585f7;
font-weight: bold;
text-transform: uppercase;
font-size: 24px;
}
form {
margin: 20px auto;
color: #394045;
padding: 10px;
width: 400px;
}
input, label, textarea {
display: block;
box-sizing: border-box;
width: 100%;
border: none;
color: #657c89;
}
label {
text-transform: uppercase;
color: #657c89;
font-size: 14px;
font-weight: bold;
padding: 0;
}
input, textarea {
background: #f0f0f0;
font-size: 20px;
padding: 10px;
margin: 5px 0 20px 0;
border-radius: 3px;
}
textarea {
resize: vertical;
}
select {
min-width: 200px;
}
input[type="submit"] {
font-weight: bold;
font-size: 16px;
color: #fff;
text-transform: uppercase;
background: #3585f7;
}
input[type="submit"]:disabled {
background: #dfdfdf;
}
#message {
color: #009688;
}
.error {
color: #f44336;
}
</style>
</head>
<body>
<div id="container">
<form action="/" method="POST">
<p id="title">Send MO message</p>
<p>
<label for="sender">Sender (MSISDN)</label>
<input id="sender" type="text" name="sender" placeholder="sender" value="{{ .Sender }}">
</p>
<p>
<label for="recipient">Recipient (short number)</label>
<input id="recipient" type="text" name="recipient" placeholder="recipient" value="{{ .Recipient }}">
</p>
<p>
<label for="system_id">System ID</label>
{{ if not .SystemIds }}
<sub class="error">No smpp sessions found. At least one smpp client should connect to smscsim</sub>
{{ end }}
<select id="system_id" name="system_id">
{{ range $systemId := .SystemIds }}
<option value="{{ $systemId }}">{{ $systemId }}</option>
{{ end }}
</select>
</p>
<p>
<label for="short_message">Short message</label>
<textarea id="short_message" name="message" placeholder="Short message..."></textarea>
</p>
<p>
<input type="submit" value="Submit" {{ if not .SystemIds }} disabled {{ end }}>
</p>
{{ if .Message }}
<p id="message">{{ .Message }}</p>
{{ end }}
{{ if .ErrorMessage }}
<p class="error">{{ .ErrorMessage }}</p>
{{ end }}
</form>
</div>
</body>
</html>
`
type WebServer struct {
Smsc Smsc
}
type TplVars struct {
SystemIds []string
Message string
ErrorMessage string
Sender string
Recipient string
}
func NewWebServer(smsc Smsc) WebServer {
return WebServer{smsc}
}
func (webServer *WebServer) Start(port int, wg *sync.WaitGroup) {
defer wg.Done()
http.HandleFunc("/", webHandler(&webServer.Smsc))
log.Println("Starting web server on port", port)
log.Fatal(http.ListenAndServe(fmt.Sprint(":", port), nil))
}
func webHandler(smsc *Smsc) func(http.ResponseWriter, *http.Request) {
if smsc == nil {
log.Fatal("nil Smsc provided to web handler")
}
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
if err := r.ParseForm(); err != nil {
log.Printf("Cannot parse POST params due [%v]", err)
fmt.Fprintf(w, "Error. Cannot parse POST params")
} else {
// parse params
params := r.Form
sender := params.Get("sender")
recipient := params.Get("recipient")
message := params.Get("message")
systemId := params.Get("system_id")
// send MO
err := smsc.SendMoMessage(sender, recipient, message, systemId)
q := url.Values{}
if err != nil {
q.Add("error", err.Error())
} else {
q.Add("message", "MO message was successfully sent")
}
q.Add("sender", sender)
q.Add("recipient", recipient)
redirUrl := "/?" + q.Encode()
http.Redirect(w, r, redirUrl, http.StatusSeeOther)
}
} else {
tpl, err := template.New("webpage").Parse(WEB_PAGE_TPL)
if err != nil {
log.Fatal("Cannot parse template of the web page")
}
q := r.URL.Query()
errorMsg := q.Get("error")
msg := q.Get("message")
sender := q.Get("sender")
recipient := q.Get("recipient")
systemIds := smsc.BoundSystemIds()
tplVars := TplVars{systemIds, msg, errorMsg, sender, recipient}
tpl.Execute(w, tplVars)
}
}
}