-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
executable file
·181 lines (171 loc) · 4.48 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
package main
import (
"flag"
"fmt"
"html/template"
"log"
"math/rand"
"net"
"net/http"
"strings"
"time"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
var (
osrmServer, geoipServer string
)
func main() {
flag.StringVar(&osrmServer, "osrm", "http://osrm.makemydrive.fun", "address of OSRM server")
flag.StringVar(&geoipServer, "geoip", "http://geoip.makemydrive.fun", "address of GeoIP server")
flag.Parse()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
router.Use(sessions.Sessions("mysession", store))
router.LoadHTMLGlob("templates/*")
router.Static("/static", "./static")
router.GET("/", handleIndex)
router.GET("/favicon.ico", func(c *gin.Context) {
c.Redirect(302, "/static/img/meta/favicon.ico")
})
router.GET("/map/:mapid", handleMap)
router.GET("/directions/:mapid", handleDirections)
router.POST("/", handlePost)
fmt.Println("Serving at http://localhost:5003/")
router.Run(":5003")
}
type MainView struct {
Error string
Previous map[string]string
Features []GeoJSONPointsFeature
GeoJSON template.JS
Latitude float64
Longitude float64
MapID string
Route RoadTrip
Acknowledge string
RandomAttraction GeoJSONPointsFeature
CurrentLocation string
}
func handleIndex(c *gin.Context) {
serverName := strings.Replace(osrmServer, "http://", "", -1)
if !strings.Contains(serverName, ":") {
serverName += ":80"
}
_, err := net.DialTimeout("tcp", serverName, 5*time.Second)
clientIP, errIP := GetClientIPHelper(c.Request)
var loc string
if errIP == nil {
log.Println(clientIP)
loc, _ = LocationFromIP(clientIP)
} else {
log.Println(errIP.Error())
}
acknowledge := strings.Title(adjectives[rand.Intn(len(adjectives)-1)])
if len(acknowledge) < 2 {
acknowledge = "Go"
}
view := MainView{
Acknowledge: acknowledge,
CurrentLocation: loc,
}
if err == nil {
c.HTML(http.StatusOK, "main", view)
} else {
log.Println(err)
c.HTML(http.StatusOK, "down", view)
}
}
func handleMap(c *gin.Context) {
mapid := c.Param("mapid")
r, err := LoadFromName(mapid)
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
}
c.HTML(http.StatusOK, "map", MainView{
Route: *r,
Features: r.NearbyFeatures,
GeoJSON: template.JS(r.JSONString()),
Latitude: r.Origin.Latitude,
Longitude: r.Origin.Longitude,
MapID: r.Name(),
})
}
func handleDirections(c *gin.Context) {
mapid := c.Param("mapid")
r, err := LoadFromName(mapid)
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
}
c.HTML(http.StatusOK, "directions", MainView{
Route: *r,
Features: r.NearbyFeatures,
GeoJSON: template.JS(r.JSONString()),
Latitude: r.Origin.Latitude,
Longitude: r.Origin.Longitude,
MapID: r.Name(),
RandomAttraction: r.NearbyFeatures[rand.Intn(len(r.NearbyFeatures)-1)],
})
}
func handlePost(c *gin.Context) {
type FormInput struct {
Origin string `form:"origin" json:"origin" binding:"required"`
Destination string `form:"destination" json:"destination" binding:"required"`
Est string
}
var form FormInput
if err := c.ShouldBind(&form); err == nil {
origin, err := Geocode(form.Origin)
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
return
}
destination, err := Geocode(form.Destination)
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
return
}
var r *RoadTrip
r, err = Load(origin, destination)
if err != nil {
r, err = New(origin, destination)
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
return
}
r.FindNearbyFeatures()
err = r.Save()
if err != nil {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
return
}
} else {
log.Println("loaded")
}
c.Redirect(http.StatusMovedPermanently, "/directions/"+r.Name())
} else {
c.HTML(http.StatusOK, "main", MainView{
Error: err.Error(),
Acknowledge: "Go",
})
}
}