-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrassgo.go
executable file
·60 lines (51 loc) · 1.3 KB
/
strassgo.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
package main
import (
"fmt"
"github.com/go-martini/martini"
"github.com/martini-contrib/cors"
"github.com/martini-contrib/render"
. "github.com/yageek/strassgo/tools"
"io/ioutil"
"log"
"os"
"time"
)
var lastRefresh time.Time
func generateGeoJSON() {
fmt.Println("Generate JSON...")
if traffic := NewTraffic(GetSections()[:], GetInformations()[:]); traffic == nil {
log.Fatal("Could not initialize kml")
} else {
traffic.TOGeoJson()
}
}
func main() {
m := martini.Classic()
corsHandler := cors.Allow(&cors.Options{
AllowOrigins: []string{"https://*.foo.com"},
AllowMethods: []string{"GET"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
})
m.Use(render.Renderer())
m.Get("/", func(r render.Render) {
r.HTML(200, "strassgo", nil)
})
m.Get("/traffic", corsHandler, func() (int, string) {
if fileInfo, err := os.Stat("render/traffic.geojson"); os.IsNotExist(err) {
fmt.Println("First time creating...")
generateGeoJSON()
} else {
currentTime := time.Now()
if currentTime.Sub(fileInfo.ModTime()).Seconds() > 180 {
fmt.Println("File to old...")
lastRefresh = time.Now()
generateGeoJSON()
}
}
data, _ := ioutil.ReadFile("render/traffic.geojson")
return 200, string(data)
})
m.Run()
}