Skip to content

Commit 2f131ec

Browse files
committed
Add sample Sinatra app and Golang binary sidecar
To be referenced in an upcoming Cloud Foundry blog post. Inspired by some test apps in the capi-bara-tests acceptance test suite: cloudfoundry/capi-bara-tests@7a0c511 [#165355813](https://www.pivotaltracker.com/story/show/165355813) Authored-by: Tim Downey <[email protected]>
1 parent fc13a40 commit 2f131ec

File tree

14 files changed

+204
-0
lines changed

14 files changed

+204
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
# capi-sidecar-samples
22

33
Sample apps demonstrating how to use [sidecar processes](http://v3-apidocs.cloudfoundry.org/version/release-candidate/#sidecars) in Cloud Foundry.
4+
5+
## Apps
6+
This repository currently contains the following sample apps and sidecars.
7+
8+
You can quickly deploy both apps by targeting your Cloud Foundry api using the `cf cli` and running the `push_sample_app_with_sidecar.sh` script.
9+
10+
### config-server-sidecar
11+
A simple Golang binary that emulates a "configuration server" for the parent app to call out to.
12+
13+
### sidecar-dependent-app
14+
A simple Sinatra app that calls out to the `config-server-sidecar` binary and echoes back its response.

config-server-sidecar/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config-server

config-server-sidecar/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Sample "Config Server" Sidecar Process
2+
3+
This is a simple Golang "config server" designed to be used along with the sample `sidecar-dependent-app`.
4+
It listens on `localhost:$CONFIG_SERVER_PORT/config/` and returns some hard coded "configuration" for the parent app to consume.
5+
6+
7+
## Building the binary
8+
```
9+
GOOS=linux GOARCH=amd64 go build -o config-server .
10+
```
11+
12+
## Running the config server
13+
```
14+
CONFIG_SERVER_PORT=8082 ./config-server
15+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"os"
8+
)
9+
10+
func main() {
11+
// Emulate an external configuration service
12+
http.HandleFunc("/config/", config)
13+
fmt.Println("listening...")
14+
err := http.ListenAndServe(":"+os.Getenv("CONFIG_SERVER_PORT"), nil)
15+
if err != nil {
16+
panic(err)
17+
}
18+
}
19+
20+
type Config struct {
21+
Scope string
22+
Password string
23+
}
24+
25+
func config(res http.ResponseWriter, req *http.Request) {
26+
config := Config{"some-service.admin", "not-a-real-p4$$w0rd"}
27+
28+
js, err := json.Marshal(config)
29+
if err != nil {
30+
panic(err)
31+
}
32+
33+
fmt.Println("Received a request for config.")
34+
fmt.Fprintln(res, string(js))
35+
}

config-server-sidecar/manifest.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
applications:
3+
- env:
4+
GOPACKAGENAME: config-server

push_sample_app_with_sidecar.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
SAMPLE_APP_PATH="sidecar-dependent-app"
4+
CONFIG_SERVER_SIDECAR_PATH="config-server-sidecar"
5+
6+
function clean() {
7+
rm "${SAMPLE_APP_PATH}/config-server"
8+
rm "${CONFIG_SERVER_SIDECAR_PATH}/config-server"
9+
}
10+
11+
function build_config_server() {
12+
pushd ${CONFIG_SERVER_SIDECAR_PATH} > /dev/null
13+
GOOS=linux GOARCH=amd64 go build -o config-server .
14+
popd > /dev/null
15+
cp "${CONFIG_SERVER_SIDECAR_PATH}/config-server" ${SAMPLE_APP_PATH}
16+
}
17+
18+
function push_app_with_sidecars() {
19+
pushd ${SAMPLE_APP_PATH} > /dev/null
20+
cf v3-create-app sidecar-dependent-app
21+
cf v3-apply-manifest -f manifest.yml
22+
cf v3-push sidecar-dependent-app
23+
popd > /dev/null
24+
}
25+
26+
function main() {
27+
clean
28+
build_config_server
29+
push_app_with_sidecars
30+
}
31+
32+
main

sidecar-dependent-app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
config-server

sidecar-dependent-app/Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source "http://rubygems.org"
2+
3+
gem "sinatra"
4+
gem "json"
5+
gem "typhoeus"
6+
7+
group :test, :development do
8+
gem "rspec"
9+
gem "rack-test"
10+
end

sidecar-dependent-app/Gemfile.lock

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
diff-lcs (1.3)
5+
ethon (0.12.0)
6+
ffi (>= 1.3.0)
7+
ffi (1.10.0)
8+
json (2.1.0)
9+
mustermann (1.0.3)
10+
rack (2.0.6)
11+
rack-protection (2.0.4)
12+
rack
13+
rack-test (0.8.3)
14+
rack (>= 1.0, < 3)
15+
rspec (3.7.0)
16+
rspec-core (~> 3.7.0)
17+
rspec-expectations (~> 3.7.0)
18+
rspec-mocks (~> 3.7.0)
19+
rspec-core (3.7.1)
20+
rspec-support (~> 3.7.0)
21+
rspec-expectations (3.7.0)
22+
diff-lcs (>= 1.2.0, < 2.0)
23+
rspec-support (~> 3.7.0)
24+
rspec-mocks (3.7.0)
25+
diff-lcs (>= 1.2.0, < 2.0)
26+
rspec-support (~> 3.7.0)
27+
rspec-support (3.7.1)
28+
sinatra (2.0.4)
29+
mustermann (~> 1.0)
30+
rack (~> 2.0)
31+
rack-protection (= 2.0.4)
32+
tilt (~> 2.0)
33+
tilt (2.0.9)
34+
typhoeus (1.3.1)
35+
ethon (>= 0.9.0)
36+
37+
PLATFORMS
38+
ruby
39+
40+
DEPENDENCIES
41+
json
42+
rack-test
43+
rspec
44+
sinatra
45+
typhoeus
46+
47+
BUNDLED WITH
48+
1.17.3

sidecar-dependent-app/Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: bundle exec rackup config.ru -p $PORT

0 commit comments

Comments
 (0)