-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
151 lines (113 loc) · 3 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
package main
import (
"database/sql"
"encoding/json"
"html/template"
"net/http"
"os"
"github.com/joho/godotenv"
_ "github.com/go-sql-driver/mysql"
)
const IndexLat = "52"
const IndexLng = "13"
const IndexZoom = 5
const CityZoom = 15
type MapTemplate struct {
Title string
Lat string
Lng string
Zoom int
}
type Video struct {
YoutubeId string `json:"youtube_id"`
Lat string `json:"lat"`
Lng string `json:"lng"`
}
type Videos []Video
type City struct {
Lat string
Lng string
}
type VideosJsonResponse struct {
Type string `json:"type"`
Data Videos `json:"data"`
Message string `json:"message"`
}
func main() {
err := godotenv.Load(".env")
if err != nil {
panic(err.Error())
}
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets"))))
http.Handle("/sitemap/", http.StripPrefix("/sitemap/", http.FileServer(http.Dir("sitemap"))))
http.HandleFunc("/api/videos", getVideos)
http.HandleFunc("/city/", showCity)
http.HandleFunc("/", showMap)
http.ListenAndServe(":"+os.Getenv("HTTP_PORT"), nil)
}
func showCity(w http.ResponseWriter, r *http.Request) {
citySlug := r.URL.Path[len("/city/"):]
db := connectToDb()
defer db.Close()
results, err := db.Query("SELECT lat, lng FROM cities WHERE name=? LIMIT 1", citySlug)
if err != nil {
panic(err.Error())
}
results.Next()
var city City
err = results.Scan(&city.Lat, &city.Lng)
if err != nil {
panic(err.Error())
}
tmpl, _ := template.ParseFiles("templates/map.tmpl")
tmpl.Execute(w, MapTemplate{
Title: citySlug + " walking and driving videos on map",
Lat: city.Lat,
Lng: city.Lng,
Zoom: CityZoom,
})
}
func showMap(w http.ResponseWriter, r *http.Request) {
tmpl, _ := template.ParseFiles("templates/map.tmpl")
tmpl.Execute(w, MapTemplate{
Title: "Walking and driving Youtube videos on map",
Lat: IndexLat,
Lng: IndexLng,
Zoom: IndexZoom,
})
}
func getVideos(w http.ResponseWriter, r *http.Request) {
keys := r.URL.Query()
llat := keys.Get("llat")
llng := keys.Get("llng")
rlat := keys.Get("rlat")
rlng := keys.Get("rlng")
db := connectToDb()
defer db.Close()
results, err := db.Query("SELECT youtube_id, lat, lng FROM videos WHERE lat > ? AND lat < ? AND lng > ? AND lng < ? AND NOT hide = 1 ORDER BY rating DESC LIMIT 70", llat, rlat, llng, rlng)
if err != nil {
panic(err.Error())
}
var videos Videos
for results.Next() {
var video Video
err = results.Scan(&video.YoutubeId, &video.Lat, &video.Lng)
if err != nil {
panic(err.Error())
}
videos = append(videos, Video{YoutubeId: video.YoutubeId, Lat: video.Lat, Lng: video.Lng})
}
var response = VideosJsonResponse{Type: "success", Data: videos}
json.NewEncoder(w).Encode(response)
}
func connectToDb() *sql.DB {
err := godotenv.Load(".env")
if err != nil {
panic(err.Error())
}
db, err := sql.Open("mysql", os.Getenv("DB_USER")+":"+os.Getenv("DB_PASSWORD")+"@"+os.Getenv("DB_CONNECTION")+"/"+os.Getenv("DB_DATABASE"))
if err != nil {
panic(err.Error())
}
return db
}