-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.go
More file actions
executable file
·58 lines (47 loc) · 1.06 KB
/
app.go
File metadata and controls
executable file
·58 lines (47 loc) · 1.06 KB
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
package zebra
import (
"fmt"
"github.com/raythorn/zebra/log"
"github.com/raythorn/zebra/router"
"net/http"
// "os"
// "os/exec"
// "path/filepath"
"time"
)
type app struct {
router.Router
g *router.Group
}
func (a *app) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
a.Handle(rw, req)
}
func (a *app) run() {
finish := make(chan bool, 1)
go func() {
host := Env.Host()
port := Env.Port()
addr := fmt.Sprintf("%s:%d", host, port)
log.Info("Server listen at %s", addr)
if err := http.ListenAndServe(addr, a); err != nil {
log.Error("ListenAndServe fail")
time.Sleep(100 * time.Microsecond)
finish <- true
}
}()
if Env.TLS() {
go func() {
cert := Env.TLSCert()
key := Env.TLSKey()
host := Env.TLSHost()
port := Env.TLSPort()
addr := fmt.Sprintf("%s:%d", host, port)
if err := http.ListenAndServeTLS(addr, cert, key, a); err != nil {
log.Error("ListenAndServeTLS fail")
time.Sleep(100 * time.Microsecond)
finish <- true
}
}()
}
<-finish
}