Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: introduce LSP websocket server framework (#9022)

rebelice5c881e3705bd
diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.go
new file mode 100644
index 0000000000..57afe1d503
--- /dev/null
+++ b/backend/api/lsp/handler.go
@@ -0,0 +1,43 @@
+package lsp
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/sourcegraph/jsonrpc2"
+)
+
+// NewHandler creates a new Language Server Protocol handler.
+func NewHandler() jsonrpc2.Handler {
+ return lspHandler{jsonrpc2.HandlerWithError((&Handler{}).handle)}
+}
+
+type lspHandler struct {
+ jsonrpc2.Handler
+}
+
+func (h lspHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
+ if isFileSystemRequest(req.Method) {
+ h.Handler.Handle(ctx, conn, req)
+ return
+ }
+ go h.Handler.Handle(ctx, conn, req)
+}
+
+// isFileSystemRequest returns if this is an LSP method whose sole
+// purpose is modifying the contents of the overlay file system.
+func isFileSystemRequest(method string) bool {
+ return method == "textDocument/didOpen" ||
+ method == "textDocument/didChange" ||
+ method == "textDocument/didClose" ||
+ method == "textDocument/didSave"
+}
+
+// Handler handles Language Server Protocol requests.
+type Handler struct {
+}
+
+func (*Handler) handle(_ context.Context, _ *jsonrpc2.Conn, req *jsonrpc2.Request) (any, error) {
+ // TODO: implement
+ return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}
+}
diff --git a/backend/api/lsp/lsp.go b/backend/api/lsp/lsp.go
new file mode 100644
index 0000000000..61d90d8caf
--- /dev/null
+++ b/backend/api/lsp/lsp.go
@@ -0,0 +1,51 @@
+package lsp
+
+import (
+ "io"
+ "log/slog"
+ "net/http"
+ "strings"
+
+ "github.com/gorilla/websocket"
+ "github.com/labstack/echo/v4"
+ "github.com/pkg/errors"
+ "github.com/sourcegraph/jsonrpc2"
+
+ wsjsonrpc2 "github.com/sourcegraph/jsonrpc2/websocket"
+
+ "github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/common/stacktrace"
+)
+
+var (
+ upgrader = websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
+ newHandler = func() (jsonrpc2.Handler, io.Closer) {
+ return NewHandler(), io.NopCloser(strings.NewReader(""))
+ }
+)
+
+func (s *Server) Router(c echo.Context) error {
+ connection, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
+ if err != nil {
+ slog.Error("Failed to upgrade websocket connection", log.BBError(errors.Errorf("errors: %v\n%s", err, stacktrace.TakeStacktrace(20 /* n */, 5 /* skip */))))
+ return errors.Wrap(err, "could not upgrade to WebSocket")
+ }
+ defer connection.Close()
+ // Register the connection to be closed when the server shuts down.
+ c.Echo().Server.RegisterOnShutdown(func() {
+ err := connection.Close()
+ if err != nil {
+ slog.Error("Failed to close websocket connection", log.BBError(err))
+ }
+ })
+ connectionID := s.connectionCount.Add(1)
+
+ handler, closer := newHandler()
+ ctx := c.Request().Context()
+ <-jsonrpc2.NewConn(ctx, wsjsonrpc2.NewObjectStream(connection), handler, nil /* connOpt */).DisconnectNotify()
+ err = closer.Close()
+ if err != nil {
+ return errors.Wrapf(err, "failed to close LSP connection: %v", connectionID)
+ }
+ return nil
+}
diff --git a/backend/api/lsp/server.go b/backend/api/lsp/server.go
new file mode 100644
index 0000000000..46323ed69d
--- /dev/null
+++ b/backend/api/lsp/server.go
@@ -0,0 +1,23 @@
+package lsp
+
+import (
+ "sync/atomic"
+
+ "github.com/bytebase/bytebase/backend/store"
+)
+
+// Server is the Language Server Protocol service.
+type Server struct {
+ connectionCount atomic.Uint64
+
+ store *store.Store
+}
+
+// NewServer creates a Language Server Protocol service.
+func NewServer(
+ store *store.Store,
+) *Server {
+ return &Server{
+ store: store,
+ }
+}
diff --git a/backend/server/routers.go b/backend/server/routers.go
index 1cc551335b..7baf66b2cc 100644
--- a/backend/server/routers.go
+++ b/backend/server/routers.go
@@ -30,6 +30,7 @@ func configureEchoRouters(e *echo.Echo, grpcServer *grpc.Server, mux *grpcruntim
// Skip grpc and webhook calls.
return strings.HasPrefix(c.Request().URL.Path, "/bytebase.v1.") ||
strings.HasPrefix(c.Request().URL.Path, "/v1:adminExecute") ||
+ strings.HasPrefix(c.Request().URL.Path, lspAPI) ||
strings.HasPrefix(c.Request().URL.Path, webhookAPIPrefix)
}
e.Use(middleware.TimeoutWithConfig(middleware.TimeoutConfig{
diff --git a/backend/server/server.go b/backend/server/server.go
index 77fb8f5cad..9daed473ee 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -21,6 +21,7 @@ import (
"github.com/bytebase/bytebase/backend/api/auth"
"github.com/bytebase/bytebase/backend/api/gitops"
+ "github.com/bytebase/bytebase/backend/api/lsp"
apiv1 "github.com/bytebase/bytebase/backend/api/v1"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/common/stacktrace"
@@ -53,7 +54,9 @@ import (
const (
// webhookAPIPrefix is the API prefix for Bytebase webhook.
- webhookAPIPrefix = "/hook"
+ webhookAPIPrefix = "/hook"
+ // lspAPI is the API for Bytebase Language Server Protocol.
+ lspAPI = "/lsp"
maxStacksize = 1024 * 10240
gracefulShutdownPeriod = 10 * time.Second
)
@@ -80,6 +83,7 @@ type Server struct {
profile config.Profile
e *echo.Echo
grpcServer *grpc.Server
+ lspServer *lsp.Server
store *store.Store
dbFactory *dbfactory.DBFactory
startedTs int64
@@ -347,6 +351,9 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
reflection.Register(s.grpcServer)
+ s.lspServer = lsp.NewServer(s.store)
+ s.e.GET(lspAPI, s.lspServer.Router)
+
serverStarted = true
return s, nil
}
@@ -393,6 +400,7 @@ func (s *Server) Run(ctx context.Context, port int) error {
slog.Error("grpc server listen error", log.BBError(err))
}
}()
+
return s.e.Start(fmt.Sprintf(":%d", port))
}
diff --git a/go.mod b/go.mod
index 2409ed40f8..5f9c775bde 100644
--- a/go.mod
+++ b/go.mod
@@ -29,6 +29,7 @@ require (
github.com/google/cel-go v0.18.1
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.1
+ github.com/gorilla/websocket v1.4.2
github.com/gosimple/slug v1.13.1
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0
@@ -56,6 +57,7 @@ require (
github.com/shopspring/decimal v1.3.1
github.com/sijms/go-ora/v2 v2.7.18
github.com/snowflakedb/gosnowflake v1.6.25
+ github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/swaggo/echo-swagger v1.4.1
@@ -103,7 +105,6 @@ require (
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/glog v1.1.2 // indirect
github.com/google/s2a-go v0.1.7 // indirect
- github.com/gorilla/websocket v1.4.2 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
@@ -269,7 +270,7 @@ require (
google.golang.org/appengine v1.6.8 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
- nhooyr.io/websocket v1.8.7 // indirect
+ nhooyr.io/websocket v1.8.10 // indirect
)
// copied from pingcap/tidb
diff --git a/go.sum b/go.sum
index 475ee08ef8..5c01ec84ca 100644
--- a/go.sum
+++ b/go.sum
@@ -283,9 +283,7 @@ github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
-github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
-github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-asn1-ber/asn1-ber v1.5.5 h1:MNHlNMBDgEKD4TcKr36vQN68BA00aDfjIt3/bD50WnA=
github.com/go-asn1-ber/asn1-ber v1.5.5/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
@@ -326,11 +324,8 @@ github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
-github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
-github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
-github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-sql-driver/mysql v1.3.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
@@ -338,11 +333,8 @@ github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LB
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
-github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
-github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
-github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
@@ -620,7 +612,6 @@ github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUU
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
-github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
@@ -902,6 +893,8 @@ github.com/snowflakedb/gosnowflake v1.6.25 h1:o5zUmxTOo0Eo9AdkEj8blCeiMuILrQJ+rj
github.com/snowflakedb/gosnowflake v1.6.25/go.mod h1:KfO4F7bk+aXPUIvBqYxvPhxLlu2/w4TtSC8Rw/yr5Mg=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY=
+github.com/sourcegraph/jsonrpc2 v0.2.0 h1:KjN/dC4fP6aN9030MZCJs9WQbTOjWHhrtKVpzzSrr/U=
+github.com/sourcegraph/jsonrpc2 v0.2.0/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
@@ -956,9 +949,7 @@ github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaO
github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk=
github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg=
github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
-github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
-github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
@@ -1379,8 +1370,8 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
nhooyr.io/websocket v1.8.6/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
-nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
-nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
+nhooyr.io/websocket v1.8.10 h1:mv4p+MnGrLDcPlBoWsvPP7XCzTYMXP9F9eIGoKbgx7Q=
+nhooyr.io/websocket v1.8.10/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0 h1:ucqkfpjg9WzSUubAO62csmucvxl4/JeW3F4I4909XkM=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=