Skip to content

Commit 08c8ebf

Browse files
ayjlizan
authored andcommitted
Add golang echo backend sample (istio#35)
* Add golang echo backend sample Add sample Go backend server so that nodejs version and dependency can be removed. Go is likely already present on build system due to other istio components usage. * Remove nodejs echo backend
1 parent ddb4d80 commit 08c8ebf

File tree

5 files changed

+79
-100
lines changed

5 files changed

+79
-100
lines changed

src/envoy/prototype/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ This Proxy will use Envoy and talk to Mixer server.
3333

3434
```
3535
cd test/backend/echo
36-
npm install
37-
node echo.js
36+
go run echo.go
3837
```
3938

4039
* Start Envoy proxy, run

test/backend/echo/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Simple echo server for esp testing.
1+
# Simple echo server for proxy testing.
22

3-
Node.js implementation of echo server.
3+
golang implementation of echo server.

test/backend/echo/echo.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2016 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// An example implementation of Echo backend in go.
17+
18+
package main
19+
20+
import (
21+
"flag"
22+
"fmt"
23+
"io/ioutil"
24+
"net/http"
25+
"strconv"
26+
"sync"
27+
"time"
28+
)
29+
30+
var (
31+
port = flag.Int("port", 8080, "default http port")
32+
33+
mu sync.Mutex
34+
requests = 0
35+
data = 0
36+
)
37+
38+
func handler(w http.ResponseWriter, r *http.Request) {
39+
body, err := ioutil.ReadAll(r.Body)
40+
if err != nil {
41+
http.Error(w, err.Error(), http.StatusInternalServerError)
42+
return
43+
}
44+
45+
// echo back the Content-Type and Content-Length in the response
46+
for _, k := range []string{"Content-Type", "Content-Length"} {
47+
if v := r.Header.Get(k); v != "" {
48+
w.Header().Set(k, v)
49+
}
50+
}
51+
w.WriteHeader(http.StatusOK)
52+
w.Write(body)
53+
54+
mu.Lock()
55+
requests++
56+
data += len(body)
57+
defer mu.Unlock()
58+
}
59+
60+
func main() {
61+
flag.Parse()
62+
63+
go func() {
64+
for {
65+
mu.Lock()
66+
fmt.Printf("Requests Requests: %v Data: %v\n", requests, data)
67+
mu.Unlock()
68+
time.Sleep(time.Second)
69+
}
70+
}()
71+
72+
fmt.Printf("Listening on port %v\n", *port)
73+
74+
http.HandleFunc("/", handler)
75+
http.ListenAndServe(":"+strconv.Itoa(*port), nil)
76+
}

test/backend/echo/echo.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

test/backend/echo/package.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)