This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
155 lines (83 loc) · 4.74 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>index.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div class="container">
<div class="page">
<div class="header">
<h1>index.js</h1>
</div>
<h2>index.js</h2>
<p>This file is designed to serve as a demonstration of Tidepool best practices for API design
and implementation. </p>
<p>It would be a good idea to read
<a href="https://github.com/tidepool-org/central/wiki">the central wiki on Tidepool's github account</a>.</p>
<div class='highlight'><pre>
<span class="comment">/*
* == TIDEPOOL LICENSE ==
* Copyright (C) 2013 Tidepool Project
*
* This source code is subject to the terms of the Tidepool Open Data License, v. 1.0.
* If a copy of the license was not provided with this file, you can obtain one at:
* http://tidepool.org/license/
*
* == TIDEPOOL LICENSE ==
*/</span></pre></div>
<p>We currently use the coffeescript-inspired anonymous namespacing model, where we create a function
and then call it with this. It keeps the namespace from being accidentally overridden
although it doesn't give us actual namespaces. </p>
<div class='highlight'><pre>(<span class="keyword">function</span>() {</pre></div>
<p>We use strict because we‘re only worried about modern browsers and we should be strict.
JSHint actually insists on this and it’s a good idea.</p>
<div class='highlight'><pre> <span class="string">'use strict'</span>;</pre></div>
<p>It‘s also a good idea to predeclare all variables at the top of a scope. Javascript doesn’t
support block scoping so putting them all at the beginning is a smart move.</p>
<div class='highlight'><pre> <span class="keyword">var</span> echo, envConfig, port, restify, server;</pre></div>
<p>Server code needs the environment.</p>
<div class='highlight'><pre> envConfig = process.env;</pre></div>
<p>Restify helps us with building a RESTful API.</p>
<div class='highlight'><pre> restify = require(<span class="string">'restify'</span>);
server = restify.createServer({</pre></div>
<p>I don't know what this name actually does. </p>
<div class='highlight'><pre> name: <span class="string">'TidepoolDummy'</span>
});</pre></div>
<p>Two standard restify handler plugins:</p>
<div class='highlight'><pre> server.use(restify.queryParser());
server.use(restify.bodyParser());</pre></div>
<p>This function merely echoes everything it got as a block of text. Useful for debugging.</p>
<div class='highlight'><pre> echo = <span class="keyword">function</span>(req, res, next) {
console.log(<span class="string">'request'</span>, req.params, req.url, req.method);
res.send([
<span class="string">'Echo!'</span>, {
params: req.params,
headers: req.headers,
method: req.method
}
]);
<span class="keyword">return</span> next();
};</pre></div>
<p>We need to have sensible responses for all the standard verbs.
Versioning of the API still needs some work — restify supports a standard model out of the box.
Maybe we should use it.</p>
<div class='highlight'><pre> server.get(<span class="string">'/api/v1/echo'</span>, echo);
server.post(<span class="string">'/api/v1/echo'</span>, echo);
server.put(<span class="string">'/api/v1/echo'</span>, echo);
server.del(<span class="string">'/api/v1/echo'</span>, echo);
server.head(<span class="string">'/api/v1/echo'</span>, echo);</pre></div>
<p>If the port is specified in the environment we'll use it, but for deploys we
want to run on port 80 and then map it in the router.</p>
<div class='highlight'><pre> port = envConfig.DUMMY_PORT || <span class="number">80</span>;
console.log(<span class="string">'echo API server serving on port'</span>, port);
server.listen(port);</pre></div>
<p>Wrap up the javascript namespacing model.</p>
<div class='highlight'><pre>}).call(<span class="keyword">this</span>);</pre></div>
<div class="fleur">h</div>
</div>
</div>
</body>
</html>