-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
178 lines (130 loc) · 6.28 KB
/
server.js
File metadata and controls
178 lines (130 loc) · 6.28 KB
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
// SETUP REQUIREMENTS
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var cookieParser = require('cookie-parser');
var cors = require('cors');
var polyanno = require('polyanno_storage');
////have released polyanno_storage as a separate NPM package so you should be able to simply "npm install" it the same as the other packages
///need to set a function to (re)define those variables here within this package rather than just recalling what is defined in the node_modules package...
var databaseURL = polyanno.setup.databaseport;
var thisWebsitePort = polyanno.setup.applicationport;
// GET APPLICATION RUNNING
var app = express();
////BASIC APP ROUTES & SETUP
app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/views'));
app.use(express.static(__dirname + '/examples'));
app.get('/', function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.use(cors());
//Currently using cors for all origins just for development but will need to be specific for actual deployment
///////SETUP
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
var port = process.env.PORT || thisWebsitePort;
mongoose.connect(databaseURL, { config: { autoIndex: false } });
//////MIDDLEWARE
var annoRouter = express.Router();
annoRouter.use(function(req, res, next) {
///logging here
console.log("the body is "+JSON.stringify(req.body)+" and params "+JSON.stringify(req.params));
next();
});
var editorRouter = express.Router();
editorRouter.use(function(req, res, next) {
//should have proper logging here
next();
});
var userRouter = express.Router();
userRouter.use(function(req, res, next) {
//should have proper logging here
next();
});
/////////////////PAGE ROUTES
editorRouter.get('/:name', function (req, res, next) {
////specific to University of Edinburgh application to prevent general spamming with any old info.json through the website
var fileName = "http://images.is.ed.ac.uk/luna/servlet/iiif/"+req.params.name+"/info.json";
var options = {
root: __dirname + '/views/',
headers: {
'Set-Cookie': "imageviewing="+fileName+"; path=/editors " ///expires todays date???
}
};
res.sendFile("theimage.html", options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
//console.log('Sent with '+JSON.stringify(options.headers));
}
});
});
editorRouter.use(express.static(__dirname + '/public'));
editorRouter.use(express.static(__dirname + '/views'));
editorRouter.use(express.static(__dirname + '/examples'));
app.get('/beginnertutorial', function(req, res) {
res.redirect("/beginnertutorial.html");
});
app.get('/aboutus', function(req, res) {
res.redirect("/aboutus.html");
});
/////////////////USER ROUTES
userRouter.get('/username/:username', polyanno.users.getByUsername);
userRouter.get('/id/:user_id', polyanno.users.getByID);
userRouter.post('/', polyanno.users.addNew);
userRouter.put('/:username', polyanno.users.updateOne);
/////////////////API ROUTES
//ANNOTATIONS API
annoRouter.post('/annotations', polyanno.annotations.addNew );
annoRouter.put( polyanno.annotations.updateOne ); ///
annoRouter.get('/annotations', polyanno.annotations.getAll);
annoRouter.get('/annotations/vectors', polyanno.annotations.getAllVectorAnnos);
annoRouter.get('/annotations/transcriptions', polyanno.annotations.getAllTranscriptionAnnos);
annoRouter.get('/annotations/translations', polyanno.annotations.getAllTranslationAnnos);
annoRouter.get('/annotations/:anno_id', polyanno.annotations.getByID);
annoRouter.get('/annotations/target/:target_id', polyanno.annotations.getByTarget);
annoRouter.get('/annotations/body/:body_id', polyanno.annotations.getByBody);
annoRouter.delete('/annotations', polyanno.annotations.deleteAll);
annoRouter.delete('/annotations/:anno_id', polyanno.annotations.deleteOne);
//VECTOR API
annoRouter.get('/vectors', polyanno.vectors.findAll);
annoRouter.post('/vectors', polyanno.vectors.addNew);
annoRouter.delete('/vectors', polyanno.vectors.deleteAll);
annoRouter.get('/vectors/:vector_id', polyanno.vectors.getByID);
annoRouter.put('/vectors/:vector_id', polyanno.vectors.updateOne);
annoRouter.delete('/vectors/:vector_id', polyanno.vectors.deleteOne);
annoRouter.get('/vectors/targets/:target', polyanno.annotations.getVectorsByTarget);
annoRouter.get('/vectors/ids/:_ids/target/:target_id', polyanno.vectors.searchByIds);
//TRANSCRIPTION API
annoRouter.get('/transcriptions', polyanno.transcriptions.findAll);
annoRouter.post('/transcriptions', polyanno.transcriptions.addNew);
annoRouter.get('/transcriptions/:transcription_id', polyanno.transcriptions.getByID);
annoRouter.put('/transcriptions/:transcription_id', polyanno.transcriptions.updateOne);
annoRouter.put('/transcriptions/voting/:voteType', polyanno.transcriptions.voting);
annoRouter.delete('/transcriptions/:transcription_id', polyanno.transcriptions.deleteOne);
annoRouter.delete('/transcriptions', polyanno.transcriptions.deleteAll);
annoRouter.get('/transcriptions/targets/:target', polyanno.annotations.getTranscriptionsByTarget);
annoRouter.get('/transcriptions/ids/:_ids/target/:target_id', polyanno.transcriptions.searchByIds);
//TRANSLATION API
annoRouter.get('/translations', polyanno.translations.findAll);
annoRouter.post('/translations', polyanno.translations.addNew);
annoRouter.get('/translations/:transcription_id', polyanno.translations.getByID);
annoRouter.put('/translations/:transcription_id', polyanno.translations.updateOne);
annoRouter.put('/translations/voting/:voteType', polyanno.translations.voting);
annoRouter.delete('/translations/:transcription_id', polyanno.translations.deleteOne);
annoRouter.delete('/translations', polyanno.translations.deleteAll);
annoRouter.get('/translations/targets/:target', polyanno.annotations.getTranslationsByTarget);
annoRouter.get('/translations/ids/:_ids/target/:target_id', polyanno.translations.searchByIds);
/////////GET STARTED
app.use('/api', annoRouter);
app.use('/editors', editorRouter);
app.use('/user', userRouter);
app.use(function(req, res, next) {
res.status(404).redirect("/404page.html");
});
//tells the server where it should be listening for req and res
app.listen(thisWebsitePort);