forked from pebble-dev/rebblestore-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.go
115 lines (101 loc) · 4.33 KB
/
boot.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"github.com/gorilla/mux"
)
const (
PEBBLE_BOOT_URL string = "https://boot.getpebble.com/api/config/"
STORE_URI string = "https://santoku.adamfourney.com"
)
// BootJSON is just a Go container object for the JSON response.
type BootJSON struct {
Config BootConfig `json:"config"`
}
// BootConfig contains the webviews from the JSON file.
type BootConfig struct {
Algolia json.RawMessage `json:"algolia"`
AppMeta json.RawMessage `json:"app_meta"`
Authentication json.RawMessage `json:"authentication"`
Cohorts json.RawMessage `json:"cohorts"`
Developer json.RawMessage `json:"developer"`
Health json.RawMessage `json:"health"`
Href json.RawMessage `json:"href"`
Id json.RawMessage `json:"id"`
KeenIo json.RawMessage `json:"keen_io"`
LinkedServices json.RawMessage `json:"linked_services"`
Links json.RawMessage `json:"links"`
Locker json.RawMessage `json:"locker"`
Notifications json.RawMessage `json:"notifications"`
SupportRequest json.RawMessage `json:"support_request"`
Timeline json.RawMessage `json:"timeline"`
TreasureData json.RawMessage `json:"treasure_data"`
Voice json.RawMessage `json:"voice"`
Webviews map[string]string `json:"webviews"`
}
// WebviewConfig contains the webviews in-which we would like to override.
type WebviewsConfig struct {
FAQ string `json:"support/faq"`
Application string `json:"appstore/application"`
ApplicationChangelog string `json:"appstore/application_changelog"`
DeveloperApps string `json:"appstore/developer_apps"`
Watchfaces string `json:"appstore/watchfaces"`
Watchapps string `json:"appstore/watchapps"`
}
// BootHandler is based off of [@afourney|https://github.com/afourney]'s
// development bootstrap override.
func BootHandler(w http.ResponseWriter, r *http.Request) {
WriteCommonHeaders(w)
// Get a store uri from the request and determine if it matches a valid URI
store_uri := r.URL.Query().Get("store_uri")
if _, err := url.Parse(store_uri); err != nil {
w.WriteHeader(400)
w.Write([]byte("Invalid store_uri parameter"))
return
}
// Copying the URL Query to modify it later
urlquery := r.URL.Query()
// If the user didn't specify a store_uri, use the pebble server
if store_uri == "" {
store_uri = STORE_URI
} else {
urlquery.Del("store_uri")
}
// Build up the request URL
request_url := fmt.Sprintf("%s%s?%s", PEBBLE_BOOT_URL, mux.Vars(r)["path"], urlquery.Encode())
// Make a request to an external server then parse the request
req, err := http.Get(request_url)
if err != nil {
log.Fatal(err)
}
data, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatal(err)
}
// Decode the JSON data
response := &BootJSON{}
err = json.Unmarshal(data, response)
if err != nil {
log.Println(err)
w.Write(data)
return
}
// Replace items in the JSON object, then prepare to output it
response.Config.Webviews["support/faq"] = fmt.Sprintf("%s/faq", store_uri)
response.Config.Webviews["appstore/application"] = fmt.Sprintf("%s/application/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
response.Config.Webviews["appstore/application_changelog"] = fmt.Sprintf("%s/changelog/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
response.Config.Webviews["appstore/developer_apps"] = fmt.Sprintf("%s/developer/$$id$$?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
response.Config.Webviews["appstore/watchfaces"] = fmt.Sprintf("%s/watchfaces?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
response.Config.Webviews["appstore/watchapps"] = fmt.Sprintf("%s/watchapps?pebble_color=$$pebble_color$$&hardware=$$hardware$$&uid=$$user_id$$&mid=$$phone_id$$&pid=$$pebble_id$$&$$extras$$", store_uri)
data, err = json.MarshalIndent(response, "", "\t")
if err != nil {
log.Fatal(err)
}
// Send the JSON object back to the user
w.Header().Add("content-type", "application/json")
w.Write(data)
}