Back-end development. Outline. Example API: Chatter. 1. Design an example API for Chatter. Tiberiu Vilcu. https://education.github.

Similar documents
Back-end architecture

Building a Django Twilio Programmable Chat Application

EPHP a tool for learning the basics of PHP development. Nick Whitelegg School of Media Arts and Technology Southampton Solent University

Contents in Detail. Foreword by Xavier Noria

DJOAuth2 Documentation

Automation with Meraki Provisioning API

MIT AITI Python Software Development Lab DJ1:

Tutorial 4 Data Persistence in Java

Bitnami MEAN for Huawei Enterprise Cloud

At Course Completion Prepares you as per certification requirements for AWS Developer Associate.

Bitnami JRuby for Huawei Enterprise Cloud

IEMS 5722 Mobile Network Programming and Distributed Server Architecture Semester 2

Lab 5: Working with REST APIs

Graphene Documentation

Flask Web Development Course Catalog

Security. CSC309 TA: Sukwon Oh

Cloud platforms T Mobile Systems Programming

From the Beginning: Your First Node.js Web Service

Lab 2 Third Party API Integration, Cloud Deployment & Benchmarking

Bitnami OSQA for Huawei Enterprise Cloud

Building Facebook Application using Python

Handel-CodePipeline Documentation

How to go serverless with AWS Lambda

This document (including, without limitation, any product roadmap or statement of direction data) illustrates the planned testing, release and

Gunnery Documentation

GitHub-Flask Documentation

Microservices without the Servers: AWS Lambda in Action

LAB EXERCISE: RedHat OpenShift with Contrail 5.0

Homework #7 Google Cloud Platform

DreamFactory Security Guide

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

BaasBox. Open Source Backend as a Service. Otto Hylli

Bitnami HHVM for Huawei Enterprise Cloud

Ansible Tower Quick Setup Guide

Volante NACHA ISO20022 Validator AMI User Guide

CitusDB Documentation

AWS Mobile Hub. Build, Test, and Monitor Your Mobile Apps. Daniel Geske, Solutions Architect 31 May 2017

How To Start Mysql Use Linux Command Line Client In Xampp

Lab 8: Building Microservices on Oracle Autonomous Transaction Processing Service

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

BriCS. University of Bristol Cloud Service Simulation Runner. User & Developer Guide. 1 October John Cartlidge & M.

Administrator Guide Administrator Guide

Persistence & State. SWE 432, Fall 2016 Design and Implementation of Software for the Web

Bitnami Ruby for Huawei Enterprise Cloud

Tablo Documentation. Release Conservation Biology Institute

A Hands on Introduction to Docker

PROCE55 Mobile: Web API App. Web API.

Jackalope Documentation

Bitnami MariaDB for Huawei Enterprise Cloud

Cloud platforms. T Mobile Systems Programming

CS 155 Project 2. Overview & Part A

Developing Android applications in Windows

CS193X: Web Programming Fundamentals

Full Stack Web Developer Nanodegree Syllabus

CIS192 Python Programming

Privacy and Security in Online Social Networks Department of Computer Science and Engineering Indian Institute of Technology, Madras

Seeder Documentation. Release 0.1. Visgean Skeloru

This assignment is about using PHP together with SQL to create, modify, and query information in a database.

Simplified CICD with Jenkins and Git on the ZeroStack Platform

Bitnami Node.js for Huawei Enterprise Cloud

Creating a Multi-Container Pod

WhoWhenWhere. Project: WhoWhenWhere Date:

ApsaraDB for RDS. Quick Start (PostgreSQL)

WEB SECURITY WORKSHOP TEXSAW Presented by Solomon Boyd and Jiayang Wang

Securing ArcGIS for Server. David Cordes, Raj Padmanabhan

AWS Lambda + nodejs Hands-On Training

Cloud-Native Applications. Copyright 2017 Pivotal Software, Inc. All rights Reserved. Version 1.0

Beyond the Admin App: FileWave Extensibility with the RESTful API

