diff --git a/README.md b/README.md index e966d0d..4a679ce 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/README_CN.md b/README_CN.md index 35861d6..5fb77d6 100644 --- a/README_CN.md +++ b/README_CN.md @@ -65,6 +65,7 @@ - [reverseproxy:](reverseproxy) 在 hertz server 中使用反向代理的示例 - [hlog:](hlog) 使用 hlog 以及其日志拓展的示例 - [graphql-go:](graphql-go) 在 hertz server 中使用 graphql 的示例 +- [embed](embed) 在hertz中使用embed打包静态web资源 ## Client diff --git a/embed/README.md b/embed/README.md new file mode 100644 index 0000000..4321e08 --- /dev/null +++ b/embed/README.md @@ -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 \ No newline at end of file diff --git a/embed/index.html b/embed/index.html new file mode 100644 index 0000000..06f5047 --- /dev/null +++ b/embed/index.html @@ -0,0 +1,13 @@ + + + + + + + Document + + +

embed static web resources

+ + \ No newline at end of file diff --git a/embed/main.go b/embed/main.go new file mode 100644 index 0000000..690ff73 --- /dev/null +++ b/embed/main.go @@ -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() +} diff --git a/embed/test/test1.txt b/embed/test/test1.txt new file mode 100644 index 0000000..f079749 --- /dev/null +++ b/embed/test/test1.txt @@ -0,0 +1 @@ +test1 \ No newline at end of file diff --git a/embed/test/test2.txt b/embed/test/test2.txt new file mode 100644 index 0000000..d606037 --- /dev/null +++ b/embed/test/test2.txt @@ -0,0 +1 @@ +test2 \ No newline at end of file