Browse Source

support WSGI

pull/2/merge
lilydjwg 10 years ago
parent
commit
28076cdc29
2 changed files with 33 additions and 12 deletions
  1. +4
    -3
      README.md
  2. +29
    -9
      main.py

+ 4
- 3
README.md View File

@ -4,10 +4,11 @@ Usage
=====
Run it
------
Just execute it!
As a standalone web server, just execute it. As a WSGI application, you may
want to specify a new `PREFIX` pattern to meet your server configuration.
By default it runs at port `8888`, with current working directory as data
directory to store image files. Try option `--help` to know how to change them.
By default it runs at port `8888`, with `/tmp` as data directory to store image
files. Try option `--help` to know how to change them.
Upload images
-------------


+ 29
- 9
main.py View File

@ -1,6 +1,11 @@
#!/usr/bin/env python3
# vim:fileencoding=utf-8
# options
DEBUG = True
DEFAULT_DATA_DIR = '/tmp'
PREFIX = r'/elimage' # this is a regex
import os
import hashlib
from collections import OrderedDict
@ -17,11 +22,6 @@ except ImportError:
return wrapper
import tornado.web
import tornado.httpserver
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
define("datadir", default='.', help="the directory to put uploaded data", type=str)
@lru_cache()
def guess_mime_using_file(path):
@ -47,7 +47,7 @@ class IndexHandler(tornado.web.RequestHandler):
for file in filelist:
if not (os.path.splitext(file['filename'])[1][1:].lower() in ('png', 'jpg', 'gif') \
or file['content_type'].startswith('image/')):
ret[file['filename']] = 'error: not an image.\n'
ret[file['filename']] = 'error: not an image.\n'
else:
m = hashlib.sha1()
m.update(file['body'])
@ -60,8 +60,8 @@ class IndexHandler(tornado.web.RequestHandler):
fpath = os.path.join(p, f)
if not os.path.exists(fpath):
open(fpath, 'wb').write(file['body'])
ret[file['filename']] = '%s://%s/%s/%s\n' % (
self.request.protocol, self.request.host, d, f
ret[file['filename']] = '%s/%s/%s\n' % (
self.request.full_url().rstrip('/'), d, f
)
if len(ret) > 1:
for i in ret.items():
@ -70,6 +70,11 @@ class IndexHandler(tornado.web.RequestHandler):
self.write(tuple(ret.values())[0])
def main():
import tornado.httpserver
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
define("datadir", default=DEFAULT_DATA_DIR, help="the directory to put uploaded data", type=str)
tornado.options.parse_command_line()
application = tornado.web.Application([
(r"/", IndexHandler),
@ -78,14 +83,29 @@ def main():
}),
],
datadir=options.datadir,
debug=True,
debug=DEBUG,
)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
def wsgi():
import tornado.wsgi
global application
application = tornado.wsgi.WSGIApplication([
(PREFIX+r"/", IndexHandler),
(PREFIX+r"/([a-fA-F0-9]{2}/[a-fA-F0-9]{38})", tornado.web.StaticFileHandler, {
'path': DEFAULT_DATA_DIR,
}),
],
datadir=DEFAULT_DATA_DIR,
debug=DEBUG,
)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
else:
wsgi()

Loading…
Cancel
Save