Bitnami Re:dash for Huawei Enterprise Cloud

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Hybrid Cloud Automation using Cisco CloudCenter API

Information Security Policy

Trunk Player Documentation

d. Delete data e. Transactions Realtime Features a. Value Listener b. Child Listener c. Remove Listeners

Attacks Against Websites. Tom Chothia Computer Security, Lecture 11

Metasploit. Installation Guide Release 4.4

AWS Lambda. 1.1 What is AWS Lambda?

MANAGING ANDROID DEVICES: VMWARE WORKSPACE ONE OPERATIONAL TUTORIAL VMware Workspace ONE

Zombie Apocalypse Workshop

Bitnami MySQL for Huawei Enterprise Cloud

Full Stack boot camp

Application vulnerabilities and defences

Index. Bessel function, 51 Big data, 1. Cloud-based version-control system, 226 Containerization, 30 application, 32 virtualize processes, 30 31

IBM Image-Analysis Node.js

MassMobile Deployment Guide

Healthcare Database Connector

Alarm Counter. A Ceilometer OpenStack Application

Problem: Currently, Procore collects terabytes of user data & analytics. However, none of that important information is visible to the client.

Docker at Lyft Speeding up development Matthew #dockercon

AWS Remote Access VPC Bundle

Using and Developing with Azure. Joshua Drew

A Glance Over the Serverless Framework

Attacks Against Websites 3 The OWASP Top 10. Tom Chothia Computer Security, Lecture 14

Json Parse Error - No Json Object Could Be Decoded Django Rest Framework

AWS Tools for Microsoft Visual Studio Team Services: User Guide

CS371m - Mobile Computing. Persistence - Web Based Storage CHECK OUT g/sync-adapters/index.

IERG 4210 Tutorial 07. Securing web page (I): login page and admin user authentication Shizhan Zhu

Lab 4: create a Facebook Messenger bot and connect it to the Watson Conversation service

Serverless Architecture Hochskalierbare Anwendungen ohne Server. Sascha Möllering, Solutions Architect

Standard 1 The student will author web pages using the HyperText Markup Language (HTML)

Transcription:

Back-end development Tiberiu Vilcu Prepared for EECS 411 Sugih Jamin 13 September 2017 https://education.github.com/pack 1 2 Outline 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API 3. Set up a database to store our data 4. Create web API endpoints to connect from anywhere 5. Test endpoints to verify and check for errors Example API: Chatter /registeruser/ < username, name, email > {} 200 OK /addchatt/ < username, message > {} 200 OK /getchatts/ < Nothing > list of chatts 200 OK 6. Discuss Firebase and Heroku as alternatives 3 4

DigitalOcean Is a simple cloud computing service Deploys Droplets to run machines Has various easy-to-install applications (Django, MongoDB, Node.js) Is not free but gives $50 credit with GitHub developer pack https://www.digitalocean.com Why DigitalOcean? Simple to code, in Python Nothing to install locally Little work necessary for bare minimum product Can also create dynamic webpages Most flexible back-end option, can serve all platforms Custom API generally easiest to use among team 5 6 Outline 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API 3. Set up a database to store our data 4. Create web API endpoints to connect from anywhere 5. Test endpoints to verify and check for errors Creating a Droplet Use these options: Django 1.8.7 on 16.04 $5 per month size (free with student developer pack) New York datacenter 1 or 3 https://cloud.digitalocean.com/droplets 6. Discuss Firebase and Heroku as alternatives 7 8

Accessing the Droplet How the server works 1. IP address, username and password are emailed to you 2. ssh root@[ip] 3. Change default password upon login 4. Can access IP address s webpage in browser https://www.digitalocean.com/community/tutorials/how-to-use-thedjango-one-click-install-image-for-ubuntu-16-04 nginx: web server that listens on port 80 configuration: /etc/nginx/sites-enabled/django gunicorn: serves Django project on port 9000 configuration: /etc/init/gunicorn.conf django: Python code framework that is run to do the work location: /home/django/django_project 9 10 Outline Setting up the database 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API 3.Set up a database to store our data 4. Create web API endpoints to connect from anywhere 5. Test endpoints to verify and check for errors 6. Discuss Firebase and Heroku as alternatives 1. sudo -u postgres psql (use PSQL as user postgres) 2. \connect django (connect to the database) 3. GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO django; 4. \dt (list tables) 5. Use SQL commands from here to do what you need 6. Control+D to exit https://www.w3schools.com/sql/ 11 12

