|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/brave-experiments/go-stt/cr_api_websocket_proxy" |
| 10 | + "github.com/rs/zerolog" |
| 11 | + "github.com/rs/zerolog/log" |
| 12 | + "github.com/urfave/cli/v2" |
| 13 | +) |
| 14 | + |
| 15 | +// Configuration for the remote WebSocket STT service |
| 16 | +const ( |
| 17 | + version = "1" |
| 18 | + defaultListenAddress = "127.0.0.1:8090" |
| 19 | + defaultWebsocketURL = "ws://127.0.0.1:8080/api-speech-wss/" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + zerolog.SetGlobalLevel(zerolog.InfoLevel) |
| 24 | + log.Logger = log.Output( |
| 25 | + zerolog.ConsoleWriter{ |
| 26 | + Out: os.Stderr, |
| 27 | + NoColor: true, |
| 28 | + }, |
| 29 | + ) |
| 30 | + zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string { |
| 31 | + short := file |
| 32 | + for i := len(file) - 1; i > 0; i-- { |
| 33 | + if file[i] == '/' { |
| 34 | + short = file[i+1:] |
| 35 | + break |
| 36 | + } |
| 37 | + } |
| 38 | + file = short |
| 39 | + return file + ":" + strconv.Itoa(line) |
| 40 | + } |
| 41 | + |
| 42 | + zerolog.SetGlobalLevel(zerolog.DebugLevel) |
| 43 | + log.Logger = log.With().Caller().Logger() |
| 44 | + |
| 45 | + app := cli.NewApp() |
| 46 | + app.Name = "Chromium WebSpeech API Endpoint to WebSocket proxy" |
| 47 | + app.Version = version |
| 48 | + app.Flags = []cli.Flag{ |
| 49 | + &cli.StringFlag{ |
| 50 | + Name: "listen-address", |
| 51 | + Value: defaultListenAddress, |
| 52 | + }, |
| 53 | + &cli.StringFlag{ |
| 54 | + Name: "websocket-url", |
| 55 | + Value: defaultWebsocketURL, |
| 56 | + }, |
| 57 | + &cli.DurationFlag{ |
| 58 | + Name: "timeout", |
| 59 | + Value: 60 * time.Second, |
| 60 | + }, |
| 61 | + } |
| 62 | + app.Action = run |
| 63 | + |
| 64 | + if err := app.Run(os.Args); err != nil { |
| 65 | + log.Fatal().Err(err) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func run(c *cli.Context) error { |
| 70 | + // Create a configuration struct |
| 71 | + config := &cr_api_websocket_proxy.HandlerConfig{ |
| 72 | + WebsocketURL: c.String("websocket-url"), |
| 73 | + Timeout: c.Duration("timeout"), |
| 74 | + } |
| 75 | + |
| 76 | + // Create a handler instance with the config |
| 77 | + handler := cr_api_websocket_proxy.NewHandler(config) |
| 78 | + |
| 79 | + // Register handlers that have access to the config |
| 80 | + http.HandleFunc("/up", handler.HandleUpstreamRequest) |
| 81 | + http.HandleFunc("/down", handler.HandleDownstreamRequest) |
| 82 | + |
| 83 | + http.ListenAndServe(c.String("listen-address"), nil) |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
0 commit comments