/
echo.go
48 lines (43 loc) · 940 Bytes
/
echo.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 (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"runtime/pprof"
"time"
"github.com/coyove/resh"
reuseport "github.com/kavu/go_reuseport"
)
func main() {
rand.Seed(time.Now().Unix())
log.SetFlags(log.Lshortfile | log.Ltime)
if os.Getenv("GO") != "" {
fmt.Println("native go")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
ln, _ := reuseport.Listen("tcp", ":7777")
http.Serve(ln, nil)
} else {
if os.Getenv("DEBUG") == "1" {
out, _ := os.Create("cpuprofile")
pprof.StartCPUProfile(out)
time.AfterFunc(time.Second*30, func() {
pprof.StopCPUProfile()
out.Close()
})
}
fmt.Println("resh", os.Getpid())
ln, _ := resh.Listen(true, ":7777")
ln.OnError = func(err error) {
log.Println(err)
}
ln.OnHTTP = func(req *resh.HTTP) bool {
req.Text(200, "hello world").Release()
return true
}
ln.Serve()
}
}