This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
/
shorts.py
51 lines (35 loc) · 1.45 KB
/
shorts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""A really simple url shortener for App Engine."""
import os
from google.appengine.ext.webapp.util import run_wsgi_app
from snakeguice import create_injector, config, modules
from snakeguice.extras import snakeweb
import interfaces
import controllers
import models
import auth
DEBUG = os.environ['SERVER_SOFTWARE'].startswith('Dev')
class ShortsModule(modules.Module):
def configure(self, binder):
binder.bind(interfaces.AuthService, to=auth.MeAuthService)
binder.bind(interfaces.LinkShorteningService,
to=models.LinkShorteningService)
if DEBUG:
config_loader = config.ConfigParserLoader('app_dev.cfg')
else:
config_loader = config.ConfigParserLoader('app_prod.cfg')
config_loader.short_name = ''
config_loader.bind_configuration(binder)
class RoutesModule(snakeweb.RoutesModule):
def configure(self, routes_binder):
bind = lambda s, c, a:\
routes_binder.connect(s, controller=c, action=a)
bind('/', controllers.ShortsController, 'index')
bind('/create', controllers.ShortsController, 'create')
bind('/favicon.ico', controllers.ShortsController, 'favicon')
bind('/:code', controllers.ShortsController, 'redirect')
def main():
injector = create_injector([ShortsModule(), RoutesModule()])
application = snakeweb.Application(injector)
run_wsgi_app(application)
if __name__ == '__main__':
main()