-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (57 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/recoilme/slowpoke"
"github.com/recoilme/okdb/api"
"google.golang.org/grpc"
)
// main start a gRPC server and waits for connection
// run from root: protoc -I api/ -I${GOPATH}/src --go_out=plugins=grpc:api api/api.proto
func main() {
grpcPort := flag.Int("grpc", 7777, "grpc port number, default 7777")
httpAddr := flag.String("http", ":5000", "http api address, default :5000")
flag.Parse()
// create a listener on TCP port 7777
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *grpcPort))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
// create a server instance
s := api.Server{}
// create a gRPC server object
grpcServer := grpc.NewServer()
// attach the Ping service to the server
api.RegisterOkdbServer(grpcServer, &s)
// start the server
go func() {
log.Fatal(grpcServer.Serve(lis))
}()
// start http server
Serve(*httpAddr)
// handle kill
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
log.Println("Shutdown signal received, exiting...")
closeErr := slowpoke.CloseAll()
if closeErr != nil {
log.Fatal(closeErr.Error())
}
}
// Serve run server
// default addr: ":5000"
// example usage ./okdb -http=":5000">>simpleserver.log &
func Serve(addr string) {
http.HandleFunc("/slowpoke/", handlerSlowPoke)
log.Fatal(http.ListenAndServe(addr, nil))
}
func handlerSlowPoke(w http.ResponseWriter, r *http.Request) {
api.Parser(w, r)
}