-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_server_test.go
54 lines (45 loc) · 994 Bytes
/
file_server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"embed"
"io/fs"
"net/http"
"testing"
)
// With Directory
func TestFileServer(t *testing.T) {
dir := http.Dir("./resources")
fileServer := http.FileServer(dir)
// File Server read Pattern
// /resources/file(index.html)
mux := http.NewServeMux()
// 404 not Found
// StripPrefix
// Delete /static
mux.Handle("/static/", http.StripPrefix("/static", fileServer))
server := http.Server{
Addr: "localhost:1417",
Handler: mux,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
// File Server With Golang Embed
//
//go:embed resources
var resources embed.FS
func TestFileServerWithEmbed(t *testing.T) {
directory, _ := fs.Sub(resources, "resources")
fileServer := http.FileServer(http.FS(directory))
mux := http.NewServeMux()
mux.Handle("/akbar/", http.StripPrefix("/akbar", fileServer))
server := http.Server{
Addr: "localhost:1417",
Handler: mux,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}