-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
164 lines (141 loc) · 4.71 KB
/
index.ts
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
/*
Rest-Tagging
The MIT License (MIT)
Copyright © 2013-2016 Patrick Liess, http://www.tcs.de
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import express, { Request, Response, NextFunction } from "express";
import morgan from "morgan";
import RedisTagging from "redis-tagging";
import config from "./config.json";
import packageJson from "./package.json";
const rt = new RedisTagging(config.redis);
const PORT = process.env.PORT || config.port || 3000;
const VERSION = packageJson.version;
const RESTPREFIX = config.rest_path_prefix
? (
config.rest_path_prefix.startsWith("/")
? config.rest_path_prefix
: "/" + config.rest_path_prefix
)
: "";
const app = express();
app.use(morgan(process.env.NODE_ENV === "production" ? "combined" : "dev", {
skip: (req) => req.url === "/ping"
}));
app.use(express.json({limit: "50mb"}));
app.use(express.urlencoded({limit: "50mb", extended: true}));
app.disable("x-powered-by");
app.get("/ping", (req, res) => {
return res.json({
result: "pong",
version: VERSION
});
});
/**
* create an item with tags
*/
app.put(RESTPREFIX + "/id/:bucket/:id", async (req: Request, res: Response, next: NextFunction) => {
console.log(req.query.tags);
const tags = req.query.tags ? (JSON.parse(req.query.tags as string) || []) : [];
const score = req.query.score as (string | undefined);
try {
const resp = await rt.set({bucket: req.params.bucket, id: req.params.id, score, tags});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* delete an id
*/
app.delete(RESTPREFIX + "/id/:bucket/:id", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.remove({bucket: req.params.bucket, id: req.params.id});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* get all tags of an id
*/
app.get(RESTPREFIX + "/id/:bucket/:id", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.get({bucket: req.params.bucket, id: req.params.id});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* get all ids of a bucket
*/
app.get(RESTPREFIX + "/allids/:bucket", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.allids({bucket: req.params.bucket});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* tags: the main query. query items for some tags
*/
app.get(RESTPREFIX + "/tags/:bucket", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.tags({
...req.query,
bucket: req.params.bucket,
tags: JSON.parse(req.query.tags as string)
});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* top tags
*/
app.get(RESTPREFIX + "/toptags/:bucket/:amount", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.toptags({bucket: req.params.bucket, amount: parseInt(req.params.amount, 10)});
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* get all buckets
*/
app.get(RESTPREFIX + "/buckets", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.buckets();
return res.json(resp);
} catch (err) {
return next(err);
}
});
/**
* remove bucket
*/
app.delete(RESTPREFIX + "/bucket/:bucket", async (req: Request, res: Response, next: NextFunction) => {
try {
const resp = await rt.removebucket({bucket: req.params.bucket});
return res.json(resp);
} catch (err) {
return next(err);
}
});
// error handler
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
return res.status(500).send({name: err.name, message: err.message});
});
if (require.main === module) {
app.listen(PORT, () => {
console.log("Server started on port " + PORT);
});
}
export default app;