Compare commits
No commits in common. "91480fea64d42a3b5af4352fd8e4944d8735ca98" and "f0c920a0d533ff2e1c793a0b9756ee6fdfc76b4f" have entirely different histories.
91480fea64
...
f0c920a0d5
|
@ -2,7 +2,6 @@ import { PostListener, MessagesResponder } from "./routes";
|
|||
import express from "express";
|
||||
import dotenv from "dotenv";
|
||||
import { Logger } from "./utils";
|
||||
import ApiRoute from "./utils/ApiRoute";
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
|
@ -12,10 +11,8 @@ const post = new PostListener(app);
|
|||
const query = new MessagesResponder(app);
|
||||
const port = +process.env.PORT || 8080;
|
||||
|
||||
ApiRoute.start(app);
|
||||
post.start();
|
||||
query.start();
|
||||
logger.log("Loading complete.");
|
||||
|
||||
app.listen(port, () => {
|
||||
logger.log(`Server started on ${Logger.emp(`http://localhost:${port}`)}`);
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
import { userReviewSchema, typeJson } from "../types";
|
||||
import { Logger, Responder, data } from "../utils";
|
||||
import { Express, Request, Response } from "express";
|
||||
import ApiRoute from "../utils/ApiRoute";
|
||||
|
||||
export class PostListener extends ApiRoute {
|
||||
export class PostListener {
|
||||
private server: Express;
|
||||
private readonly logger = new Logger("Post Listener");
|
||||
|
||||
constructor(server: Express) {
|
||||
// Route always trails a slash, the final result is is "/api/post". See src/utils/ApiRoute.ts
|
||||
super(server, "/post");
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
this.server.post(this.routeName, (req: Request, res: Response) => {
|
||||
this.server.post("/post", (req: Request, res: Response) => {
|
||||
req.on("data", async (chunk) => {
|
||||
const sender = `${Logger.bold(Logger.emp("Client:"))} ${Logger.emp(req.headers["user-agent"])}`;
|
||||
|
||||
|
@ -64,6 +65,6 @@ export class PostListener extends ApiRoute {
|
|||
});
|
||||
});
|
||||
|
||||
this.complete();
|
||||
this.logger.log("Loading complete.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
import { Express, Request, Response } from "express";
|
||||
import { Logger, Responder, data } from "../utils";
|
||||
import { Logger, data } from "../utils";
|
||||
import { IdRequest, typeJson } from "../types";
|
||||
import ApiRoute from "../utils/ApiRoute";
|
||||
|
||||
export class MessagesResponder extends ApiRoute {
|
||||
export class MessagesResponder {
|
||||
private server: Express;
|
||||
private readonly logger = new Logger("Query");
|
||||
|
||||
constructor(server: Express) {
|
||||
// Route always trails a slash, the final result is "/api/reviews". See src/utils/ApiRoute.ts
|
||||
super(server, "/reviews");
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
public async start(): Promise<void> {
|
||||
this.server.get(this.routeName, (req: Request, res: Response) => {
|
||||
this.server.get("/reviews", (req: Request, res: Response) => {
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviews();
|
||||
|
||||
|
@ -21,23 +22,17 @@ export class MessagesResponder extends ApiRoute {
|
|||
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
|
||||
});
|
||||
|
||||
this.server.get(`${this.routeName}/:id`, (req: IdRequest, res: Response) => {
|
||||
this.server.get("/reviews/:id", (req: IdRequest, res: Response) => {
|
||||
const receiver = req.headers["user-agent"];
|
||||
const result = data.getReviewById(req.params.id);
|
||||
|
||||
if (Object.keys(result).length) {
|
||||
res.writeHead(200, typeJson);
|
||||
res.write(JSON.stringify(result));
|
||||
res.end();
|
||||
} else {
|
||||
res.writeHead(404, typeJson);
|
||||
res.write(Responder.requestError("review not found"));
|
||||
res.end();
|
||||
}
|
||||
|
||||
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
|
||||
});
|
||||
|
||||
this.complete();
|
||||
this.logger.log("Loading complete.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,38 +0,0 @@
|
|||
import { Express, Request, Response } from "express";
|
||||
import { Logger } from "./logger";
|
||||
import { typeJson } from "../types";
|
||||
import { Responder } from "./responder";
|
||||
|
||||
export default class ApiRoute {
|
||||
public readonly server: Express;
|
||||
public readonly routeName: string;
|
||||
public readonly logger = new Logger("API");
|
||||
|
||||
constructor(server: Express, route: string) {
|
||||
// API Routes are a list of routes, the beginning route should be first
|
||||
const mainRoute = `/api${route}`;
|
||||
|
||||
this.server = server;
|
||||
this.routeName = mainRoute;
|
||||
}
|
||||
|
||||
// this.complete() should be called when the route is finished initalizing.
|
||||
// This makes it easy to see what routes
|
||||
public complete(): void {
|
||||
this.logger.log("Loaded", Logger.emp(this.routeName));
|
||||
}
|
||||
|
||||
public static async start(server: Express): Promise<void> {
|
||||
server.post("/api", (req: Request, res: Response) => {
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.success("ok"));
|
||||
res.end();
|
||||
});
|
||||
|
||||
server.get("/api", (req: Request, res: Response) => {
|
||||
res.writeHead(400, typeJson);
|
||||
res.write(Responder.success("ok"));
|
||||
res.end();
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue