diff --git a/src/envoy/prototype/README.md b/src/envoy/prototype/README.md index 5b4e85bfb4d..ad7eba84287 100644 --- a/src/envoy/prototype/README.md +++ b/src/envoy/prototype/README.md @@ -33,8 +33,7 @@ This Proxy will use Envoy and talk to Mixer server. ``` cd test/backend/echo - npm install - node echo.js + go run echo.go ``` * Start Envoy proxy, run diff --git a/test/backend/echo/README.md b/test/backend/echo/README.md index b3d4ee11032..8df2854c5f3 100644 --- a/test/backend/echo/README.md +++ b/test/backend/echo/README.md @@ -1,3 +1,3 @@ -# Simple echo server for esp testing. +# Simple echo server for proxy testing. -Node.js implementation of echo server. +golang implementation of echo server. diff --git a/test/backend/echo/echo.go b/test/backend/echo/echo.go new file mode 100644 index 00000000000..51aaa07e353 --- /dev/null +++ b/test/backend/echo/echo.go @@ -0,0 +1,76 @@ +// Copyright 2016 Google Inc. 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. +// + +// An example implementation of Echo backend in go. + +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "sync" + "time" +) + +var ( + port = flag.Int("port", 8080, "default http port") + + mu sync.Mutex + requests = 0 + data = 0 +) + +func handler(w http.ResponseWriter, r *http.Request) { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + // echo back the Content-Type and Content-Length in the response + for _, k := range []string{"Content-Type", "Content-Length"} { + if v := r.Header.Get(k); v != "" { + w.Header().Set(k, v) + } + } + w.WriteHeader(http.StatusOK) + w.Write(body) + + mu.Lock() + requests++ + data += len(body) + defer mu.Unlock() +} + +func main() { + flag.Parse() + + go func() { + for { + mu.Lock() + fmt.Printf("Requests Requests: %v Data: %v\n", requests, data) + mu.Unlock() + time.Sleep(time.Second) + } + }() + + fmt.Printf("Listening on port %v\n", *port) + + http.HandleFunc("/", handler) + http.ListenAndServe(":"+strconv.Itoa(*port), nil) +} diff --git a/test/backend/echo/echo.js b/test/backend/echo/echo.js deleted file mode 100644 index b1d0028f1c8..00000000000 --- a/test/backend/echo/echo.js +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2016 Google Inc. 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. -// -//////////////////////////////////////////////////////////////////////////////// -// -// An example implementation of Echo backend. - -"use strict"; - -var http = require('http'); -var server = http.createServer(); - -var totalReceived = 0; -var totalData = 0; - -server.on('request', function(req, res) { - totalReceived += 1; - var method = req.method; - var url = req.url; - if (method == 'GET' && url == '/version') { - res.writeHead(200, {"Content-Type": "application/json"}); - res.write('{"version":"${VERSION}"}'); - res.end(); - return; - } - req.on('data', function(chunk) { - totalData += chunk.length; - res.write(chunk); - }) - req.on('end', function() { - res.end(); - }); - - var cl = req.headers['content-length']; - var ct = req.headers['content-type']; - - var headers = {}; - if (cl !== undefined) { - headers['Content-Length'] = cl; - } - if (ct !== undefined) { - headers['Content-Type'] = ct; - } - - res.writeHead(200, headers); - req.resume(); -}); - -var totalConnection = 0; - -server.on('connection', function(socket) { - totalConnection += 1; -}); - -setInterval(function() { - console.log("Requests received:", totalReceived, " Data: ", totalData, " Connection: ", totalConnection); -}, 1000); - -var port = process.env.PORT || 8080; - -server.listen(port, function() { - console.log('Listening on port', port); -}); diff --git a/test/backend/echo/package.json b/test/backend/echo/package.json deleted file mode 100644 index 38bf8b4d3a3..00000000000 --- a/test/backend/echo/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "echo", - "version": "8.8.1", - "description": "An echo server.", - "main": "echo.js", - "readme": "README.md", - "scripts": { - "start": "node echo.js" - }, - "repository": { - "type": "git", - "url": "https://github.com/istio/proxy" - }, - "license": "Apache-2.0", - "engines" : { - "node" : ">=4.1.2" - }, - "files": [ - "README.md", - "echo.js" - ] -}