Creating tables CREATE TABLE users (username varchar(255), name varchar(255), email varchar(255)); CREATE TABLE chatts (username varchar(255), message varchar(255), time timestamp DEFAULT CURRENT_TIMESTAMP); Adding a dummy entry INSERT INTO chatts values('testuser1', 'Hello world'); SELECT * from chatts; 13 14 Outline Setting up Django 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API python manage.py startapp chatter Creates a directory chatter and the necessary files 3. Set up a database to store our data 4. Create web API endpoints to connect from anywhere 5. Test endpoints to verify and check for errors 6. Discuss Firebase and Heroku as alternatives 15 16

Adding a view Updating the URLs In chatter/views.py In django_project/urls.py from django.http import JsonResponse, HttpResponse def getchatts(request): if request.method!= 'GET': return HttpResponse(status=404) response = {} response['chatts'] = ['Hello', 'World'] return JsonResponse(response) from chatter import views urlpatterns = [ url(r'^getchatts/$', views.getchatts, name='getchatts'), url(r'^admin/', include(admin.site.urls)), ] 17 18 Running the application Creating the rest of the views service gunicorn restart restart the application completely getchatts Query the database and return all found chatts python manage.py runserver localhost:9000 let it restart every time a change is detected not good for production registeruser Parse JSON parameters and insert a user into the database addchatt Parse JSON parameters and insert a chatt into the database 19 20

Connecting to the database In django_project/settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'django', 'USER': 'django', 'PASSWORD': [given password], 'HOST': 'localhost', 'PORT': '', } } Using database cursors from django.db import connection cursor = connection.cursor() cursor.execute('select * FROM chatts;') rows = cursor.fetchall() 21 22 Parsing URL parameters http://website.com/path/?key=val&key2=val are parameters Do not need to specify them in the URLs file Parsing JSON parameters import json json_data = json.loads(request.body) username = json_data[ username'] numentries = request.get.get('numentries', None) Retrieves the argument with key numentries, or None if does not exist 23 24

Preventing CSRF errors Django wants CSRF (cross-site request forgery) cookies by default from django.views.decorators.csrf import csrf_exempt @csrf_exempt def addchatt(request): pass Finishing addchat @csrf_exempt def addchatt(request): if request.method!= 'POST': return HttpResponse(status=404) json_data = json.loads(request.body) username = json_data['username'] message = json_data['message'] cursor = connection.cursor() cursor.execute('insert INTO chatts (username, message) VALUES ' '(%s, %s);', (username, message)) return JsonResponse({}) 25 26 Adding adduser Finishing the URLs @csrf_exempt def adduser(request): if request.method!= 'POST': return HttpResponse(status=404) json_data = json.loads(request.body) username = json_data['username'] name = json_data['name'] email = json_data['email'] cursor = connection.cursor() cursor.execute('insert INTO users (username, name, email) VALUES ' '(%s, %s, %s);', (username, name, email)) return JsonResponse({}) urlpatterns = [ url(r'^getchatts/$', views.getchatts, name='getchatts'), url(r'^addchatt/$', views.addchatt, name='addchatt'), url(r'^adduser/$', views.adduser, name='adduser'), url(r'^admin/', include(admin.site.urls)), ] 27 28

