simple api to do common things
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Blaine Motsinger a2e7b226d7 Update deps 6 months ago
config updated examples 2 years ago
db Add quote 1 year ago
lib added quote route and table 1 year ago
scripts created script to handle quotes and jokes 1 year ago
.gitignore removed joke_types from the schema 2 years ago
LICENSE added license 2 years ago
README.md typo 1 year ago
adventurehound-api.js Add CORS to allow for getting data through JS 8 months ago
package.json Update deps 6 months ago

README.md

adventurehound-api

simple api to do common things

DESCRIPTION

This project provides a simple API, written in nodejs, to do common things (mostly for Slack bots).

SYNOPSIS

server ~/git/adventurehound-api $ nodejs adventurehound-api.js 
[06152018-233738] [info] adventurehound-api - version 0.1.0
[06152018-233738] [info] serving: 0.0.0.0:3000
[06152018-233948] 10.0.0.103 GET /joke

laptop ~ $ curl -s -X GET server:3000/joke | json_pp
{
   "id" : 1,
   "joke" : "Why did the chicken cross the road? To get to the other side!"
}

ROUTES

joke

This route provides a way to get a random joke from the application's database.

METHODS

GET

ARGUMENTS

None.

RETURNS

The joke in application/json content-type.

laptop ~ $ curl -si -X GET server:3000/joke
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 154
Date: Sun, 19 Aug 2018 15:34:45 GMT
Connection: keep-alive

{"id":1,"joke":"Why did the chicken cross the road? To get to the other side!"}

quote

This route provides a way to get a random quote from the application's database.

METHODS

GET

ARGUMENTS

None.

RETURNS

The quote in application/json content-type.

laptop ~ $ curl -si -X GET server:3000/quote
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 154
Date: Sun, 19 Aug 2018 15:34:45 GMT
Connection: keep-alive

{"id":1,"quote":"Some of them try to rhyme but they can't rhyme like this. -Kris Kross"}

LOGGING

Timestamped startup and request info are logged to stdout.

[06162018-002138] [info] adventurehound-api server started - version 0.1.0
[06162018-002138] [info] serving: 0.0.0.0:3000
[06162018-002156] 10.0.0.103 GET /joke

CONFIGURATION

application configuration settings

This project requires configuration settings which are stored and defined within the ./config/application/application.js file located within the project's base dir. The config object is exported and accessed within adventurehound-api.js.

# ./config/application/application.js
config.address     = '0.0.0.0';
config.port        = 3000;
config.api_key     = '1234567890qwerty';

The key value pairs within the configuration file are verified on startup. The server will fail to start if any of the names or values aren't correct or within range. If there is a failure, the reason will be logged.

# unexpected key name in the config
[06292018-025505] [info] adventurehound-api - version 0.1.0
[06292018-025505] [error] config verification failed
[06292018-025505] [error] config.interfaces: interfaces is not a valid key name
[06292018-025505] [error] exiting

# incorrect value
[06292018-025521] [info] adventurehound-api - version 0.1.0
[06292018-025521] [error] config verification failed
[06292018-025521] [error] config.port: "3000a" is not a valid value
[06292018-025521] [error] exiting

# api key isn't set
[08192018-171010] [info] adventurehound-api - version 0.1.0
[08192018-171010] [error] config verification failed
[08192018-171010] [error] config.api_key: "" is not a valid value
[08192018-171010] [error] exiting

config.address

The network address on the server to bind to.

Verification on startup allows for both ip addresses and hostnames.

config.port

The port on the server to listen on.

Verification on startup allows for digits only.

config.api_key

The authorization header string to validate against.

Verification on startup allows for case-insensitive alpha-numeric characters, and requires between a 16 and 40 character length string.

service configuration settings

httpd.conf and systemd service files are included within the ./config/services directory for setup in a production environment.

DATABASE

This project uses sqlite3 for it's database. A schema file is located within the db/ directory for creating the tables.

server ~/git/adventurehound-api/db $ sqlite3 adventurehound-api.db < schema.sql 

TABLES

jokes

The jokes table contains the jokes to be displayed through the joke route.

sqlite> PRAGMA table_info( jokes );
0|id|INTEGER|0||1
1|joke|TEXT|1||0

quotes

The quotes table contains the quotes to be displayed through the quote route.

sqlite> PRAGMA table_info( quotes );
0|id|INTEGER|0||1
1|quote|TEXT|1||0

DEPENDENCIES

This project requires the express, moment, path, and sqlite3 modules installed through npm. The modules are defined in the package.json file within the project, and can be installed via npm.

server ~/git/adventurehound-api $ npm install

This software is available under the MIT license.

Copyright (c) 2018 Blaine Motsinger

AUTHOR

Blaine Motsinger blaine@renderorange.com