-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
48 lines (46 loc) · 1.35 KB
/
main.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
package main
import (
"crypto/tls"
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
pusher, ok := w.(http.Pusher)
if ok {
// 主动推送服务资源
if err := pusher.Push("/static/app.js", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
if err := pusher.Push("/static/style.css", nil); err != nil {
log.Printf("Failed to push: %v", err)
}
}
fmt.Fprintf(w, `<html>
<head>
<title>"Gopher指北</title>
<link rel="stylesheet" href="/static/style.css"">
</head>
<body>
<div>Hello "Gopher指北</div>
<div id="content"></div>
<script src="/static/app.js"></script>
</body>
</html>`)
})
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// http.ListenAndServe(":8080", nil)
// http.ListenAndServeTLS(":8080", "ca.crt", "ca.key", nil)
server := &http.Server{Addr: ":8080", Handler: nil}
server.TLSConfig = new(tls.Config)
server.TLSConfig.PreferServerCipherSuites = true
server.TLSConfig.NextProtos = append(server.TLSConfig.NextProtos, "h2", "http/1.1")
// 如果支持的tls版本小于VersionTLS12,则h2不能放在server.TLSConfig.NextProtos里
server.TLSConfig.MaxVersion = tls.VersionTLS12
server.ListenAndServeTLS("ca.crt", "ca.key")
}