updated wiki view and config

This commit is contained in:
Matthew Scragg 2013-12-09 14:24:22 -06:00
parent a9158f3d30
commit 99ce4acd00
5 changed files with 57 additions and 68 deletions

14
app.py
View file

@ -1,11 +1,13 @@
from gevent import monkey, pywsgi from gevent import monkey, wsgi
from realms import config, app from realms import config, app, manager
monkey.patch_all() monkey.patch_all()
import logging
@manager.command
def server(port=10000):
print "Server started (%s)" % config.ENV
wsgi.WSGIServer(('', int(port)), app).serve_forever()
if __name__ == '__main__': if __name__ == '__main__':
print "Starting server" manager.run()
app.logger.setLevel(logging.INFO)
pywsgi.WSGIServer(('', config.PORT), app).serve_forever()

View file

@ -3,7 +3,7 @@ import time
import sys import sys
import os import os
from flask import Flask, request, render_template, url_for, redirect, session from flask import Flask, request, render_template, url_for, redirect, session, g
from flask.ctx import _AppCtxGlobals from flask.ctx import _AppCtxGlobals
from flask.ext.script import Manager from flask.ext.script import Manager
from flask.ext.login import LoginManager, login_required from flask.ext.login import LoginManager, login_required
@ -17,27 +17,40 @@ from realms.lib.session import RedisSessionInterface
from realms.lib.wiki import Wiki from realms.lib.wiki import Wiki
from realms.lib.util import to_canonical, remove_ext, mkdir_safe, gravatar_url from realms.lib.util import to_canonical, remove_ext, mkdir_safe, gravatar_url
from realms.lib.services import db from realms.lib.services import db
from models import Site, User, CurrentUser from realms.models import User, CurrentUser
wikis = {} sites = {}
class Site(object):
wiki = None
class AppCtxGlobals(_AppCtxGlobals): class AppCtxGlobals(_AppCtxGlobals):
@cached_property @cached_property
def current_wiki(self): def current_site(self):
subdomain = format_subdomain(self.current_site) subdomain = format_subdomain(self.current_subdomain)
if not subdomain: if not subdomain:
subdomain = "_" subdomain = "www"
if not wikis.get(subdomain): if subdomain is "www" and self.current_subdomain:
wikis[subdomain] = Wiki("%s/%s" % (config.REPO_DIR, subdomain)) # Invalid sub domain
return False
return wikis[subdomain] if not sites.get(subdomain):
sites[subdomain] = Site()
sites[subdomain].wiki = Wiki("%s/%s" % (config.REPO_DIR, subdomain))
return sites[subdomain]
@cached_property @cached_property
def current_site(self): def current_wiki(self):
return g.current_site.wiki
@cached_property
def current_subdomain(self):
host = request.host.split(':')[0] host = request.host.split(':')[0]
return host[:-len(config.DOMAIN)].rstrip('.') return host[:-len(config.DOMAIN)].rstrip('.')
@ -103,13 +116,6 @@ class Application(Flask):
print >> sys.stderr, ' * Ready in %.2fms' % (1000.0 * (time.time() - start_time)) print >> sys.stderr, ' * Ready in %.2fms' % (1000.0 * (time.time() - start_time))
def init_db(dbname):
"""
Assures DB has minimal setup
"""
pass
class RegexConverter(BaseConverter): class RegexConverter(BaseConverter):
""" """
Enables Regex matching on endpoints Enables Regex matching on endpoints
@ -188,6 +194,12 @@ else:
assets.register('js_editor', js) assets.register('js_editor', js)
@app.before_request
def check_subdomain():
if not g.current_site:
return redirect('http://%s' % config.SERVER_NAME)
@app.after_request @app.after_request
def inject_x_rate_headers(response): def inject_x_rate_headers(response):
limit = get_view_rate_limit() limit = get_view_rate_limit()
@ -220,7 +232,6 @@ def root():
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(config.ROOT_ENDPOINT))
@app.route("/_account/") @app.route("/_account/")
@login_required @login_required
def account(): def account():

View file

@ -4,7 +4,6 @@ HOSTNAME = socket.gethostname()
DOMAIN = 'realms.dev' DOMAIN = 'realms.dev'
ENV = 'DEV' ENV = 'DEV'
PORT = 10000
DB_URI = 'postgresql://realms:dbpassword@localhost:5432/realms' DB_URI = 'postgresql://realms:dbpassword@localhost:5432/realms'
@ -32,7 +31,10 @@ MODULES = [
] ]
if ENV is 'PROD': if ENV is 'PROD':
pass SERVER_NAME = 'realms.io'
PORT = 80
else: else:
DEBUG = True DEBUG = True
ASSETS_DEBUG = True ASSETS_DEBUG = True
SERVER_NAME = 'realms.dev:8000'
PORT = 8000

View file

