Skip to content

Commit

Permalink
feat: implement pure http server on listen method
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 3, 2018
1 parent 5873ef4 commit a068884
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
48 changes: 45 additions & 3 deletions src/alice.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as http from 'http';
import { IImagesApiConfig, IImagesApi, ImagesApi } from './imagesApi';
import { Middleware, IMiddlewareResult } from './middleware/middleware';
import { IApiRequest } from './api/request';
Expand All @@ -18,11 +19,13 @@ export class Alice implements IAlice {
private readonly _config: IAliceConfig;
private readonly _middlewares: Middleware[];
private readonly _imagesApi: IImagesApi;
private _server: object | null;

constructor(config: IAliceConfig) {
this._config = config;
this._middlewares = [];
this._imagesApi = new ImagesApi(this._config);
this._server = null;
}

private _buildContext(request: IApiRequest): IContext {
Expand All @@ -35,16 +38,15 @@ export class Alice implements IAlice {
context: IContext,
): Promise<IMiddlewareResult | null> {
const middlewares = Array.from(this._middlewares);
if (middlewares.length <= 0) {
if (middlewares.length === 0) {
return null;
}

let index = middlewares.length - 1;
const next = async (
context: IContext,
): Promise<IMiddlewareResult | null> => {
const middleware = middlewares[index];
index--;
const middleware = middlewares[index--];
return middleware(context, index <= 0 ? null : next);
};
return next(context);
Expand Down Expand Up @@ -81,6 +83,46 @@ export class Alice implements IAlice {
};
}

public listen(
port: number = 80,
webhookUrl: string = '/',
callback?: () => void,
) {
this._server = http
.createServer(
async (request: http.ServerRequest, response: http.ServerResponse) => {
const body: (string | Buffer)[] = [];
request
.on('data', chunk => {
body.push(chunk);
})
.on('end', async () => {
const requestData = Buffer.from(body).toString();
if (request.method === 'POST' && request.url === webhookUrl) {
try {
const requestBody = JSON.parse(requestData);
const responseBody = await this.handleRequest(requestBody);
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify(responseBody));
} catch (error) {
throw new Error(error);
}
} else {
response.statusCode = 400;
return response.end();
}
});
},
)
.listen(port, () => {
if (typeof callback === 'function') {
return callback();
}
});
return this._server;
}

public use(middleware: Middleware): void {
this._middlewares.push(middleware);
}
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export type Middleware<
> = (
context: TContextFrom,
next: MiddlewareNext<TContextTo> | null,
) => IMiddlewareResult | null | Promise<IMiddlewareResult | null>;
) => IMiddlewareResult | Promise<IMiddlewareResult | null>;

0 comments on commit a068884

Please sign in to comment.