-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
302 lines (219 loc) · 6.35 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
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
package main
import (
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"github.com/go-chi/chi"
"github.com/gorilla/handlers"
"github.com/joho/godotenv"
"golang.org/x/net/html"
log "github.com/sirupsen/logrus"
)
var MAPSERVER_EXEC = ""
func main() {
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
envFilePath := ".env"
args := os.Args[1:]
if len(args) > 0 {
envFilePath = args[0]
}
LoadDotEnvFile(envFilePath)
MAPSERVER_EXEC = GetMapServerPath()
router := chi.NewRouter()
router.Route(GetRootPath(), func(r chi.Router) {
r.Get("/", handleTile)
})
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
originsOk := handlers.AllowedOrigins(GetAllowedOrigins())
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
port := GetApiPort()
host := fmt.Sprintf(":%d", port)
handler := handlers.CORS(originsOk, headersOk, methodsOk)(router)
log.Info("Listening at " + host)
http.ListenAndServe(host, handler)
}
func handleTile(w http.ResponseWriter, r *http.Request) {
queryString := r.URL.RawQuery
if queryString == "" {
http.Error(w, "Query parameters cannot be empty", http.StatusBadRequest)
return
}
b, err := getTileFromDisk(queryString)
if err == nil {
w.Header().Add("Content-Type", "image/jpeg")
w.Header().Add("Content-Disposition", "inline; filename=\"tile.jpeg\"")
w.Write(b)
return
}
b, imgType, err := renderTile(MAPSERVER_EXEC, queryString)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
go saveTile(queryString, b)
w.Header().Add("Content-Type", fmt.Sprintf("image/%s", imgType))
w.Header().Add("Content-Disposition", fmt.Sprintf("inline; filename=\"image.%s\"", imgType))
w.Write(b)
}
func renderTile(mapServerExecPath, queryString string) ([]byte, string, error) {
queryString = fmt.Sprintf("QUERY_STRING=%s", queryString)
cmd := exec.Command(
mapServerExecPath,
queryString,
)
out, err := cmd.Output()
if err != nil {
msg := fmt.Sprintf("Failed to render tile: %v", err)
log.Error(msg)
return nil, "", errors.New(msg)
}
response := string(out)
if strings.Contains(response, "MapServer Message") {
message, err := parseMapServerErrorMessage(response)
if err != nil {
return nil, "", errors.New("cannot generate tile")
}
log.Errorf("cannot generate tile. Cause: %s", message)
return nil, "", fmt.Errorf("cannot generate tile. %s", message)
}
imgType := ""
if strings.Contains(response, "image/jpeg") {
imgType = "jpeg"
}
if strings.Contains(response, "image/png") {
imgType = "png"
}
contentType := fmt.Sprintf("Content-Type: image/%s", imgType)
response = strings.ReplaceAll(response, contentType, "")
response = strings.TrimSpace(response)
return []byte(response), imgType, nil
}
func parseMapServerErrorMessage(htmlResponse string) (string, error) {
tokenizer := html.NewTokenizer(strings.NewReader(htmlResponse))
for {
tt := tokenizer.Next()
if tt == html.ErrorToken {
if tokenizer.Err() == io.EOF {
break
}
continue
}
tag, _ := tokenizer.TagName()
if strings.Contains(string(tag), "body") {
tokenizer.Next()
errorMsg := string(tokenizer.Text())
return errorMsg, nil
}
}
return "", errors.New("cannot parse MapServer error message")
}
func saveTile(queryString string, bytes []byte) error {
queryHash, err := createMd5Hash(queryString)
if err != nil {
log.Errorf("Cannot save tile. Cause: %s", err.Error())
return fmt.Errorf("cannot generate tile hash. Cause: %w", err)
}
filepath := GetCachePath() + string(os.PathSeparator) + queryHash
if _, err := os.Stat(filepath); err == nil {
return nil
}
err = os.WriteFile(filepath, bytes, 0644)
if err != nil {
log.Warnf("cannot create tile file. Cause: %s", err)
return fmt.Errorf("cannot create tile file. Cause: %w", err)
}
return nil
}
func getTileFromDisk(queryString string) ([]byte, error) {
queryHash, err := createMd5Hash(queryString)
if err != nil {
return nil, fmt.Errorf("cannot generate tile hash. Cause: %w", err)
}
filepath := GetCachePath() + string(os.PathSeparator) + queryHash
if _, err := os.Stat(filepath); err != nil {
return nil, errors.New("tile is not cached")
}
bytes, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("cannot read tile from disk. Cause: %w", err)
}
return bytes, nil
}
func createMd5Hash(text string) (string, error) {
hasher := md5.New()
_, err := io.WriteString(hasher, text)
if err != nil {
return "", err
}
return hex.EncodeToString(hasher.Sum(nil)), nil
}
func LoadDotEnvFile(path string) {
if err := godotenv.Load(path); err != nil {
msg := "Cannot load '%s' environment file. " +
"Looking for environment variables in the system environment"
log.Warnf(msg, path)
}
}
func GetMapServerPath() string {
envVar := os.Getenv("MAPSERVER_WRAPPER_MAP_SERV_EXEC_PATH")
if envVar == "" {
fmt.Println("MAPSERVER_WRAPPER_ALLOWED_ORIGINS environment variable is not defined. Aborted.")
os.Exit(1)
}
return envVar
}
func GetAllowedOrigins() []string {
envVar := os.Getenv("MAPSERVER_WRAPPER_ALLOWED_ORIGINS")
if envVar != "" {
return strings.Split(envVar, ",")
}
log.Warn("MAPSERVER_WRAPPER_ALLOWED_ORIGINS environment variable is not defined. Accepting only local connections")
return []string{GetLocalHost()}
}
func GetApiPort() int {
envVar := os.Getenv("MAPSERVER_WRAPPER_PORT")
if envVar != "" {
port, err := strconv.Atoi(envVar)
if err == nil {
return port
}
log.Warn("Cannot parse MAPSERVER_WRAPPER environment variable. Port must be an integer.")
}
log.Warn("MAPSERVER_WRAPPER is not defined. Using default port 8000.")
return 8000
}
func GetLocalHost() string {
logLevel := log.GetLevel()
log.SetLevel(0)
port := GetApiPort()
log.SetLevel(logLevel)
return fmt.Sprintf("http://localhost:%d", port)
}
func GetRootPath() string {
root := os.Getenv("MAPSERVER_WRAPPER_BASE_PATH")
if root != "" && len(root) > 0 {
if root[0] == '/' {
return root
}
}
log.Warn("MAPSERVER_WRAPPER_BASE_PATH environment variable is not defined. Default is /")
return "/"
}
func GetCachePath() string {
path := os.Getenv("MAPSERVER_WRAPPER_CACHE_PATH")
if path != "" {
return path
}
log.Warn("MAPSERVER_WRAPPER_CACHE_PATH environment variable is not defined." +
"Cached tiles will be stored in ./cache folder")
return "cache"
}