0

My personal website only contains static files. I want to deploy it to Sina App Engine. The app engine requires me to configure a index.wsgi file.

The problem is that I do not know how to match domain/static/index.html to the domian itself. That means when I input the domain itself the server will response with the file /static/index.html.

I could not Google a good solution. Could anyone help please?

Jiawei Lu
  • 1
  • 2

1 Answers1

0

I have found something really useful Serve Static Content And based on this I wrote some Python code. Problem solved!

Here is the code (index.wsgi)

import os

    MIME_TABLE = {'.txt': 'text/plain',
          '.html': 'text/html',
          '.css': 'text/css',
          '.js': 'application/javascript'
          }  

def application(environ, start_response):

    path = environ['PATH_INFO']

    if path == '/':
        path = 'static/index.html'
    else:
        path = 'static' + path

    if os.path.exists(path):
        h = open(path, 'rb')
        content = h.read()
        h.close()
        headers = [('content-type', content_type(path))]
        start_response('200 OK', headers)
        return [content]
    ''' else: return a 404 application '''

def content_type(path):

    name, ext = os.path.splitext(path)

    if ext in MIME_TABLE:
        return MIME_TABLE[ext]
    else:
        return "application/octet-stream"
Jiawei Lu
  • 1
  • 2
  • 1
    I think your code is vulnerable to a directory traversal attack (eg if someone provides `path = '../../etc/passwd'` - see https://stackoverflow.com/questions/6803505/does-my-code-prevent-directory-traversal for how to sanitise input paths – John Carter Feb 27 '16 at 02:30