-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (114 loc) · 3.17 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
// serveGBFS project main.go
package main
import (
"encoding/gob"
"encoding/json"
_ "fmt"
"log"
_ "math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/patrickmn/go-cache"
)
func saveCache() {
dataFile, err := os.Create("cache.gob")
if err != nil {
log.Println(err)
os.Exit(1)
}
// serialize the data
dataEncoder := gob.NewEncoder(dataFile)
dataEncoder.Encode(c.Items())
dataFile.Close()
}
func getCache() (map[string]cache.Item, error) {
var data map[string]cache.Item
// open data file
dataFile, err := os.Open("cache.gob")
if err != nil {
log.Println(err)
return nil, err
}
dataDecoder := gob.NewDecoder(dataFile)
err = dataDecoder.Decode(&data)
if err != nil {
log.Println(err)
return nil, err
}
dataFile.Close()
return data, nil
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Nothing to see here", http.StatusForbidden)
return
}
func autodiscoverHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
now := time.Now() // current local time
sec := now.Unix()
profile := GbfsMain{int(sec), 0, "2.0", System{}}
js, err := json.Marshal(profile)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func gbfsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
now := time.Now() // current local time
sec := now.Unix()
id := strings.TrimPrefix(r.URL.RequestURI(), "/gbfs/")
log.Println("Hello..." + id)
// Set the value of the key "baz" to 42, with no expiration time
// (the item won't be removed until it is re-set, or removed using
// c.Delete("baz")
//c.Set("gbfs_versions", rand.Intn(99-1)+1, cache.NoExpiration)
profile := GbfsMain{int(sec), 0, "2.0", System{}}
c.Set(id, profile, cache.NoExpiration)
foo, found := c.Get(id)
if found {
log.Println(foo)
} else {
log.Println("Not found in cache")
}
js, err := json.Marshal(foo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
saveCache()
}
var c *cache.Cache
func main() {
data, err := getCache()
if err != nil {
log.Println("Cache not found")
c = cache.New(5*time.Minute, 10*time.Minute)
} else {
c = cache.NewFrom(5*time.Minute, 10*time.Minute, data)
}
port := os.Getenv("PORT")
if len(port) == 0 {
port = "8080"
}
http.HandleFunc("/", indexHandler)
http.HandleFunc("/gbfs.json", autodiscoverHandler)
http.HandleFunc("/gbfs/gbfs_versions", gbfsHandler)
http.HandleFunc("/gbfs/system_information", gbfsHandler)
http.HandleFunc("/gbfs/station_information", gbfsHandler)
http.HandleFunc("/gbfs/station_status", gbfsHandler)
http.HandleFunc("/gbfs/free_bike_status", gbfsHandler)
http.HandleFunc("/gbfs/system_hours", gbfsHandler)
http.HandleFunc("/gbfs/system_calendar", gbfsHandler)
http.HandleFunc("/gbfs/system_regions", gbfsHandler)
http.HandleFunc("/gbfs/system_pricing_plans", gbfsHandler)
http.HandleFunc("/gbfs/system_alerts", gbfsHandler)
log.Print("GBFS server started")
http.ListenAndServe(":"+port, nil)
}