Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static file using App Engine

I created an App Engine application. Till now, I only have a few HTML files to serve. What can I do to make App Engine serve the index.html file whenever someone visits http://example.appengine.com/ ?

Currently, my app.yaml file looks like this:

application: appname
version: 1
runtime: python
api_version: 1

handlers:

- url: /
  static_dir: static_files
like image 396
User Avatar asked Apr 10 '11 00:04

User


People also ask

What is serving static file?

Static files are files that clients download as they are from the server. Create a new directory, public. Express, by default does not allow you to serve static files. You need to enable it using the following built-in middleware.

Which app must installed for use of static file?

ASP.NET Core application cannot serve static files by default. We must include Microsoft. AspNetCore.

Which is the best way to serve static assets to users around the globe?

It is usually the most optimized solution to reduce the amount of requests on your node. js server that is slower to server static files than nginx for example : An even better option is to use a reverse proxy to serve static files; see Use a reverse proxy for more information.


2 Answers

This should do what you need:

https://gist.github.com/873098

Explanation: In App Engine Python it's possible to use regular expressions as URL handlers in app.yaml and redirect all URLs to a hierarchy of static files.

Example app.yaml:

application: your-app-name-here
version: 1
runtime: python
api_version: 1

handlers:
- url: /(.*\.css)
  mime_type: text/css
  static_files: static/\1
  upload: static/(.*\.css)

- url: /(.*\.html)
  mime_type: text/html
  static_files: static/\1
  upload: static/(.*\.html)

- url: /(.*\.js)
  mime_type: text/javascript
  static_files: static/\1
  upload: static/(.*\.js)

- url: /(.*\.txt)
  mime_type: text/plain
  static_files: static/\1
  upload: static/(.*\.txt)

- url: /(.*\.xml)
  mime_type: application/xml
  static_files: static/\1
  upload: static/(.*\.xml)

# image files
- url: /(.*\.(bmp|gif|ico|jpeg|jpg|png))
  static_files: static/\1
  upload: static/(.*\.(bmp|gif|ico|jpeg|jpg|png))

# index files
- url: /(.+)/
  static_files: static/\1/index.html
  upload: static/(.+)/index.html

# redirect to 'url + /index.html' url.
- url: /(.+)
  static_files: static/redirector.html
  upload: static/redirector.html

# site root
- url: /
  static_files: static/index.html
  upload: static/index.html

In order to handle requests to URLs that don't end with a recognized type (.html, .png, etc.) or / you need to redirect those requests to URL + / so the index.html for that directory is served. I don't know of a way to do this inside the app.yaml, so I added a javascript redirector. This could also be done with a tiny python handler.

redirector.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script language="JavaScript">
      self.location=self.location + "/";
    </script>
  </head>
  <body>
  </body>
</html>
like image 91
Calvin Avatar answered Oct 04 '22 04:10

Calvin


If you're trying to map / to index.html:

handlers:
- url: /
  upload: folderpath/index.html
  static_files: folderpath/index.html

the url: will match on a path and supports regex.

- url: /images
  static_dir: static_files/images

So if your image file is stored at static_files/images/picture.jpg use this:

<img src="/images/picture.jpg" />
like image 23
hyperslug Avatar answered Oct 04 '22 05:10

hyperslug