create server

This commit is contained in:
Evan Gray 2022-09-08 20:39:12 +00:00
parent 28055d4b74
commit fc6eb4f599
7 changed files with 1551 additions and 0 deletions

View File

@ -12,12 +12,14 @@ config.define_string("webHost", False, "Public hostname for port forwards")
config.define_bool("mongo", False, "Enable mongo component") config.define_bool("mongo", False, "Enable mongo component")
config.define_bool("mongo-express", False, "Enable mongo-express component") config.define_bool("mongo-express", False, "Enable mongo-express component")
config.define_bool("fly", False, "Enable fly component") config.define_bool("fly", False, "Enable fly component")
config.define_bool("server", False, "Enable server component")
cfg = config.parse() cfg = config.parse()
webHost = cfg.get("webHost", "localhost") webHost = cfg.get("webHost", "localhost")
mongo = cfg.get("mongo", True) mongo = cfg.get("mongo", True)
mongoExpress = cfg.get("mongo-express", True) mongoExpress = cfg.get("mongo-express", True)
fly = cfg.get("fly", True) fly = cfg.get("fly", True)
server = cfg.get("server", True)
if mongo: if mongo:
k8s_yaml("devnet/mongo.yaml") k8s_yaml("devnet/mongo.yaml")
@ -53,3 +55,20 @@ if fly:
"fly", "fly",
resource_deps = ["mongo"] resource_deps = ["mongo"]
) )
if server:
docker_build(
ref = "server",
context = "server",
dockerfile = "server/Dockerfile",
)
k8s_yaml("devnet/server.yaml")
k8s_resource(
"server",
port_forwards = [
port_forward(3000, name = "Server [:3000]", host = webHost),
],
resource_deps = ["mongo"]
)

47
devnet/server.yaml Normal file
View File

@ -0,0 +1,47 @@
---
apiVersion: v1
kind: Service
metadata:
name: server
labels:
app: server
spec:
clusterIP: None
selector:
app: server
ports:
- port: 3000
name: server
protocol: TCP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: server
spec:
selector:
matchLabels:
app: server
serviceName: server
template:
metadata:
labels:
app: server
spec:
restartPolicy: Always
terminationGracePeriodSeconds: 0
containers:
- name: server
image: server:latest
env:
- name: MONGODB_URI
value: mongodb://root:example@mongo:27017/
readinessProbe:
tcpSocket:
port: 3000
periodSeconds: 1
failureThreshold: 300
ports:
- containerPort: 3000
name: server
protocol: TCP

1
server/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

11
server/Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM node:16-alpine@sha256:004dbac84fed48e20f9888a23e32fa7cf83c2995e174a78d41d9a9dd1e051a20
RUN mkdir -p /app
WORKDIR /app
COPY package.json package-lock.json /app/
RUN --mount=type=cache,uid=1000,gid=1000,target=/home/node/.npm \
npm ci
COPY . .
ENTRYPOINT node src/index.js

1375
server/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

6
server/package.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": {
"express": "^4.18.1",
"mongodb": "4.9"
}
}

92
server/src/index.js Normal file
View File

@ -0,0 +1,92 @@
const express = require("express");
const app = express();
const port = 3000;
const { MongoClient } = require("mongodb");
const mongoURI = process.env.MONGODB_URI;
if (!mongoURI) {
console.error("You must set your 'MONGODB_URI' environmental variable.");
process.exit(1);
}
const mongoClient = new MongoClient(mongoURI);
app.get("/api/heartbeats", async (req, res) => {
const database = mongoClient.db("wormhole");
const heartbeats = database.collection("heartbeats");
const cursor = heartbeats.find();
const result = await cursor.toArray();
res.send(result);
});
app.get("/api/vaas/:chain/:emitter/:sequence", async (req, res) => {
const id = `${req.params.chain}/${req.params.emitter}/${req.params.sequence}`;
const database = mongoClient.db("wormhole");
const vaas = database.collection("vaas");
const result = await vaas.findOne({ _id: id });
if (!result) {
res.sendStatus(404);
return;
}
res.send(result);
});
app.get("/api/vaas", async (req, res) => {
const database = mongoClient.db("wormhole");
const vaas = database.collection("vaas");
const limit =
req.query?.limit && req.query.limit <= 100 ? req.query.limit : 20;
const skip = req.query?.page ? req.query?.page * limit : undefined;
const query = req.query?.before
? { createdAt: { $lt: new Date(req.query.before) } }
: {};
const cursor = await vaas.find(query, {
sort: { createdAt: -1 },
skip,
limit,
});
const result = await cursor.toArray();
if (result.length === 0) {
res.sendStatus(404);
return;
}
res.send(result);
});
app.get("/api/observations/:chain/:emitter/:sequence", async (req, res) => {
const id = `${req.params.chain}/${req.params.emitter}/${req.params.sequence}`;
const database = mongoClient.db("wormhole");
const observations = database.collection("observations");
const cursor = observations.find({ _id: { $regex: `^${id}/*` } });
const result = await cursor.toArray();
if (result.length === 0) {
res.sendStatus(404);
return;
}
res.send(result);
});
app.get("/api/observations", async (req, res) => {
const database = mongoClient.db("wormhole");
const observations = database.collection("observations");
const limit =
req.query?.limit && req.query.limit <= 100 ? req.query.limit : 20;
const skip = req.query?.page ? req.query?.page * limit : undefined;
const query = req.query?.before
? { createdAt: { $lt: new Date(req.query.before) } }
: {};
const cursor = await observations.find(query, {
sort: { createdAt: -1 },
skip,
limit,
});
const result = await cursor.toArray();
if (result.length === 0) {
res.sendStatus(404);
return;
}
res.send(result);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});