如何在Go语言和Google App Engine中使用外部CSS和(静态)图片

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

How to use externel CSS and (static) images in Go, Google App Engine

问题

我正在使用Go编写一个网页。Go代码使用“html/template”解析HTML。我想在项目中使用CSS。当我使用内部CSS代码时,一切都正常,但是当我想要切换到外部CSS时,它不起作用。看起来它无法访问.css文件。

这是我的app.yaml配置:

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

handlers:
- url: /.*
  script: _go_app
  
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images 

这是我想要从HTML中访问.css的方式:

<head>
<link href="/stylesheets/main.css" media="screen" rel="Stylesheet" type="text/css" />
</head>

我还遇到了(静态)图像的问题。我想使用以下HTML代码访问图像:

<img src="/images/img1.jpg" />

问题可能是什么?

(我在我的PC上进行测试,Win 7)

英文:

I'm working on a webpage in Go. The Go code uses "html/template" to parse HTML. I would like to use CSS in the project. Everything is working well when I use internal CSS code, but when I would like to change to external it dosen't work. It looks like it can't access to the .css file.

Here is my app.yaml configuration:

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

handlers:
- url: /.*
  script: _go_app
  
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images 

Here is how I would like to access to the .css from the html:

&lt;head&gt;
&lt;link href=&quot;/stylesheets/main.css&quot; media=&quot;screen&quot; rel=&quot;Stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;/head&gt;

I also have problems with (static) images. I would like to access to the image with this html code:

&lt;img src=&quot;/images/img1.jpg&quot; /&gt;

What can be the problem?

(I'm testing it on my PC, Win 7)

答案1

得分: 4

处理程序按顺序进行检查,您的第一个处理程序与所有内容匹配;将静态处理程序移到其上方。换句话说,当您的浏览器请求/stylesheets/main.css时,它会与第一个处理程序的/.*模式匹配,并要求go来提供它,而不是尝试静态目录。如果您交换处理程序的顺序,它将首先匹配/stylesheets并从静态目录中提供它。

例如:

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images 

- url: /.*
  script: _go_app
英文:

The handlers are checked in order, and your first handler matches everything; move your static handlers up above it. In other words, when your browser makes a request for /stylesheets/main.css, it matches the /.* pattern of your first handler and asks go to serve it rather than trying the static dir. If you flip the order of the handlers, it will match /stylesheets first and serve it from the static dir.

i.e.:

handlers:
- url: /stylesheets
  static_dir: stylesheets

- url: /images
  static_dir: images 

- url: /.*
  script: _go_app

huangapple
  • 本文由 发表于 2012年12月14日 20:39:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/13878887.html
匿名

发表评论

匿名网友

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

确定