Skip to content

Commit

Permalink
Add sample Sinatra app and Golang binary sidecar
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
tcdowney committed Apr 17, 2019
1 parent fc13a40 commit 2f131ec
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# capi-sidecar-samples

Sample apps demonstrating how to use [sidecar processes](http://v3-apidocs.cloudfoundry.org/version/release-candidate/#sidecars) in Cloud Foundry.

## Apps
This repository currently contains the following sample apps and sidecars.

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.

### config-server-sidecar
A simple Golang binary that emulates a "configuration server" for the parent app to call out to.

### sidecar-dependent-app
A simple Sinatra app that calls out to the `config-server-sidecar` binary and echoes back its response.
1 change: 1 addition & 0 deletions config-server-sidecar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config-server
15 changes: 15 additions & 0 deletions config-server-sidecar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Sample "Config Server" Sidecar Process

This is a simple Golang "config server" designed to be used along with the sample `sidecar-dependent-app`.
It listens on `localhost:$CONFIG_SERVER_PORT/config/` and returns some hard coded "configuration" for the parent app to consume.


## Building the binary
```
GOOS=linux GOARCH=amd64 go build -o config-server .
```

## Running the config server
```
CONFIG_SERVER_PORT=8082 ./config-server
```
35 changes: 35 additions & 0 deletions config-server-sidecar/config_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"os"
)

func main() {
// Emulate an external configuration service
http.HandleFunc("/config/", config)
fmt.Println("listening...")
err := http.ListenAndServe(":"+os.Getenv("CONFIG_SERVER_PORT"), nil)
if err != nil {
panic(err)
}
}

type Config struct {
Scope string
Password string
}

func config(res http.ResponseWriter, req *http.Request) {
config := Config{"some-service.admin", "not-a-real-p4$$w0rd"}

js, err := json.Marshal(config)
if err != nil {
panic(err)
}

fmt.Println("Received a request for config.")
fmt.Fprintln(res, string(js))
}
4 changes: 4 additions & 0 deletions config-server-sidecar/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
applications:
- env:
GOPACKAGENAME: config-server
32 changes: 32 additions & 0 deletions push_sample_app_with_sidecar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

SAMPLE_APP_PATH="sidecar-dependent-app"
CONFIG_SERVER_SIDECAR_PATH="config-server-sidecar"

function clean() {
rm "${SAMPLE_APP_PATH}/config-server"
rm "${CONFIG_SERVER_SIDECAR_PATH}/config-server"
}

function build_config_server() {
pushd ${CONFIG_SERVER_SIDECAR_PATH} > /dev/null
GOOS=linux GOARCH=amd64 go build -o config-server .
popd > /dev/null
cp "${CONFIG_SERVER_SIDECAR_PATH}/config-server" ${SAMPLE_APP_PATH}
}

function push_app_with_sidecars() {
pushd ${SAMPLE_APP_PATH} > /dev/null
cf v3-create-app sidecar-dependent-app
cf v3-apply-manifest -f manifest.yml
cf v3-push sidecar-dependent-app
popd > /dev/null
}

function main() {
clean
build_config_server
push_app_with_sidecars
}

main
1 change: 1 addition & 0 deletions sidecar-dependent-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config-server
10 changes: 10 additions & 0 deletions sidecar-dependent-app/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source "http://rubygems.org"

gem "sinatra"
gem "json"
gem "typhoeus"

group :test, :development do
gem "rspec"
gem "rack-test"
end
48 changes: 48 additions & 0 deletions sidecar-dependent-app/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.3)
ethon (0.12.0)
ffi (>= 1.3.0)
ffi (1.10.0)
json (2.1.0)
mustermann (1.0.3)
rack (2.0.6)
rack-protection (2.0.4)
rack
rack-test (0.8.3)
rack (>= 1.0, < 3)
rspec (3.7.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
sinatra (2.0.4)
mustermann (~> 1.0)
rack (~> 2.0)
rack-protection (= 2.0.4)
tilt (~> 2.0)
tilt (2.0.9)
typhoeus (1.3.1)
ethon (>= 0.9.0)

PLATFORMS
ruby

DEPENDENCIES
json
rack-test
rspec
sinatra
typhoeus

BUNDLED WITH
1.17.3
1 change: 1 addition & 0 deletions sidecar-dependent-app/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: bundle exec rackup config.ru -p $PORT
6 changes: 6 additions & 0 deletions sidecar-dependent-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Sample Sidecar-Dependent App

This is a simple Sinatra-based Ruby app that calls out to a co-located `config-server` sidecar process to retrieve its "configuration."

## Endpoints
* `GET /config` will echo back any configuration retrieved from the `config-server` sidecar
23 changes: 23 additions & 0 deletions sidecar-dependent-app/app_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ENV['RACK_ENV'] ||= 'development'

require 'rubygems'
require 'sinatra/base'
require 'json'
require 'typhoeus'

Bundler.require :default, ENV['RACK_ENV'].to_sym

class AppServer < Sinatra::Base
get '/' do
"Hi, I'm an app with a sidecar!"
end

get '/config' do
puts "Sending a request to the config-server sidecar at localhost:#{ENV['CONFIG_SERVER_PORT']}/config/"
response = Typhoeus.get("localhost:#{ENV['CONFIG_SERVER_PORT']}/config/")
puts "Received #{response.body} from the config-server sidecar"
response.body
end

run! if app_file == $0
end
4 changes: 4 additions & 0 deletions sidecar-dependent-app/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$: << File.expand_path("../.", __FILE__)

require "app_server"
run AppServer
13 changes: 13 additions & 0 deletions sidecar-dependent-app/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
applications:
- name: sidecar-dependent-app
disk_quota: 1G
instances: 1
memory: 256M
env:
CONFIG_SERVER_PORT: 8082
stack: cflinuxfs3
sidecars:
- name: config-server
process_types:
- web
command: './config-server'

0 comments on commit 2f131ec

Please sign in to comment.