@ -1,14 +1,14 @@
from flask import g, render_template, request, redirect, Blueprint, flash, url_for from flask import g, render_template, request, redirect, Blueprint, flash, url_for
from flask.ext.login import login_required from flask.ext.login import login_required
from realms import app, redirect_url, config from realms import redirect_url, config
from realms.lib.util import to_canonical, remove_ext from realms.lib.util import to_canonical, remove_ext
from realms.lib.wiki import Wiki from realms.lib.wiki import Wiki
from realms.models import Site from realms.models import Site
blueprint = Blueprint('wiki', __name__) blueprint = Blueprint('wiki', __name__, url_prefix='/wiki')
@blueprint.route("/wiki/new/", methods=['GET', 'POST']) @blueprint.route("/new/", methods=['GET', 'POST'])
@login_required @login_required
def new(): def new():
if request.method == 'POST': if request.method == 'POST':
@ -25,7 +25,7 @@ def new():
return render_template('wiki/new.html') return render_template('wiki/new.html')
@blueprint.route("/wiki/_commit/<sha>/<name>") @blueprint.route("/_commit/<sha>/<name>")
def commit(name, sha): def commit(name, sha):
cname = to_canonical(name) cname = to_canonical(name)
@ -36,29 +36,30 @@ def commit(name, sha):
return redirect(url_for('.create', name=cname)) return redirect(url_for('.create', name=cname))
@blueprint.route("/wiki/_compare/<name>/<regex('[^.]+'):fsha><regex('\.{2,3}'):dots><regex('.+'):lsha>") @blueprint.route("/_compare/<name>/<regex('[^.]+'):fsha><regex('\.{2,3}'):dots><regex('.+'):lsha>")
def compare(name, fsha, dots, lsha): def compare(name, fsha, dots, lsha):
diff = g.current_wiki.compare(name, fsha, lsha) diff = g.current_wiki.compare(name, fsha, lsha)
return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha) return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha)
@blueprint.route("/wiki/_revert", methods=['POST']) @blueprint.route("/_revert", methods=['POST'])
def revert(): def revert():
if request.method == 'POST': if request.method == 'POST':
name = request.form.get('name') name = request.form.get('name')
commit = request.form.get('commit') commit = request.form.get('commit')
cname = to_canonical(name) cname = to_canonical(name)
g.current_wiki.revert_page(name, commit, message="Reverting %s" % cname, username=g.current_user.get('username')) g.current_wiki.revert_page(name, commit, message="Reverting %s" % cname,
username=g.current_user.get('username'))
flash('Page reverted', 'success') flash('Page reverted', 'success')
return redirect(url_for('.page', name=cname)) return redirect(url_for('.page', name=cname))
@blueprint.route("/wiki/_history/<name>") @blueprint.route("/_history/<name>")
def history(name): def history(name):
history = g.current_wiki.get_history(name) history = g.current_wiki.get_history(name)
return render_template('wiki/history.html', name=name, history=history, wiki_home=url_for('wiki.page')) return render_template('wiki/history.html', name=name, history=history, wiki_home=url_for('wiki.page'))
@blueprint.route("/wiki/_edit/<name>", methods=['GET', 'POST']) @blueprint.route("/_edit/<name>", methods=['GET', 'POST'])
def edit(name): def edit(name):
data = g.current_wiki.get_page(name) data = g.current_wiki.get_page(name)
cname = to_canonical(name) cname = to_canonical(name)
@ -80,14 +81,14 @@ def edit(name):
return redirect(url_for('.create', name=cname)) return redirect(url_for('.create', name=cname))
@blueprint.route("/wiki/_delete/<name>", methods=['POST']) @blueprint.route("/_delete/<name>", methods=['POST'])
@login_required @login_required
def delete(name): def delete(name):
pass pass
@blueprint.route("/wiki/_create/", defaults={'name': None}, methods=['GET', 'POST']) @blueprint.route("/_create/", defaults={'name': None}, methods=['GET', 'POST'])
@blueprint.route("/wiki/_create/<name>", methods=['GET', 'POST']) @blueprint.route("/_create/<name>", methods=['GET', 'POST'])
def create(name): def create(name):
cname = "" cname = ""
if name: if name:
@ -107,8 +108,8 @@ def create(name):
return render_template('wiki/edit.html', name=cname, content="") return render_template('wiki/edit.html', name=cname, content="")
@blueprint.route("/wiki/", defaults={'name': 'home'}) @blueprint.route("/", defaults={'name': 'home'})
@blueprint.route("/wiki/<name>") @blueprint.route("/<name>")
def page(name): def page(name):
cname = to_canonical(name) cname = to_canonical(name)
if cname != name: if cname != name:

View file

@ -1,29 +1,2 @@
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[unix_http_server]
file = /tmp/supervisor.sock
chmod = 0777
[supervisorctl]
serverurl = unix:///tmp/supervisor.sock
[supervisord]
logfile = /tmp/supervisord.log
logfile_maxbytes = 50MB
logfile_backups=10
loglevel = info
pidfile = /tmp/supervisord.pid
nodaemon = false
minfds = 1024
minprocs = 200
umask = 022
user = root
identifier = supervisor
directory = /tmp
nocleanup = true
childlogdir = /tmp
strip_ansi = false
[program:realms] [program:realms]
command=/home/deploy/virtualenvs/realms/bin/python /home/deploy/realms/app.py command=/home/deploy/virtualenvs/realms/bin/python /home/deploy/realms/app.py