proxy_server.go 1.6 KB
Newer Older
B
barriery 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

B
barriery 已提交
15 16 17
package main

import (
B
barriery 已提交
18
  "C"
B
barriery 已提交
19 20 21
  "flag"
  "net/http"
  "log"
B
barriery 已提交
22
  "strconv"
B
barriery 已提交
23 24 25 26 27

  "golang.org/x/net/context"
  "github.com/grpc-ecosystem/grpc-gateway/runtime"
  "google.golang.org/grpc"

M
MRXLT 已提交
28
  gw "serving-gateway/proto"
B
barriery 已提交
29 30
)

B
barriery 已提交
31
//export run_proxy_server
B
barriery 已提交
32 33 34 35 36
func run_proxy_server(grpc_port int, http_port int) error {
  var (
    pipelineEndpoint = flag.String("pipeline_endpoint", "localhost:" + strconv.Itoa(grpc_port), "endpoint of PipelineService")
  )

B
barriery 已提交
37 38 39 40
  ctx := context.Background()
  ctx, cancel := context.WithCancel(ctx)
  defer cancel()

T
TeslaZhao 已提交
41 42
  //EmitDefaults=true, does not filter out the default inputs 
  mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
B
barriery 已提交
43 44 45 46 47 48
  opts := []grpc.DialOption{grpc.WithInsecure()}
  err := gw.RegisterPipelineServiceHandlerFromEndpoint(ctx, mux, *pipelineEndpoint, opts)
  if err != nil {
    return err
  }

B
barriery 已提交
49
  log.Println("start proxy service")
B
barriery 已提交
50
  return http.ListenAndServe(":" + strconv.Itoa(http_port), mux) // proxy port
B
barriery 已提交
51 52
}

B
barriery 已提交
53
func main() {}