在app.yaml中混合使用脚本和静态内容

huangapple go评论78阅读模式
英文:

Mixing script and static in app.yaml

问题

我希望在一个应用程序中使用Google App Engine来提供静态文件和REST请求。

我使用了这个app.yaml配置文件:

application: test
version: 1
runtime: go
api_version: go1

default_expiration: "7d 5h"

handlers:
- url: /(index.html)?
  static_files: static/app/index.html
  upload: static/app/index.html
  http_headers:
    Content-Type: text/html; charset=UTF-8

- url: /
  static_dir: static/app/
  http_headers:
    Vary: Accept-Encoding

- url: /.*
  script: _go_app

这个配置可以用于处理静态文件,但我无法访问我的脚本。

package main

import (
    "fmt"
    "net/http"
)

func init() {
    http.HandleFunc("/hello_world", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello, world!")
}

这种情况是否可行?如果是,我应该如何进行操作?

英文:

I wish to use Google App Engine for serve static files and REST request in one application.

I use this app.yaml
<!-- language: lang-yaml -->

application: test
version: 1
runtime: go
api_version: go1

default_expiration: &quot;7d 5h&quot;

handlers:
- url: /(index.html)?
  static_files: static/app/index.html
  upload: static/app/index.html
  http_headers:
    Content-Type: text/html; charset=UTF-8

- url: /
  static_dir: static/app/
  http_headers:
    Vary: Accept-Encoding

- url: /.*
  script: _go_app

It works for static files, but I can't access to my script.

<!-- language: lang-go -->

package main

import (
    &quot;fmt&quot;
    &quot;net/http&quot;
)

func init() {
    http.HandleFunc(&quot;/hello_world&quot;, handler)
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, &quot;Hello, world!&quot;)
}

It's possible? If yes, how I can to proceed?

答案1

得分: 1

是的,一个应用程序可以同时提供静态文件和动态内容。实际上,这对大多数Web应用程序来说是很正常的,没有什么神奇的要求。

你的配置看起来没问题。有一件事你应该改变:你的go文件的包名。

不要使用main包,而是使用另一个包,并确保你的.go文件放在其中。例如,如果你将包命名为mypackage,请使用以下文件夹结构:

+ProjectRoot
    app.yaml
    +mypackage
        myfile.go

并在你的myfile.go文件中加入以下代码:

package mypackage
英文:

Yes, it's possible for an app to serve both static files and dynamic content. Actually there's nothing magical required for it, this is normal for most web applications.

Your configuration looks ok. One thing you should change: the package of your go file.

Don't use package main, instead use another package and make sure your .go file is put into that. For example if you name your package mypackage, use this folder structure:

+ProjectRoot
    app.yaml
    +mypackage
        myfile.go

And start your myfile.go with line:

package mypackage

huangapple
  • 本文由 发表于 2015年6月5日 18:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/30664333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定