Changeset - 50f3dbc467f9
[Not reviewed]
0 0 1
cyrill - 10 months ago 2021-01-30 12:56:55
ake@headake.win
Added sample application
1 file changed with 40 insertions and 0 deletions:
0 comments (0 inline, 0 general)
files.py
Show inline comments
 
new file 100755
 
import os
 
import mimetypes
 
from serpens import Serpens, SerpensResponse, error
 
 
BASE_DIR = os.path.dirname(__file__)
 
 
app = Serpens()
 
 
@app.route('/')
 
def index(request):
 
    if os.path.isfile(BASE_DIR + '/index.gmi'):
 
        with open(BASE_DIR + '/index.gmi', 'rb') as f:
 
            return SerpensResponse(f.read(), 20, mediatype='text/gemini')
 
    else:
 
        output = ['# Index']
 
        for item in os.listdir(BASE_DIR):
 
            output.append('=> /{} {}'.format(item, item))
 
        return SerpensResponse('\n'.join(output).encode('utf-8'), 20, 'text/gemini')
 
 
@app.route('/{any:path}')
 
def file_access(request, path):
 
    full_path = os.path.join(BASE_DIR, path)
 
    if os.path.isdir(full_path):
 
        if os.path.isfile(full_path + '/index.gmi'):
 
            with open(full_path + '/index.gmi', 'rb') as f:
 
                return SerpensResponse(f.read(), 20, mediatype='text/gemini')
 
        else:
 
            output = ['# ' + path]
 
            for item in os.listdir(full_path):
 
                output.append('=> /{} {}'.format(os.path.join(path, item), item))
 
            return SerpensResponse('\n'.join(output).encode('utf-8'), 20, 'text/gemini')
 
    elif os.path.isfile(full_path):
 
        mediatype = mimetypes.guess_type(full_path)[0] or 'application/octet-stream'
 
        with open(full_path + '/index.gmi', 'rb') as f:
 
            return SerpensResponse(f.read(), 20, mediatype=mediatype)
 
    else:
 
        return error('Not found', 51)
 
 
 
application = app.wsgi
 
\ No newline at end of file
0 comments (0 inline, 0 general)