forked from cloudwego/hertz-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (85 loc) · 2.68 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"crypto/tls"
"fmt"
"time"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/network/standard"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
func main() {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.X25519, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
},
}
cert, err := tls.LoadX509KeyPair("./protocol/tls/server.crt", "./protocol/tls/server.key")
if err != nil {
fmt.Println(err.Error())
}
cfg.Certificates = append(cfg.Certificates, cert)
h := server.Default(server.WithTLS(cfg), server.WithHostPorts(":8443"))
h.Use(func(c context.Context, ctx *app.RequestContext) {
fmt.Fprint(ctx, "Before real handle...\n")
ctx.Next(c)
fmt.Fprint(ctx, "After real handle...\n")
})
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.String(consts.StatusOK, "TLS test\n")
})
go func() {
h.Spin()
}()
time.Sleep(time.Millisecond * 50)
doTlsRequest()
}
func doTlsRequest() {
clientCfg := &tls.Config{
InsecureSkipVerify: true,
}
c, err := client.NewClient(
client.WithTLSConfig(clientCfg),
client.WithDialer(standard.NewDialer()),
)
if err != nil {
fmt.Println(err.Error())
}
req, res := protocol.AcquireRequest(), protocol.AcquireResponse()
defer func() {
protocol.ReleaseRequest(req)
protocol.ReleaseResponse(res)
}()
req.SetMethod(consts.MethodGet) // set request method
req.Header.SetContentTypeBytes([]byte("application/json")) // set request header
req.SetRequestURI("https://localhost:8443/ping") // set request url
err = c.Do(context.Background(), req, res)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%v\n", string(res.Body())) // read response body
time.Sleep(time.Second)
}