You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
import * as bodyParser from "body-parser"; |
|
import * as express from "express"; |
|
import { serialize} from "serializr"; |
|
|
|
import * as schema from "@common/sprinklersRpc/schema"; |
|
import { ServerState } from "../state"; |
|
import requestLogger from "./requestLogger"; |
|
import serveApp from "./serveApp"; |
|
|
|
import { User } from "../models/User"; |
|
import { authentication } from "./authentication"; |
|
import errorHandler from "./errorHandler"; |
|
|
|
export function createApp(state: ServerState) { |
|
const app = express(); |
|
|
|
app.use(requestLogger); |
|
app.use(bodyParser.json()); |
|
|
|
app.get("/api/devices/:deviceId", (req, res) => { |
|
// TODO: authorize device |
|
const device = state.mqttClient.getDevice(req.params.deviceId); |
|
const j = serialize(schema.sprinklersDevice, device); |
|
res.send(j); |
|
}); |
|
|
|
app.get("/api/users", (req, res) => { |
|
User.loadAll(state.database) |
|
.then((users) => { |
|
res.json({ |
|
data: users, |
|
}); |
|
}); |
|
}); |
|
|
|
app.get("/api/users/:username", (req, res, next) => { |
|
User.loadByUsername(state.database, req.params.username) |
|
.then((user) => { |
|
res.json({ |
|
data: user, |
|
}); |
|
}) |
|
.catch(next); |
|
}); |
|
|
|
app.use("/api", authentication(state)); |
|
|
|
serveApp(app); |
|
|
|
app.use(errorHandler); |
|
|
|
return app; |
|
}
|
|
|