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 55 56 57 58 59 60 61 62 63
|
package main
import (
"fmt"
"github.com/valyala/fasthttp"
)
var domains = make(map[string]fasthttp.RequestHandler)
func main() {
server := &fasthttp.Server{
// You can check the access using openssl command:
// $ openssl s_client -connect localhost:8080 << EOF
// > GET /
// > Host: localhost
// > EOF
//
// $ openssl s_client -connect localhost:8080 << EOF
// > GET /
// > Host: 127.0.0.1:8080
// > EOF
//
Handler: func(ctx *fasthttp.RequestCtx) {
h, ok := domains[string(ctx.Host())]
if !ok {
ctx.NotFound()
return
}
h(ctx)
},
}
// preparing first host
cert, priv, err := fasthttp.GenerateTestCertificate("localhost:8080")
if err != nil {
panic(err)
}
domains["localhost:8080"] = func(ctx *fasthttp.RequestCtx) {
ctx.WriteString("You are accessing to localhost:8080\n")
}
err = server.AppendCertEmbed(cert, priv)
if err != nil {
panic(err)
}
// preparing second host
cert, priv, err = fasthttp.GenerateTestCertificate("127.0.0.1")
if err != nil {
panic(err)
}
domains["127.0.0.1:8080"] = func(ctx *fasthttp.RequestCtx) {
ctx.WriteString("You are accessing to 127.0.0.1:8080\n")
}
err = server.AppendCertEmbed(cert, priv)
if err != nil {
panic(err)
}
fmt.Println(server.ListenAndServeTLS(":8080", "", ""))
}
|