Skip to content

grehujt/RestfulApiServer

Repository files navigation

Restful Api Server

Overview

overview

  • Nginx: the web server & RESTful API server powered by Openresty.
  • MariaDB: the database server.

Project File Structure

Deployment

  • OS: 64-bit ubuntu 14.04 / 16.04 server
  • RAM: >= 8G
  • Port: 443, 3306
sudo su
cd backend_deployment_demo
chmod +x init.sh
./init.sh
# (the init.sh will reboot the server)

RESTful API Spec

  • Read operations:
    • /{db}/{table}: get all records from a table
      • verb: GET
      • output: JSON
    • /{db}/{table}/{id}: get one record specified by {id} from a table
      • verb: GET
      • output: JSON
    • /{db}/{table}/{id},{id},..: get multi records specified by {id} from a table
      • verb: GET
      • output: JSON
  • Write operations:
    • /_bulk/{db}/{table}
      • verb:
        • DELETE: bulk delete operations
        • PUT: bulk upsert operations
      • need http basic authentication
      • input: JSON
      • output: JSON
    • /_private/{db}/{table}
      • verb:
        • POST, create a new entry
        • PUT, create a new entry if not exists, update otherwise
      • need http basic authentication
      • input: JSON
      • output: JSON
    • /_private/{db}/{table}/{id}
      • verb:
        • PATCH, update the entry
        • DELETE, delete the entry
      • need http basic authentication
      • input: JSON
      • output: JSON

Reminders

  • As the nginx serves https, a self-signed cert will be generated by init.sh, one can specify a valid cert in the docker-compose.yml as needed, then run the following commands to active the cert:
cd backend_deployment_demo
sudo docker-compose restart backenddeploymentdemo_nginx_1
  • the init.sh will create a demo db called 'lbs_demo_site' with some default tables
  • Default db root password is defined in docker-compose.yml
  • DB naming convention for security: db name prefix with 'lbs_' will exposed to user, for example:
curl -k https://localhost/demo_site/imgs
# (the 'demo_site' will map to internal db 'lbs_demo_site')
  • All write operations need http basic authentication, one can edit the userauth file to add/remove users, an easy way to generate the password would be http://www.htaccesstools.com/htpasswd-generator/.
  • One default user in the userauth: demoauthuser:20demoauthuser17

RESTful API examples

# (get all entries from lbs_demo_site.imgs)
curl -k https://localhost/demo_site/imgs

# (get one entry with id=3 from lbs_demo_site.imgs)
curl -k https://localhost/demo_site/imgs/3

# (get multi entries with ids=1,2,3,4 fromlbs_demo_site.imgs)
curl -k https://localhost/demo_site/imgs/1,2,3,4

# (insert one entry to lbs_demo_site.imgs, 'ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=' is base64 encoded version of 'demoauthuser:20demoauthuser17')
curl -kXPOST -H"Authorization: Basic ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=" https://localhost/_private/demo_site/imgs -d'{"type":"png", "imPath":"1.png"}'

# (update one entry with id=1 of lbs_demo_site.imgs)
curl -kXPATCH -H"Authorization: Basic ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=" https://localhost/_private/demo_site/imgs/1 -d'{"type":"png", "imPath":"1.1.png"}'

# (delete one enrty with id=1 of lbs_demo_site.imgs)
curl -kXDELETE -H"Authorization: Basic ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=" https://localhost/_private/demo_site/imgs/1

# (upsert multi entries with id=1,2 for lbs_demo_site.imgs)
curl -kXPUT -H"Authorization: Basic ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=" https://localhost/_bulk/demo_site/imgs -d'[{"_id":1, "type":"png", "imPath":"1.png"}, {"_id":2, "type":"png", "imPath":"2.png"}]'

# (delete multi entries with id=1,2 for lbs_demo_site.imgs)
curl -kXDELETE -H"Authorization: Basic ZGVtb2F1dGh1c2VyOjIwZGVtb2F1dGh1c2VyMTc=" https://localhost/_bulk/demo_site/imgs -d'[1, 2]'

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published