Skip to content

Commit eaf1357

Browse files
committed
Add Chromium to websocket proxy.
1 parent bfe8fb9 commit eaf1357

File tree

11 files changed

+821
-0
lines changed

11 files changed

+821
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ models/*
2424
third_party/whisper.cpp
2525
.env
2626
dist
27+
28+
/.vscode

cmd/crtowebsocket/main.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

cmd/crtowebsocket/pprof.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//go:build pprof
2+
3+
package main
4+
5+
import (
6+
_ "net/http/pprof"
7+
)

cr_api_websocket_proxy/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Chromium to WebSocket
2+
3+
This is a simple HTTP server that listens for WebSpeech connections from a
4+
Chromium browser, converts audio data to WAV format, streams it to a WebSocket
5+
client, receives text data from the WebSocket client, and sends it back to the
6+
Chromium browser.
7+
8+
## Usage
9+
10+
```bash
11+
go run .
12+
```
13+
14+
## Building
15+
16+
```bash
17+
go build -o crtws .
18+
```
19+
20+
## Running
21+
22+
```bash
23+
./crtws
24+
```
25+
26+
## Building for release
27+
28+
```bash
29+
go build -o crtws .
30+
```
31+
32+
## Running for release
33+
34+
```bash
35+
./crtws
36+
```

0 commit comments

Comments
 (0)