Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add an example about embed static web resources #150

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ You can enter the example for information about "How to run"
- [hlog:](hlog) Example of using hlog and its log extension
- [trailer:](trailer) Example of read/write trailers for hertz server
- [graphql-go:](graphql-go) Example of using graphql in hertz server

- [embed](embed) Example of embed static web resources.

## Client

Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- [reverseproxy:](reverseproxy) 在 hertz server 中使用反向代理的示例
- [hlog:](hlog) 使用 hlog 以及其日志拓展的示例
- [graphql-go:](graphql-go) 在 hertz server 中使用 graphql 的示例
- [embed](embed) 在hertz中使用embed打包静态web资源


## Client
Expand Down
8 changes: 8 additions & 0 deletions embed/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# embed

## embed static web resources in hertz
- You can check the code [main.go](main.go)
- Start the server, and you can request the route:
- `localhost:8888/` you will see: embed static web resources
- `localhost:8888/test/test1.txt` you will see: test1
- `localhost:8888/test/test2.txt` you will see: test2
13 changes: 13 additions & 0 deletions embed/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>embed static web resources</h1>
</body>
</html>
24 changes: 24 additions & 0 deletions embed/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"embed"
"net/http"

"github.com/cloudwego/hertz/pkg/app/server"
"github.com/hertz-contrib/pprof/adaptor"
)

//go:embed index.html
var index embed.FS

//go:embed test
var test embed.FS

func main() {
srv := server.New()

srv.GET("/", adaptor.NewHertzHTTPHandler(http.FileServer(http.FS(index))))
srv.GET("/test/*filepath", adaptor.NewHertzHTTPHandler(http.FileServerFS(test)))

srv.Run()
}
1 change: 1 addition & 0 deletions embed/test/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test1
1 change: 1 addition & 0 deletions embed/test/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test2
Loading