Go HTTP 静态文件
最后修改时间 2024 年 4 月 11 日
在本文中,我们将展示如何在 Go 中设置 HTTP 服务器来提供静态文件。
超文本传输协议(Hypertext Transfer Protocol (HTTP))是一种用于分布式、协作式、超媒体信息系统的应用层协议。HTTP 协议是万维网数据通信的基础。
net/http 包提供了 HTTP 客户端和服务器实现,并用于创建 GET 和 POST 请求。
静态文件是不会改变的文件。它们包括 CSS 文件、JavaScript 文件和图像;也包括不包含模板指令的纯 HTML 文件。
http.FileServer 用于提供静态文件。它返回一个处理程序,该处理程序通过文件系统的内容来处理 HTTP 请求。
提供静态文件
在第一个示例中,我们提供包含图像和 CSS 文件的 HTML 文件。
go.mod main.go public ├── about.html ├── css │ └── format.css ├── img │ └── sid.png └── index.html
这是项目结构。
public/about.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/format.css">
<title>About</title>
</head>
<body>
<h2>About page</h2>
<p>
<a href="/">Home page</a>
</p>
</body>
</html>
这是 about.html 文件。
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/format.css">
<title>Home page</title>
</head>
<body>
<h2>Home page</h2>
<p>
<a href="about.html">About page</a>
</p>
<figure>
<img src="img/sid.png" alt="Sid the sloth">
<figcaption>Sid the sloth</figcaption>
</figure>
</body>
</html>
这是 index.html 页面。
public/css/format.css
* {
font-size: medium;
background-color:#2c2b2b ;
color: #e6d7d7;
}
这是 CSS 文件。
main.go
package main
import (
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./public"))
http.Handle("/", fs)
http.ListenAndServe(":8080", nil)
}
我们设置了服务器。静态文件从 public 目录读取。
Go 提供图像示例
在以下示例中,我们使用模板文件来动态生成页面。除了模板系统,我们还设置了为模板文件中包含的 CSS 文件提供静态文件。
go.mod
main.go
public
└── css
└── format.css
templates
└── layout.html
这是项目结构。
public/css/format.css
table {
border-top: 1px solid #999;
border-left: 1px solid #999;
border-collapse: collapse;
}
td, th {
padding: 5px;
border-right: 1px solid #999;
border-bottom: 1px solid #999;
}
tr:nth-child(2) {
background-color: #c1f5f7;
}
我们有一些 CSS 代码来为 HTML 表格设置样式。
templates/layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="public/css/format.css">
<title>Users</title>
</head>
<body>
<table>
<thead>
<tr>
<th>User</th>
<th>Occupation</th>
</tr>
</thead>
<tbody>
{{ range . }}
<tr>
<td>{{ .Name }}</td>
<td>{{ .Occupation }}</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>
在模板文件中,我们有指令可以将传入的数据合并到 HTML 表格中。format.css 文件包含在模板中,并作为静态资源提供。
main.go
package main
import (
"html/template"
"net/http"
"os"
)
type User struct {
Name string
Occupation string
}
func main() {
fs := http.FileServer(http.Dir("public"))
http.Handle("/public/", http.StripPrefix("/public/", fs))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/layout.html"))
data := []User{
{Name: "John Doe", Occupation: "gardener"},
{Name: "Roger Roe", Occupation: "driver"},
{Name: "Thomas Green", Occupation: "teacher"},
}
tmpl.Execute(w, data)
})
http.ListenAndServe(":8080", nil)
}
在代码示例中,我们设置了提供静态文件和模板引擎。
fs := http.FileServer(http.Dir("public"))
http.Handle("/public/", http.StripPrefix("/public/", fs))
静态资源来自 public 目录。
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/layout.html"))
data := []User{
{Name: "John Doe", Occupation: "gardener"},
{Name: "Roger Roe", Occupation: "driver"},
{Name: "Thomas Green", Occupation: "teacher"},
}
tmpl.Execute(w, data)
})
所有未从 public 目录提供的资源都由模板引擎管理。在匿名函数中,我们使用 template.parseFiles 解析 layout.html 文件,并将其与数据合并。
来源
在本文中,我们展示了如何在 Golang 中处理静态资源。