1/* 2 * Copyright 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import express = require('express'); 18import bodyParser = require('body-parser'); 19import { log, profile } from './logger'; 20import { PORT } from './flags'; 21import { handleRequest as handleLicenseRequest } from './license'; 22 23/** 24 * The HTTP request handler. 25 */ 26class RequestHandler { 27 @profile 28 defaultHandler(request: express.Request, response: express.Response) { 29 response.status(200).send('Server is up.'); 30 } 31 32 @profile 33 async licenseRequestHandler(request: express.Request, response: express.Response) { 34 return handleLicenseRequest(request, response); 35 } 36} 37 38// Bootstrap application. 39 40const app = express(); 41// define the standard body parsers 42app.use(bodyParser.urlencoded({ extended: true })); 43app.use(bodyParser.json()); 44 45const requestHandler = new RequestHandler(); 46 47app.get('/', requestHandler.defaultHandler); 48app.post('/', requestHandler.defaultHandler); 49app.post('/convert/licenses', requestHandler.licenseRequestHandler); 50 51app.listen(PORT); 52log(`Server started. Listening for requests. Listening on PORT ${PORT}`); 53