英文:
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: "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
It works for static files, but I can't access to my script.
<!-- language: lang-go -->
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!")
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论