forked from junghao/geonet-rest
/
routes.go
94 lines (88 loc) · 3.26 KB
/
routes.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
package main
import (
"github.com/GeoNet/web"
"github.com/GeoNet/web/api/apidoc"
"net/http"
"strings"
)
var docs = apidoc.Docs{
Production: config.WebServer.Production,
APIHost: config.WebServer.CNAME,
Title: `GeoNet API`,
Description: `<p>The data provided here is used for the GeoNet web site and other similar services.
If you are looking for data for research or other purposes then please check the
<a href="http://info.geonet.org.nz/x/DYAO">full range of data</a> available from GeoNet. </p>`,
RepoURL: `https://github.com/GeoNet/geonet-rest`,
StrictVersioning: false,
}
func init() {
docs.AddEndpoint("quake", &quakeDoc)
docs.AddEndpoint("region", ®ionDoc)
docs.AddEndpoint("felt", &feltDoc)
docs.AddEndpoint("news", &newsDoc)
docs.AddEndpoint("impact", &impactDoc)
docs.AddEndpoint("volcano", &volcanoDoc)
}
var exHost = "http://localhost:" + config.WebServer.Port
func router(w http.ResponseWriter, r *http.Request) {
// requests that don't have a specific version header are routed to the latest version.
var latest bool
accept := r.Header.Get("Accept")
switch accept {
case web.V1GeoJSON, web.V1JSON:
default:
latest = true
}
switch {
case strings.HasPrefix(r.URL.Path, "/quake") && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
switch {
case r.URL.Query().Get("intensity") != "":
quakes(w, r)
case r.URL.Query().Get("regionIntensity") != "":
quakesRegion(w, r)
case strings.HasPrefix(r.URL.Path, "/quake/"):
quake(w, r)
default:
web.BadRequest(w, r, "Can't find a route for this request. Please refer to /api-docs")
}
case r.URL.Path == "/intensity" && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
switch {
case r.URL.Query().Get("type") == "measured":
intensityMeasuredLatest(w, r)
// case r.URL.Query().Get("type") == "reported" && r.URL.Query().Get("publicID") == "":
// intensityReportedLatest(w, r)
// case r.URL.Query().Get("type") == "reported" && r.URL.Query().Get("publicID") != "":
// intensityReported(w, r)
default:
web.BadRequest(w, r, "Can't find a route for this request. Please refer to /api-docs")
}
case r.URL.Path == "/felt/report" && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
felt(w, r)
case r.URL.Path == "/volcano/alert/level" && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
alertLevel(w, r)
case r.URL.Path == "/volcano/alert/bulletin" && (accept == web.V1JSON || latest):
w.Header().Set("Content-Type", web.V1JSON)
alertBulletin(w, r)
case strings.HasPrefix(r.URL.Path, "/region/") && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
region(w, r)
case r.URL.Path == "/region" && (accept == web.V1GeoJSON || latest):
w.Header().Set("Content-Type", web.V1GeoJSON)
regions(w, r)
case r.URL.Path == "/news/geonet" && (accept == web.V1JSON || latest):
w.Header().Set("Content-Type", web.V1JSON)
news(w, r)
case strings.HasPrefix(r.URL.Path, apidoc.Path):
docs.Serve(w, r)
case r.URL.Path == "/soh":
soh(w, r)
case r.URL.Path == "/soh/impact":
impactSOH(w, r)
default:
web.BadRequest(w, r, "Can't find a route for this request. Please refer to /api-docs")
}
}