Skip to content

Commit edc7ecf

Browse files
authored
add http gateway (#167)
1 parent cd6f534 commit edc7ecf

File tree

9 files changed

+745
-12
lines changed

9 files changed

+745
-12
lines changed

.github/workflows/release.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Build and publish gnetcli-server
22

33
env:
4-
GORELEASER_VER: v1.20.0
4+
GORELEASER_VER: v1.20.4
55

66
on:
77
release:
@@ -44,7 +44,7 @@ jobs:
4444
- name: Set up Go
4545
uses: actions/setup-go@v4
4646
with:
47-
go-version: '1.20.0'
47+
go-version: '1.20.4'
4848
- name: Login to github container registry
4949
uses: docker/login-action@v3
5050
with:

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ proto:
2020
--go_out=./pkg/server/proto/ \
2121
--go-grpc_out=./pkg/server/proto/ \
2222
'./pkg/server/proto/server.proto'
23+
docker run --rm -v `pwd`:/home/docker/app --workdir /home/docker/app proto_builder:tag \
24+
protoc -I ./pkg/server/proto/ --grpc-gateway_out ./pkg/server/proto/ \
25+
--grpc-gateway_opt paths=source_relative \
26+
--grpc-gateway_opt generate_unbound_methods=true \
27+
pkg/server/proto/server.proto
2328
docker run --rm -v `pwd`:/home/docker/app --workdir /home/docker/app proto_builder:tag \
2429
$(protoc_cmd) -I ./pkg/server/proto/ \
2530
--python_out=./pkg/server/proto/ \

cmd/gnetcli_server/server.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ import (
44
"context"
55
"log"
66
"net"
7+
"net/http"
78
"os"
89
"os/signal"
910
"path/filepath"
1011
"runtime"
1112
"strings"
1213
"syscall"
1314

15+
gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
16+
1417
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
1518
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
1619
"go.uber.org/zap"
1720
"golang.org/x/sync/errgroup"
1821
"google.golang.org/grpc"
1922
"google.golang.org/grpc/credentials"
23+
"google.golang.org/grpc/credentials/insecure"
2024
"google.golang.org/grpc/reflection"
2125

2226
gcred "github.com/annetutil/gnetcli/pkg/credentials"
@@ -59,7 +63,7 @@ func main() {
5963
if cfg.DevUseAgent {
6064
cfg.DevAuth.UseAgent = cfg.DevUseAgent
6165
}
62-
var listeners []net.Listener
66+
var grpcListeners []net.Listener
6367

6468
logConfig = zap.NewDevelopmentConfig()
6569
if cfg.Logging.Json {
@@ -74,8 +78,9 @@ func main() {
7478
if err != nil {
7579
logger.Panic("unix socket error", zap.Error(err))
7680
}
77-
listeners = append(listeners, unixSocketLn)
81+
grpcListeners = append(grpcListeners, unixSocketLn)
7882
}
83+
var gatewayServer *http.Server
7984
if !cfg.DisableTcp {
8085
address := cfg.Listen
8186
if !strings.Contains(cfg.Listen, ":") { // just port
@@ -86,9 +91,15 @@ func main() {
8691
logger.Panic("tcp socket error", zap.Error(err))
8792
}
8893
logger.Debug("init tcp socket", zap.String("address", tcpSocketLn.Addr().String()))
89-
listeners = append(listeners, tcpSocketLn)
94+
grpcListeners = append(grpcListeners, tcpSocketLn)
95+
if cfg.HttpListen != "" {
96+
logger.Debug("init http gateway socket", zap.String("address", cfg.HttpListen))
97+
mux := gateway.NewServeMux()
98+
pb.RegisterGnetcliHandlerFromEndpoint(context.Background(), mux, address, []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())})
99+
gatewayServer = &http.Server{Addr: cfg.HttpListen, Handler: mux}
100+
}
90101
}
91-
if len(listeners) == 0 {
102+
if len(grpcListeners) == 0 {
92103
logger.Panic("specify tcp or unix socket")
93104
}
94105
var opts []grpc.ServerOption
@@ -134,7 +145,7 @@ func main() {
134145
reflection.Register(grpcServer)
135146
ctx := context.Background()
136147
wg, wCtx := errgroup.WithContext(ctx)
137-
for _, listener := range listeners {
148+
for _, listener := range grpcListeners {
138149
wListener := listener
139150
wg.Go(func() error {
140151
return grpcServer.Serve(wListener)
@@ -145,6 +156,11 @@ func main() {
145156
return nil
146157
})
147158
}
159+
if gatewayServer != nil {
160+
wg.Go(func() error {
161+
return gatewayServer.ListenAndServe()
162+
})
163+
}
148164
wg.Go(func() error {
149165
err := WaitInterrupted(wCtx)
150166
logger.Debug("WaitInterrupted", zap.Error(err))

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.20
44

55
require (
66
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
7+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0
78
github.com/heetch/confita v0.0.0-20181011080120-653cbec9ecff
89
github.com/kevinburke/ssh_config v1.2.0
910
github.com/mitchellh/go-homedir v1.1.0
@@ -14,8 +15,10 @@ require (
1415
go.uber.org/zap v1.26.0
1516
golang.org/x/crypto v0.14.0
1617
golang.org/x/exp v0.0.0-20230725093048-515e97ebf090
18+
golang.org/x/net v0.16.0
1719
golang.org/x/sync v0.4.0
1820
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97
21+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97
1922
google.golang.org/grpc v1.58.2
2023
google.golang.org/protobuf v1.31.0
2124
gopkg.in/yaml.v3 v3.0.1
@@ -27,10 +30,8 @@ require (
2730
github.com/golang/protobuf v1.5.3 // indirect
2831
github.com/kr/fs v0.1.0 // indirect
2932
github.com/pmezard/go-difflib v1.0.0 // indirect
30-
golang.org/x/net v0.16.0 // indirect
3133
golang.org/x/sys v0.13.0 // indirect
3234
golang.org/x/text v0.13.0 // indirect
3335
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
34-
google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect
3536
gopkg.in/yaml.v2 v2.2.8 // indirect
3637
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
1818
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
1919
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
2020
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
21+
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
2122
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
2223
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2324
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -30,6 +31,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3031
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
3132
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
3233
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
34+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q86mfnu7NAeHfte7A=
35+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0/go.mod h1:YDZoGHuwE+ov0c8smSH49WLF3F2LaWnYYuDVd+EWrc0=
3336
github.com/heetch/confita v0.0.0-20181011080120-653cbec9ecff h1:W7AlaRcJDMoRCcUFYyDGwP/3gt7+ob8QYArF9Klb0Zw=
3437
github.com/heetch/confita v0.0.0-20181011080120-653cbec9ecff/go.mod h1:S8Em4kuK8pR5vfTiaNkFLfNDMlGF/EtQUaCxDhXRpCs=
3538
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=

pkg/server/conf.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
)
1919

2020
type Config struct {
21-
Logging LogConfig `yaml:"logging"`
22-
Listen string `config:"port,description=Listen address" yaml:"port"`
21+
Logging LogConfig `yaml:"logging"`
22+
Listen string `config:"port,description=Listen address" yaml:"port"`
23+
HttpListen string `config:"http_port,description=Http listen address" yaml:"http_port"`
2324
// FIXME: Dev* in DevAuth, drop it
2425
DevLogin string `config:"dev-login,description=Default device login" yaml:"dev_login"`
2526
DevPass string `config:"dev-pass,description=Default device password" yaml:"dev_pass"`

0 commit comments

Comments
 (0)