Skip to content

Instantly share code, notes, and snippets.

@kimmobrunfeldt
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimmobrunfeldt/9567086 to your computer and use it in GitHub Desktop.
Save kimmobrunfeldt/9567086 to your computer and use it in GitHub Desktop.
Simple blog REST API with Eve http://python-eve.org/. Install Mongo and Eve and you're good to go!
"""
1. Install Mongo
2. pip install eve
3. python blog-backend-api.py
"""
# Mongo setup
MONGO_PORT = 27017
MONGO_USERNAME = 'test-blog'
MONGO_PASSWORD = 'test-blog'
MONGO_DBNAME = 'test-blog'
SERVER_NAME = '127.0.0.1:5000'
# Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint).
RESOURCE_METHODS = ['GET', 'POST']
# Enable reads (GET), edits (PATCH) and deletes of individual items
# (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'DELETE']
# We enable standard client cache directives for all resources exposed by the
# API. We can always override these global settings later.
CACHE_CONTROL = 'max-age=20'
CACHE_EXPIRES = 20
# Define blog data structure
# Schema definition, based on Cerberus grammar. Check the Cerberus project
# (https://github.com/nicolaiarocci/cerberus) for details.
users = {
'schema': {
'name': {
'type': 'string',
'minlength': 1,
'maxlength': 50,
},
'username': {
'type': 'string',
'minlength': 1,
'maxlength': 20,
'unique': True
}
}
}
blog_posts = {
'schema': {
'title': {
'type': 'string',
'minlength': 1,
'maxlength': 50,
},
'text': {
'type': 'string'
},
'author': {
'type': 'objectid',
'data_relation': {
'resource': 'users'
}
}
}
}
comments = {
'schema': {
'text': {
'type': 'string',
'minlength': 1,
'maxlength': 140,
},
'author': {
'type': 'objectid',
'data_relation': {
'resource': 'users'
}
},
'target_post': {
'type': 'objectid',
'data_relation': {
'resource': 'blog_posts'
}
}
}
}
# The DOMAIN dict explains which resources will be available and how they will
# be accessible to the API consumer.
DOMAIN = {
'users': users,
'blog_posts': blog_posts,
'comments': comments
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment