Lamp and Rest
Lamp and Rest
Development
Amy Krause
Outline
What is LAMP?
What is REST?
Introduction to REST
An example REST application
Building the LAMP stack
2 2
What is LAMP
Originally
LAMP = Linux Apache MySQL PHP
Now refers to a software stack model for building dynamic
websites and web applications
Components can vary
Now
Web Server + Database + Scripting Language
3
LAMP in a picture
Web server
Client CGI
Database
4
LAMP software stacks
Database:
SQL: MySQL/PostgreSQL/Oracle/SQLServer/SQLite/
NoSQL: MongoDB/
Cassandra/HBase/
5
Linux
Choose a database
SQL or NoSQL? Vendor?
6
What is REST
Common architecture
Stateless
Resource representation
State transitions
7
REST
8
A RESTful example
9
A RESTful example
10
Example of a client interaction
Method Activity
1 GET List all tasks
2 POST Create a new task
3 GET Check the task was created
4 GET <id> Get the task info
5 PUT <id> Update the task
6 GET <id> Check the task was updated
7 DELETE <id> Remove the task
8 GET Check the task was removed
11
Example: Implementation in Python
12
Hello World!
@app.route(/)
def hello():
return Hello World!
13
RESTful Python using flask
GET
@app.route(/<task_id>)
def get_task(task_id):
return tasks[task_id]
POST
@app.route(/, methods=[POST])
def add_task():
task_id = uuid.uuid4()
tasks[task_id] = request.form
14
RESTful Python using flask
@app.route(/<task_id>,
methods=[GET,
PUT,
DELETE])
def task_resource(task_id):
task = tasks[task_id]
if request.method == PUT:
task = json.loads(request.data)
tasks[task_id] = task
return task
15
Deploying to a web server
$ python hello.py
* Running on http://127.0.0.1:5000/
16
Client example
17
Another client example
curl X POST
d {name:
Write test for new feature}
http://localhost:5000/
18
Database
19
Adding a database: Requirements
20
Database example: MongoDB
Choose the database solution for your use case and the
queries you will run
NoSQL or SQL, distributed or single server,
21
MongoDB clients
import pymongo
client = MongoClient()
tasks = client.db.collection
22
Database example: MongoDB
Add a task:
tasks.insert_one(task)
Retrieve a task:
tasks.find_one({name: task_id})
23
Our example in a picture
Browser Flask
curl
REST client
MongoDB
24
Conclusion
25