Boilerplate serverless API project using serverless-openapi-joi plugin
- Auto-generated OpenAPI v3 docs from code
- Joi validated requests with nice errors
- Swagger UI to view OpenAPI docs, hosted on S3 as a static site (example)
- Example CRUD API
- Local development powered by serverless-offline
- Knex migrations & query builder on PostgreSQL
- Tests with jest
- Travis CI configuration for serverless deployment
- Typescript configuration with sensible defaults
- Prettier + Tslint
Requirements:
- Node.js v8+
- Local PostgreSQL (docker-compose file included)
source .env.sample# Set up environment variables
npm install
docker-compose up --detach # PostgreSQL listening at port 5432
npm run migrate # Set up database schema with knex migrations
npm run dev # Serverless offline: http://localhost:9000, Swagger UI: http://localhost:9001
serverless.yml
– serverless config file, defines functions, endpoints and cloudformation resourcessrc/**
– typescript source filessrc/handler.ts
– serverless main entrypoint, this is where all http requests and function invocations start fromsrc/routes.ts
– where api routes and validations are definedsrc/handler/**
– handlers for when routing is finished, this is where control logic happenssrc/core/**
– core business logic + reading & writing to database happens heresrc/util/**
– boring utilities and helpers are stored heresrc/types/**
– type definitions (.d.ts files)src/migrations/**
– knex database migration files (useknex migrate:make
to create these)src/seeds/**
– knex database seed files (useknex seeds:make
to create these)
dist/**
– transpiled javascript from source filesstatic/**
– generated static Swagger UI docsscripts/**
– reusable scripts like for building static Swagger UI docs__tests__/**
– jest tests
Make sure your AWS IAM access is set-up with sufficient privileges and access keys are set.
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
Full deploy with Serverless. (This will take some time on the first time)
npm run build # build project
serverless deploy --stage dev
In order to save time, you can also just deploy a single function and skip Cloudformation after first deploy is finished
serverless deploy function -f <function name> --stage dev