mirror of
https://github.com/bykof/cordova-plugin-webserver.git
synced 2026-04-20 00:02:45 +08:00
Introducing the AppServer -> an abstraction of the webserver
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
import UniversalRouter from 'universal-router';
|
||||
import Request from "./Request";
|
||||
import Response from "./Response";
|
||||
|
||||
|
||||
export default class AppServer {
|
||||
constructor(port) {
|
||||
this.port = port;
|
||||
// Why? because SOLID of this class
|
||||
this.webserver = webserver;
|
||||
this.routes = [];
|
||||
|
||||
this.initWebserver();
|
||||
this.initRouter();
|
||||
}
|
||||
|
||||
initWebserver() {
|
||||
this.webserver.onRequest(this.onRequest);
|
||||
}
|
||||
|
||||
initRouter() {
|
||||
this.router = new UniversalRouter(this.routes);
|
||||
}
|
||||
|
||||
addRoute(path, callback) {
|
||||
this.routes.push(
|
||||
{
|
||||
path: path,
|
||||
action: () => callback
|
||||
}
|
||||
);
|
||||
this.initRouter();
|
||||
}
|
||||
|
||||
onRequest(request) {
|
||||
let request = new Request(
|
||||
request.requestId,
|
||||
request.method,
|
||||
request.path,
|
||||
request.query,
|
||||
request.body,
|
||||
request.headers
|
||||
);
|
||||
|
||||
let response = new Response(
|
||||
this.webserver,
|
||||
request.requestId
|
||||
);
|
||||
|
||||
this.router.resolve(
|
||||
request.url
|
||||
).then(
|
||||
// callback is a function
|
||||
(callback) => {
|
||||
// run the callback with all information we got for the request and the response
|
||||
callback(request, response);
|
||||
}
|
||||
).catch(
|
||||
(error) => {
|
||||
// if there is an error, just send a not found 404 bljad
|
||||
response.notFound();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
start() {
|
||||
this.webserver.start();
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.webserver.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
|
||||
|
||||
export default class Request {
|
||||
constructor(
|
||||
requestId,
|
||||
method,
|
||||
body,
|
||||
headers,
|
||||
path,
|
||||
query
|
||||
) {
|
||||
this.requestId = requestId;
|
||||
this.method = method;
|
||||
this.path = path;
|
||||
this.query = query;
|
||||
this.body = body;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
get json() {
|
||||
return JSON.parse(this.body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
|
||||
export default class Response {
|
||||
constructor(
|
||||
webserver,
|
||||
requestId,
|
||||
status=200,
|
||||
body='',
|
||||
headers={
|
||||
'Content-Type': 'text/plain'
|
||||
}
|
||||
) {
|
||||
this.webserver = webserver;
|
||||
this.requestId = requestId;
|
||||
this.status = status;
|
||||
this.body = body;
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
send() {
|
||||
this.webserver.sendResponse(
|
||||
{
|
||||
status: this.status,
|
||||
body: this.body,
|
||||
headers: this.headers
|
||||
}
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
status(status) {
|
||||
this.status(status);
|
||||
return this;
|
||||
}
|
||||
|
||||
notFound(){
|
||||
return this.status(404).send();
|
||||
}
|
||||
|
||||
methodNotAllowed() {
|
||||
return this.status(405).send();
|
||||
}
|
||||
|
||||
ok() {
|
||||
return this.status(200).send();
|
||||
}
|
||||
|
||||
json(data) {
|
||||
this.body = JSON.stringify(data);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user