-
Notifications
You must be signed in to change notification settings - Fork 2
/
listen.go
64 lines (54 loc) · 1.7 KB
/
listen.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
// +build linux darwin dragonfly freebsd netbsd openbsd rumprun
// Package macross is a high productive and modular web framework in Golang.
package macross
import (
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
"log"
"runtime"
)
func (m *Macross) Listen(args ...interface{}) {
addr := GetAddress(args...)
if runtime.NumCPU() > 1 {
runtime.GOMAXPROCS(runtime.NumCPU())
ln, err := reuseport.Listen("tcp4", addr)
if err != nil {
log.Fatalf("error in reuseport.Listen: %s", err)
}
if err = fasthttp.Serve(ln, m.ServeHTTP); err != nil {
log.Fatalf("error in fasthttp.Serve: %s", err)
}
} else {
fasthttp.ListenAndServe(addr, m.ServeHTTP)
}
}
func (m *Macross) ListenTLS(certFile, keyFile string, args ...interface{}) {
addr := GetAddress(args...)
if runtime.NumCPU() > 1 {
runtime.GOMAXPROCS(runtime.NumCPU())
ln, err := reuseport.Listen("tcp4", addr)
if err != nil {
log.Fatalf("error in reuseport.Listen: %s", err)
}
if err = fasthttp.ServeTLS(ln, certFile, keyFile, m.ServeHTTP); err != nil {
log.Fatalf("error in fasthttp.ServeTLS: %s", err)
}
} else {
fasthttp.ListenAndServeTLS(addr, certFile, keyFile, m.ServeHTTP)
}
}
func (m *Macross) ListenTLSEmbed(certData, keyData []byte, args ...interface{}) {
addr := GetAddress(args...)
if runtime.NumCPU() > 1 {
runtime.GOMAXPROCS(runtime.NumCPU())
ln, err := reuseport.Listen("tcp4", addr)
if err != nil {
log.Fatalf("error in reuseport.Listen: %s", err)
}
if err = fasthttp.ServeTLSEmbed(ln, certData, keyData, m.ServeHTTP); err != nil {
log.Fatalf("error in fasthttp.ServeTLSEmbed: %s", err)
}
} else {
fasthttp.ListenAndServeTLSEmbed(addr, certData, keyData, m.ServeHTTP)
}
}