mirror of
https://github.com/anyproto/anytype-heart.git
synced 2025-06-07 21:37:04 +09:00
protos-web server
This commit is contained in:
parent
34b13baced
commit
d5e424553a
13 changed files with 149 additions and 2381 deletions
|
@ -52,7 +52,7 @@ jobs:
|
|||
name: Run unit tests
|
||||
command: |
|
||||
mkdir ~/gotestsum-report
|
||||
PACKAGE_NAMES=$(go list ./... | grep -v "github.com/anytypeio/go-anytype-middleware/lib-debug" | circleci tests split --split-by=timings --timings-type=classname)
|
||||
PACKAGE_NAMES=$(go list ./... | grep -v "github.com/anytypeio/go-anytype-middleware/lib-server" | circleci tests split --split-by=timings --timings-type=classname)
|
||||
gotestsum --junitfile ~/gotestsum-report/gotestsum-report.xml -- -tags nogrpcserver $PACKAGE_NAMES
|
||||
- save_cache:
|
||||
key: go-pkg-v3-{{ arch }}-{{ checksum "go.sum" }}
|
||||
|
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -7,4 +7,4 @@ package-lock.json
|
|||
.vscode
|
||||
/vendor/
|
||||
*_mock.go
|
||||
lib-debug/service.pb.go
|
||||
lib-server/service.pb.go
|
||||
|
|
24
Makefile
24
Makefile
|
@ -70,23 +70,28 @@ setup-protoc:
|
|||
cd $(GOPATH)/src/github.com/gogo; git clone https://github.com/anytypeio/protobuf
|
||||
cd $(GOPATH)/src/github.com/gogo/protobuf; go install github.com/gogo/protobuf/protoc-gen-gogofaster
|
||||
cd $(GOPATH)/src/github.com/gogo/protobuf; go install github.com/gogo/protobuf/protoc-gen-gogofast
|
||||
|
||||
cd $(GOPATH); go get -u github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc
|
||||
export PATH=$(PATH):$(GOROOT)/bin:$(GOPATH)/bin
|
||||
|
||||
git clone https://github.com/grpc/grpc-web
|
||||
cd grpc-web
|
||||
make install-plugin
|
||||
cd ..
|
||||
rm -rf grpc-web
|
||||
protos-deps:
|
||||
$(eval LIBRARY_PATH = $(shell go list -m -json all | jq -r 'select(.Path == "github.com/anytypeio/go-anytype-library") | .Dir'))
|
||||
mkdir -p vendor/github.com/anytypeio/go-anytype-library/
|
||||
cp -R $(LIBRARY_PATH)/pb vendor/github.com/anytypeio/go-anytype-library/
|
||||
chmod -R 755 ./vendor/github.com/anytypeio/go-anytype-library/pb
|
||||
|
||||
protos-debug: protos-deps
|
||||
protos-server: protos-deps
|
||||
$(eval P_TIMESTAMP := Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types)
|
||||
$(eval P_STRUCT := Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types)
|
||||
$(eval P_PROTOS := Mvendor/github.com/anytypeio/go-anytype-library/pb/model/protos/models.proto=github.com/anytypeio/go-anytype-library/pb/model)
|
||||
$(eval P_PROTOS2 := Mpb/protos/commands.proto=github.com/anytypeio/go-anytype-middleware/pb)
|
||||
$(eval P_PROTOS3 := Mpb/protos/events.proto=github.com/anytypeio/go-anytype-middleware/pb)
|
||||
$(eval PKGMAP := $$(P_TIMESTAMP),$$(P_STRUCT),$$(P_PROTOS),$$(P_PROTOS2),$$(P_PROTOS3))
|
||||
GOGO_NO_UNDERSCORE=1 GOGO_EXPORT_ONEOF_INTERFACE=1 GOGO_GRPC_SERVER_METHOD_NO_ERROR=1 GOGO_GRPC_SERVER_METHOD_NO_CONTEXT=1 PACKAGE_PATH=github.com/anytypeio/go-anytype-middleware/pb protoc -I=. --gogofaster_out=$(PKGMAP),plugins=grpc:. ./pb/protos/service/service.proto; mv ./pb/protos/service/*.pb.go ./lib-debug/
|
||||
GOGO_NO_UNDERSCORE=1 GOGO_EXPORT_ONEOF_INTERFACE=1 GOGO_GRPC_SERVER_METHOD_NO_ERROR=1 GOGO_GRPC_SERVER_METHOD_NO_CONTEXT=1 PACKAGE_PATH=github.com/anytypeio/go-anytype-middleware/pb protoc -I=. --gogofaster_out=$(PKGMAP),plugins=grpc:. ./pb/protos/service/service.proto; mv ./pb/protos/service/*.pb.go ./lib-server/
|
||||
|
||||
protos: protos-deps
|
||||
$(eval P_TIMESTAMP := Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types)
|
||||
|
@ -103,18 +108,23 @@ protos: protos-deps
|
|||
protos-swift:
|
||||
protoc -I ./ --swift_opt=FileNaming=DropPath --swift_opt=Visibility=Internal --swift_out=./build/swift pb/protos/* vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*
|
||||
|
||||
protos-web:
|
||||
protoc -I ./ --js_out=import_style=commonjs:./build/web pb/protos/service/*.proto pb/protos/*.proto vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*
|
||||
protoc -I ./ --grpc-web_out=import_style=commonjs,mode=grpcwebtext:./build/web pb/protos/service/*.proto pb/protos/*.proto vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*
|
||||
npm run build:protos
|
||||
|
||||
protos-java:
|
||||
protoc -I ./ --java_out=./protobuf pb/protos/* vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*.proto
|
||||
|
||||
protos-ts:
|
||||
npm run build:ts
|
||||
|
||||
build-debug: protos-debug
|
||||
build-server: protos-server
|
||||
$(eval FLAGS := $$(shell govvv -flags -pkg github.com/anytypeio/go-anytype-middleware/core))
|
||||
go build -i -v -o dist/debug -ldflags "$(FLAGS)" ./lib-debug/server/grpc.go
|
||||
go build -i -v -o dist/server -ldflags "$(FLAGS)" ./lib-server/server/grpc.go
|
||||
|
||||
run-debug: build-debug
|
||||
./dist/debug
|
||||
run-server: build-server
|
||||
./dist/server
|
||||
|
||||
build-dev-js: setup protos-deps build-lib build-js protos-ts
|
||||
cp -r jsaddon/build ../js-anytype/
|
||||
|
|
1
build/web/.gitignore
vendored
Normal file
1
build/web/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
[^.]*
|
|
@ -7,7 +7,7 @@ import (
|
|||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/lib-debug"
|
||||
"github.com/anytypeio/go-anytype-middleware/lib-server"
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
)
|
||||
|
||||
|
|
1
go.mod
1
go.mod
|
@ -12,6 +12,7 @@ require (
|
|||
github.com/google/uuid v1.1.1
|
||||
github.com/h2non/filetype v1.0.12
|
||||
github.com/hashicorp/golang-lru v0.5.4
|
||||
github.com/improbable-eng/grpc-web v0.12.0
|
||||
github.com/mauidude/go-readability v0.0.0-20141216012317-2f30b1a346f1
|
||||
github.com/microcosm-cc/bluemonday v1.0.2
|
||||
github.com/otiai10/opengraph v1.1.1
|
||||
|
|
2
go.sum
2
go.sum
|
@ -74,6 +74,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018 h1:6xT9KW8zLC5IlbaIF5Q7JNieBoACT7iW0YTxQHR0in0=
|
||||
github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4=
|
||||
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f h1:U5y3Y5UE0w7amNe7Z5G/twsBW0KEalRQXZzf8ufSh9I=
|
||||
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f/go.mod h1:xH/i4TFMt8koVQZ6WFms69WAsDWr2XsYL3Hkl7jkoLE=
|
||||
github.com/dgraph-io/badger v1.5.5-0.20190226225317-8115aed38f8f/go.mod h1:VZxzAIRPHRVNRKRo6AXrX9BJegn6il06VMTZVJYCIjQ=
|
||||
github.com/dgraph-io/badger v1.6.0-rc1 h1:JphPpoBZJ3WHha133BGYlQqltSGIhV+VsEID0++nN9A=
|
||||
|
@ -179,6 +180,7 @@ github.com/huin/goupnp v0.0.0-20180415215157-1395d1447324/go.mod h1:MZ2ZmwcBpvOo
|
|||
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
|
||||
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
|
||||
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
|
||||
github.com/improbable-eng/grpc-web v0.12.0 h1:GlCS+lMZzIkfouf7CNqY+qqpowdKuJLSLLcKVfM1oLc=
|
||||
github.com/improbable-eng/grpc-web v0.12.0/go.mod h1:6hRR09jOEG81ADP5wCQju1z71g6OL4eEvELdran/3cs=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/ipfs/bbloom v0.0.1/go.mod h1:oqo8CVWsJFMOZqTglBG4wydCE4IQA/G2/SEofB0rjUI=
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
// +build !nogrpcserver,!_test
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core"
|
||||
"github.com/anytypeio/go-anytype-middleware/lib-debug"
|
||||
)
|
||||
|
||||
const defaultAddr = "127.0.0.1:9999"
|
||||
|
||||
func main() {
|
||||
var addr string
|
||||
if len(os.Args) > 1 {
|
||||
addr = os.Args[len(os.Args)-1]
|
||||
} else if env := os.Getenv("ANYTYPE_GRPC_ADDR"); env != "" {
|
||||
addr = env
|
||||
} else {
|
||||
addr = defaultAddr
|
||||
}
|
||||
|
||||
var stopChan = make(chan os.Signal, 2)
|
||||
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
var mw = &core.Middleware{}
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
grpcServer := grpc.NewServer()
|
||||
lib.RegisterClientCommandsServer(grpcServer, mw)
|
||||
|
||||
fmt.Println("gRPC server started at: " + addr)
|
||||
go func() {
|
||||
grpcServer.Serve(lis)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-stopChan:
|
||||
grpcServer.Stop()
|
||||
mw.Shutdown(&pb.RpcShutdownRequest{})
|
||||
return
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
104
lib-server/server/grpc.go
Normal file
104
lib-server/server/grpc.go
Normal file
|
@ -0,0 +1,104 @@
|
|||
// +build !nogrpcserver,!_test
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/pb"
|
||||
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/anytypeio/go-anytype-middleware/core"
|
||||
"github.com/anytypeio/go-anytype-middleware/lib-server"
|
||||
)
|
||||
|
||||
const defaultAddr = "127.0.0.1:31007"
|
||||
const defaultWebAddr = "127.0.0.1:31008"
|
||||
|
||||
func main() {
|
||||
var addr string
|
||||
var webaddr string
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
addr = os.Args[1]
|
||||
if len(os.Args) > 2 {
|
||||
webaddr = os.Args[2]
|
||||
}
|
||||
}
|
||||
|
||||
if addr == "" {
|
||||
if env := os.Getenv("ANYTYPE_GRPC_ADDR"); env != "" {
|
||||
addr = env
|
||||
} else {
|
||||
addr = defaultAddr
|
||||
}
|
||||
}
|
||||
|
||||
if webaddr == "" {
|
||||
if env := os.Getenv("ANYTYPE_GRPCWEB_ADDR"); env != "" {
|
||||
webaddr = env
|
||||
} else {
|
||||
webaddr = defaultWebAddr
|
||||
}
|
||||
}
|
||||
|
||||
var stopChan = make(chan os.Signal, 2)
|
||||
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
var mw = &core.Middleware{}
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to listen: %v", err)
|
||||
}
|
||||
|
||||
server := grpc.NewServer()
|
||||
lib.RegisterClientCommandsServer(server, mw)
|
||||
|
||||
webrpc := grpcweb.WrapServer(
|
||||
server,
|
||||
grpcweb.WithOriginFunc(func(origin string) bool {
|
||||
return true
|
||||
}),
|
||||
grpcweb.WithWebsockets(true),
|
||||
grpcweb.WithWebsocketOriginFunc(func(req *http.Request) bool {
|
||||
return true
|
||||
}))
|
||||
|
||||
proxy := &http.Server{
|
||||
Addr: webaddr,
|
||||
}
|
||||
proxy.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if webrpc.IsGrpcWebRequest(r) ||
|
||||
webrpc.IsAcceptableGrpcCorsRequest(r) ||
|
||||
webrpc.IsGrpcWebSocketRequest(r) {
|
||||
webrpc.ServeHTTP(w, r)
|
||||
}
|
||||
})
|
||||
|
||||
fmt.Println("gRPC server started at: " + addr)
|
||||
go func() {
|
||||
server.Serve(lis)
|
||||
}()
|
||||
|
||||
fmt.Println("gRPC Web proxy started at: " + webaddr)
|
||||
go func() {
|
||||
if err := proxy.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("proxy error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-stopChan:
|
||||
server.Stop()
|
||||
proxy.Close()
|
||||
mw.Shutdown(&pb.RpcShutdownRequest{})
|
||||
return
|
||||
}
|
||||
}
|
10
package.json
10
package.json
|
@ -4,9 +4,15 @@
|
|||
"description": "protobuf TS bindings for Anytype middleware library",
|
||||
"scripts": {
|
||||
"build:ts": "pbjs -t static-module -w commonjs -o build/ts/commands.js ./pb/protos/*.proto ./pb/protos/service/*.proto /service/*.proto ./vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*.proto && ./node_modules/.bin/pbts -o build/ts/commands.d.ts build/ts/commands.js ./vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*.proto",
|
||||
"build:tsmodel": "pbjs -t static-module -w commonjs -o build/ts/models.js ./vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*.proto && ./node_modules/.bin/pbts -o build/ts/models.d.ts ./vendor/github.com/anytypeio/go-anytype-library/pb/model/protos/*.proto"
|
||||
"build:protos": "webpack -o ./build/web/service.pb.js ./build/web/pb/protos/service/service_grpc_web_pb.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"protobufjs": "^6.8.8"
|
||||
"protobufjs": "^6.8.8",
|
||||
"google-protobuf": "^3.6.1",
|
||||
"grpc-web": "^0.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"webpack": "^4.16.5",
|
||||
"webpack-cli": "^3.3.12"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,6 +100,6 @@ service ClientCommands {
|
|||
|
||||
rpc ProcessCancel (anytype.Rpc.Process.Cancel.Request) returns (anytype.Rpc.Process.Cancel.Response);
|
||||
|
||||
// used only for lib-debug via grpc
|
||||
// used only for lib-server via grpc
|
||||
rpc ListenEvents (anytype.Empty) returns (stream anytype.Event);
|
||||
}
|
||||
|
|
12
webpack.config.js
Normal file
12
webpack.config.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
var path = require('path');
|
||||
|
||||
module.exports = {
|
||||
mode: 'none',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'build/web/'),
|
||||
filename: 'commands.pb.js'
|
||||
},
|
||||
optimization: {
|
||||
minimize: false
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue