-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (45 loc) · 1.44 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
package main
import (
"flag"
"log"
"os"
ep "github.com/wrossmorrow/envoy-extproc-sdk-go"
)
type processor interface {
Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error
Finish()
ep.RequestProcessor
}
var processors = map[string]processor{
"body-rewrite": &bodyRewriteRequestProcessor{},
}
func parseArgs(args []string) (port *int, opts *ep.ProcessingOptions, nonFlagArgs []string) {
rootCmd := flag.NewFlagSet("root", flag.ExitOnError)
port = rootCmd.Int("port", 50051, "the gRPC port.")
opts = ep.NewDefaultOptions()
rootCmd.BoolVar(&opts.LogStream, "log-stream", false, "log the stream or not.")
rootCmd.BoolVar(&opts.LogPhases, "log-phases", false, "log the phases or not.")
rootCmd.BoolVar(&opts.UpdateExtProcHeader, "update-extproc-header", false, "update the extProc header or not.")
rootCmd.BoolVar(&opts.UpdateDurationHeader, "update-duration-header", false, "update the duration header or not.")
rootCmd.Parse(args)
nonFlagArgs = rootCmd.Args()
return
}
func main() {
// cmd subCmd arg, arg2,...
args := os.Args
if len(args) < 2 {
log.Fatal("Passing a processor is required.")
}
cmd := args[1]
proc, exists := processors[cmd]
if !exists {
log.Fatalf("Processor \"%s\" not defined.", cmd)
}
port, opts, nonFlagArgs := parseArgs(os.Args[2:])
if err := proc.Init(opts, nonFlagArgs); err != nil {
log.Fatalf("Initialize the processor is failed: %v.", err.Error())
}
defer proc.Finish()
ep.Serve(*port, proc)
}