使用App Engine提供静态文件 [英] Serve static file using App Engine

查看:126
本文介绍了使用App Engine提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个App Engine应用程序。到目前为止,我只有几个HTML文件可供使用。当有人访问 http://example.appengine.com/ 时,我可以做些什么来使App Engine成为index.html文件服务器? / b> $ <$ p

目前,我的app.yaml文件如下所示:

c $ c> application:appname
version:1
runtime:python
api_version:1

处理程序:
$ b $ - url:/
static_dir:static_files


解决方案

您需要:

https://gist.github.com / 873098



说明:在App Engine Python中,可以在 app.yaml 并将所有URL重定向到静态文件的层次结构。


$ b

示例 app.yaml

  application:your-app-name-here 
版本:1
运行时:python
api_version:1

处理程序:
- url:/(.*\.css)
mime_type:text / css
static_files:static / \ 1
上传:static /(.* \.css)

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

- url:/(.*\.js)
mime_type:text / javascript
static_files:static / \ 1
上传: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)

#图片文件
- url:/(.*\.(bmp | gif | ico | jpeg | jpg | png))
static_files:static / \ 1
上传:stati c /(.* \。(bmp | gif | ico | jpeg | jpg | png))

#索引文件
- url:/(.+)/
static_files:static / \ 1 / index.html
upload:static /(.+)/ index.html

#重定向到url + /index.htmlurl。
- url:/(.+)
static_files:static / redirector.html
upload:static / redirector.html

$ root
- url:/
static_files:static / index.html
upload:static / index.html

为了处理对不以识别类型( .html .png 等)或 / ,您需要将这些请求重定向到 URL + / ,所以<$ c $为该目录提供c> index.html 。我不知道如何在 app.yaml 内执行此操作,所以我添加了一个javascript重定向器。这也可以用一个小型的python处理程序完成。



redirector.html

 <!DOCTYPE html> 
< html lang =en>
< head>
< script language =JavaScript>
self.location = self.location +/;
< / script>
< / head>
< body>
< / body>
< / html>


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

解决方案

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>

这篇关于使用App Engine提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