-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
65 lines (56 loc) · 1.45 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
package hyprls
import (
"context"
"io"
"os"
"path/filepath"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/protocol"
"go.uber.org/multierr"
"go.uber.org/zap"
)
var Version string
func StartServer(logger *zap.Logger, logClientIn string) {
logger.Debug("starting server")
conn := jsonrpc2.NewConn(jsonrpc2.NewStream(&readWriteCloser{
reader: os.Stdin,
writer: os.Stdout,
logAt: logClientIn,
}))
handler, ctx, err := NewHandler(context.Background(), protocol.ServerDispatcher(conn, logger), logger)
if err != nil {
logger.Sugar().Fatalf("while initializing handler: %w", err)
}
conn.Go(ctx, protocol.ServerHandler(handler, jsonrpc2.MethodNotFoundHandler))
<-conn.Done()
}
type readWriteCloser struct {
reader io.ReadCloser
writer io.WriteCloser
logAt string
}
func (r *readWriteCloser) Read(b []byte) (int, error) {
var f *os.File
if r.logAt != "" {
f, _ = os.OpenFile(filepath.Join(r.logAt, "client-request-from.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
}
n, err := r.reader.Read(b)
if r.logAt != "" {
if err != nil {
f.Write([]byte(err.Error() + "\n"))
} else {
f.Write(b)
}
}
return n, err
}
func (r *readWriteCloser) Write(b []byte) (int, error) {
if r.logAt != "" {
f, _ := os.OpenFile(filepath.Join(r.logAt, "client-response-to.log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
f.Write(b)
}
return r.writer.Write(b)
}
func (r *readWriteCloser) Close() error {
return multierr.Append(r.reader.Close(), r.writer.Close())
}