Outline 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API 3. Set up a database to store our data 4. Create web API endpoints to connect from anywhere Testing the API Can test GET requests through the browser URL Use Postman to test POST and PUT (and other) requests Free app / Chrome extension to create custom requests https://www.getpostman.com 5.Test endpoints to verify and check for errors 6. Discuss Firebase and Heroku as alternatives 29 30 Adding JSON to request body 1. Set request type to POST 2. In Body select raw form 3. Change type to application/json 4. Type in valid JSON { } "username": "tv", "message": "Hello world! Live demo right here!" Takeaways Now have working API server Mobile app can interact with GET/POST requests Easy to extend database and endpoints Easy to add functionality / fix correctness 31 32

Stability improvements Don t want 500 INTERNAL SERVER ERROR Validate URL/JSON parameters and values Validate entries in database before/after adding Return correct error codes for caught exceptions Security improvements Remove the CSRF token workaround Prevent SQL injection Don t allow any user to POST on behalf of anyone else If using passwords: salt and hash Never write your own crypto/hashing code, use Python libs 33 34 Other improvements Document API for team! Verify user/item exists before posting Add option to limit number of chatts to return Integrate with other API s (perhaps Facebook for user accounts) Server scripts for database management Outline 1. Design an example API for Chatter 2. Create a DigitalOcean Droplet to serve the API 3. Set up a database to store our data 4. Create web API endpoints to connect from anywhere 5. Test endpoints to verify and check for errors 6. Discuss Firebase and Heroku as alternatives 35 36

Firebase as an alternative https://firebase.google.com Realtime database (NoSQL, use JSON) User authentication Cloud storage and functions Support for ios, Android, web apps Using Firebase on mobile apps 1. Register app through bundle ID or package name 2. Download configuration files from Firebase 3. Add Firebase SDK to project 4. Use their referenced API to interact with services Online account management (also through Node.js) Limits: don t have console control, stuck with their API 37 38 Firebase database Firebase database JSON JSON formatted data (not structured in tables) Allows rules for authentications and permissions Automatically backs up (with paid plan) Can select and insert items by keys and sub-keys { } "users": [{ "username": "testuser1", "email": "test1@user.com", "password": "fakepass" }, { "username": "testuser2", "email": "test2@user.com", "password": "fakepass" } ], "chatts": [{ "username": "testuser1", "message:": "This is my first chatt", "timestamp": "right now" }, { "username": "testuser2", "message:": "This is also my first chatt", "timestamp": "a little while ago" } ] 39 40

Heroku as an alternative https://www.heroku.com/home Myriad of products: Platform: managed containers with runtime environments and ecosystems Postgres: SQL database service Redis: key-value data store Kafka: distributed data streams (not very relevant) Heroku as an alternative Things to note: Offers analogous features to Firebase and DigitalOcean Harder to get started with for the less experienced Confusing to know which product works best Requires local development setup (deploy from git) 41 42 Using Heroku on mobile apps Has Sinatra API for ios but requires attaching SDK Can create a REST API server on their stack with Node.js and MongoDB: Set up app environment AWS alternatives AWS Lambda: run your own serverless code on AWS AWS EC2: cloud computing service Various pricing tiers per service; free or paid with GitHub credit Provision a database and connect to the server Create API endpoints with Node.js and Express Create client-side contract to interact with API 43 44

Resources Project sample repo: https://github.com/um-eecs-441/django-project-sample-f17 GitHub student pack: https://education.github.com/pack/ DigitalOcean: https://www.digitalocean.com Django setup: https://www.digitalocean.com/community/tutorials/how-to-use-the-django-one-click-install-image-for-ubuntu-16-04 SQL W3 tutorials: https://www.w3schools.com/sql/ Postman: https://www.getpostman.com Firebase: https://firebase.google.com Firebase API reference: https://firebase.google.com/docs/reference/ Heroku: https://www.heroku.com/home Heroku ios tutorial: https://devcenter.heroku.com/articles/getting-started-ios-development-sinatra-cedar Heroku REST API tutorial: https://devcenter.heroku.com/articles/mean-apps-restful-api AWS Lambda: https://aws.amazon.com/lambda/ AWS EC2: https://aws.amazon.com/ec2/ 45