Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //----------------------------------------
- //handler.js
- //----------------------------------------
- import mysql from 'mysql2/promise';
- import db_config from './db_config.json';
- import { success, failure } from './libs/response-lib';
- export const main = (event, context, callback) => {
- console.log('start');
- const pool = mysql.createPool({
- host : db_config.dbendpoint,
- user : db_config.dbuser,
- password : db_config.dbpwd,
- database : db_config.dbname
- });
- const locationid = event.locationid;
- const parameter = event.parameter;
- //proof both event.* and db_config.* are working
- console.log(parameter + " and " + db_config.dbport);
- //prevent timeout from waiting event loop
- context.callbackWaitsForEmptyEventLoop = false;
- const resPromise = pool.getConnection()
- .then((connection) => {
- console.log("got connection");
- // Use the connection
- const res = connection.execute(
- 'SELECT * FROM radar_pubweb r WHERE r.locationid = ? AND r.parameter = ?',
- [locationid, parameter]);
- // And done with the connection.
- connection.release();
- return res;
- }).then((result) => {
- console.log(result);
- return result;
- }).catch((err) => {
- // And done with the connection.
- console.log('error : ', err);
- throw err;
- })
- console.log("end");
- return resPromise;
- }
- //----------------------------------------
- //response-lib.js
- //----------------------------------------
- function buildResponse(statusCode, body) {
- return {
- statusCode,
- headers: {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Credentials': true,
- },
- body: JSON.stringify(body),
- };
- }
- export function internalError(err) {
- return buildResponse(500, { message: err.message, type: 'internalError' });
- }
- export function success(body) {
- return buildResponse(200, body);
- }
- export function failure(body) {
- return buildResponse(500, body);
- }
- export function notFound(obj) {
- return buildResponse(404, { message: `The object ${obj} is not found`, type: 'notFound' });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement