-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (60 loc) · 1.41 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
package main
import (
"context"
"flag"
"fmt"
"github.com/simon-engledew/twirpmock/pkg/handler"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"time"
)
func unmarshalSet(filename string, set *descriptorpb.FileDescriptorSet) error {
data, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
return proto.Unmarshal(data, set)
}
func run(setPath, scriptPath, host string, port int) error {
var set descriptorpb.FileDescriptorSet
if err := unmarshalSet(setPath, &set); err != nil {
return err
}
mux := handler.NewServeMux()
if err := mux.Handle(&set, scriptPath, nil); err != nil {
return err
}
addr := fmt.Sprintf("%s:%d", host, port)
ctx := context.Background()
server := &http.Server{
Handler: mux,
Addr: addr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
BaseContext: func(config net.Listener) context.Context {
return ctx
},
}
return server.ListenAndServe()
}
func main() {
var host string
flag.StringVar(&host, "h", "127.0.0.1", "host")
flag.Parse()
log.SetFlags(0)
if flag.NArg() != 2 {
log.Fatalf("usage: %s <descriptor-set> <script>", os.Args[0])
}
port := 8888
setPath := flag.Arg(0)
scriptPath := flag.Arg(1)
log.Printf("Starting listener on %s:%d...", host, port)
if err := run(setPath, scriptPath, host, port); err != nil {
panic(err)
}
}