-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.go
219 lines (200 loc) · 6.76 KB
/
app.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
"html/template"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
type Name struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Id string `json:"id"`
}
type NamesResponse struct {
Profiles []Name `json:"profiles"`
}
type Genome struct {
Rs9525638 string `json:"rs9525638"`
Rs2908004 string `json:"rs2908004"`
Rs2707466 string `json:"rs2707466"`
Rs7776725 string `json:"rs7776725"`
Id string `json:"id"`
}
type BoneStrength struct {
Score int
CorticalStrength int
ForearmBMD int
LowerForearmRisk int
Description string
}
type BoneStrengthProfile struct {
Name Name
BoneStrength *BoneStrength
}
const (
MAXIMUM_BONE_STRENGTH = 14
API_URI = "https://api.23andme.com"
)
func descriptionForStrength(strength int) string {
switch {
case strength < 5:
return "weak"
case strength < 8:
return "average"
case strength < 11:
return "high"
}
return "superhuman"
}
func computeBoneStrength(g *Genome) (strength *BoneStrength) {
strength = new(BoneStrength)
strength.CorticalStrength = 4 - strings.Count(g.Rs9525638, "T") - strings.Count(g.Rs2707466, "C")
strength.ForearmBMD = 4 - strings.Count(g.Rs2908004, "G") - strings.Count(g.Rs2707466, "C")
strength.LowerForearmRisk = 6 - strings.Count(g.Rs7776725, "C") - strings.Count(g.Rs2908004, "G") - strings.Count(g.Rs2707466, "C")
strength.Score = strength.CorticalStrength + strength.ForearmBMD + strength.LowerForearmRisk
strength.Description = descriptionForStrength(strength.Score)
return
}
func buildConfig() (configs map[string]string) {
configs = make(map[string]string)
genotype_scopes := []string{"rs9525638", "rs2908004", "rs2707466", "rs7776725"}
regular_scopes := []string{"basic", "names"}
scopes := make([]string, len(genotype_scopes)+len(regular_scopes))
copy(scopes, regular_scopes)
copy(scopes[len(regular_scopes):], genotype_scopes)
configs["genotype_scopes"] = strings.Join(genotype_scopes, "%20")
configs["scope"] = strings.Join(scopes, " ")
// Your API credentials and server info
config_keys := []string{"CLIENT_ID", "CLIENT_SECRET", "REDIRECT_URI",
"COOKIE_SECRET", "SESSION_NAME", "SESSION_ACCESS_TOKEN_KEY", "PORT"}
var environment_result string
for _, value := range config_keys {
environment_result = os.Getenv(value)
if environment_result == "" {
log.Fatalf("You must define %s in your environment", value)
}
configs[value] = environment_result
}
return
}
func JSONResponse(http_method string, url string, access_token string) (data []byte, status_code int) {
client := &http.Client{}
req, err := http.NewRequest(http_method, url, nil)
req.Header.Add("Authorization", "Bearer "+access_token)
resp, err := client.Do(req)
data, err = ioutil.ReadAll(resp.Body)
status_code = resp.StatusCode
if err != nil {
log.Printf(err.Error())
}
return
}
func namesByProfile(names *NamesResponse) (names_by_profile map[string]Name) {
names_by_profile = make(map[string]Name)
for _, name := range names.Profiles {
names_by_profile[name.Id] = name
}
return
}
func main() {
config := buildConfig()
store := sessions.NewCookieStore([]byte(config["COOKIE_SECRET"]))
templates := template.Must(template.ParseFiles("public/templates/_base.dtml", "public/templates/index.dtml", "public/templates/result.dtml"))
root, _ := os.Getwd()
log.Println(root)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(root+"/public/static/"))))
http.HandleFunc("/receive_code/", func(w http.ResponseWriter, req *http.Request) {
receiveCode(w, req, config, store, templates)
})
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
index(w, req, config, store, templates)
})
err := http.ListenAndServe(":"+config["PORT"], nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func receiveCode(w http.ResponseWriter, req *http.Request, config map[string]string, store *sessions.CookieStore, templates *template.Template) {
session, _ := store.Get(req, config["SESSION_NAME"])
context, _ := url.ParseQuery(req.URL.RawQuery)
if code, ok := context["code"]; ok {
auth_code := string(code[0])
resp, _ := http.PostForm(API_URI+"/token/",
url.Values{"client_id": {config["CLIENT_ID"]},
"client_secret": {config["CLIENT_SECRET"]},
"grant_type": {"authorization_code"},
"code": {auth_code},
"redirect_uri": {config["REDIRECT_URI"]},
"scope": {config["scope"]},
})
defer resp.Body.Close()
if resp.StatusCode == 200 {
var t_res TokenResponse
dec := json.NewDecoder(resp.Body)
err := dec.Decode(&t_res)
if err != nil {
log.Printf(err.Error())
} else {
session.Values[config["SESSION_ACCESS_TOKEN_KEY"]] = t_res.AccessToken
session.Save(req, w)
http.Redirect(w, req, "/", 303)
}
}
} else if error_type, ok := context["error"]; ok {
fmt.Fprintf(w, "%s: %s", string(error_type[0]), string(context["error_description"][0]))
}
}
func index(w http.ResponseWriter, req *http.Request, config map[string]string, store *sessions.CookieStore, templates *template.Template) {
session, _ := store.Get(req, config["SESSION_NAME"])
token, ok := session.Values[config["SESSION_ACCESS_TOKEN_KEY"]]
if !ok {
context := map[string]string{
"path": req.URL.Path,
"client_id": config["CLIENT_ID"],
"scope": config["scope"],
"redirect_uri": config["REDIRECT_URI"],
}
_ = templates.ExecuteTemplate(w, "index", context)
} else {
access_token, _ := token.(string)
data, status := JSONResponse("GET", API_URI+"/1/names/", access_token)
if status != 200 {
// Probably, the auth code expired. Go back home and re-authenticate.
delete(session.Values, config["SESSION_ACCESS_TOKEN_KEY"])
session.Save(req, w)
http.Redirect(w, req, "/", 303)
}
var names NamesResponse
var genotypes []Genome
err := json.Unmarshal(data, &names)
data, status = JSONResponse("GET", API_URI+"/1/genotype/?locations="+config["genotype_scopes"], access_token)
err = json.Unmarshal(data, &genotypes)
if err != nil {
log.Printf(err.Error())
}
names_by_profile := namesByProfile(&names)
var boneStrengthProfiles []BoneStrengthProfile
for _, genotype := range genotypes {
boneStrength := computeBoneStrength(&genotype)
boneStrengthProfile := BoneStrengthProfile{
BoneStrength: boneStrength,
Name: names_by_profile[genotype.Id],
}
boneStrengthProfiles = append(boneStrengthProfiles, boneStrengthProfile)
}
_ = templates.ExecuteTemplate(w, "result", boneStrengthProfiles)
}
}