Skip to content

Instantly share code, notes, and snippets.

@gouthambs
Last active February 14, 2018 22:52
Show Gist options
  • Save gouthambs/c0effc21d5ac37bb2317d8a4c56f4a1b to your computer and use it in GitHub Desktop.
Save gouthambs/c0effc21d5ac37bb2317d8a4c56f4a1b to your computer and use it in GitHub Desktop.
Serverless Blog example
from flask import Flask, render_template
from flask_login import LoginManager
from flask_blogging import BloggingEngine
from flask_blogging.dynamodbstorage import DynamoDBStorage
from routes import setup_auth
app = Flask(__name__)
app.config.from_object("config")
# extensions
# dyn_storage = DynamoDBStorage(endpoint_url='http://localhost:8000') # local testing
dyn_storage = DynamoDBStorage(region_name='us-east-1')
blog_engine = BloggingEngine(app, dyn_storage)
login_manager = LoginManager(app)
@app.route("/")
def index():
return render_template("index.html")
setup_auth(app, login_manager, blog_engine)
if __name__ == "__main__":
app.run(debug=True, port=8001, use_reloader=True, )
SECRET_KEY = "secret" # for WTF-forms and login
BLOGGING_URL_PREFIX = "/blog"
BLOGGING_DISQUS_SITENAME = "test"
BLOGGING_SITEURL = ""
BLOGGING_SITENAME = "My Site"
BLOGGING_ALLOW_FILEUPLOAD = False
<!-- In templates/index.html -->
<!DOCTYPE html>
<html>
<head> </head>
<body>
{% if current_user.is_authenticated %}
<a href="/logout/"> Logout </a>
{% else %}
<a href="/login/"> Login </a>
{% endif %}
&nbsp&nbsp<a href="/blog/"> Blog </a>
&nbsp&nbsp<a href="/blog/sitemap.xml">Sitemap</a>
&nbsp&nbsp<a href="/blog/feeds/all.atom.xml">ATOM</a>
</body>
</html>
The MIT License (MIT)
Copyright (c) 2018 Gouthaman Balaraman
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
from flask import redirect, render_template
from flask_login import UserMixin, login_user, logout_user
class User(UserMixin):
def __init__(self, user_id):
self.id = user_id
def get_name(self):
return "Paul Dirac"
def setup_auth(app, login_manager, blog_engine):
@login_manager.user_loader
@blog_engine.user_loader
def load_user(user_id):
return User(user_id)
@app.route("/login/")
def login():
login_user(User("pdirac"))
return redirect("/blog")
@app.route("/logout/")
def logout():
logout_user()
return redirect("/")
return app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment