Code Monkey home page Code Monkey logo

go-requester's Introduction

Go-Requester

HTTP service that accepts a collection of "components"
then fans-out requests and returns aggregated content

Summary

  • Components should be defined in a YAML page configuration file
  • Components are requested concurrently via goroutines
  • Components can be marked as "mandatory" (if they fail, the request summary is set to "failure")

Example Page Config

components:
  - id: google
    url: http://google.com
  - id: integralist
    url: http://integralist.co.uk
    mandatory: true
  - id: not-found
    url: http://httpstat.us/404
  - id: timeout
    url: http://httpstat.us/408
  - id: server-error
    url: http://httpstat.us/500
  - id: service-unavailable
    url: http://httpstat.us/503

Note: example config provided as part of this repo

Example JSON Output

{
  "summary": "success",
  "components": [
    {
      "id": "google",
      "status": 200,
      "body": "<!doctype html> ... </html>",
      "mandatory": false
    },
    {
      "id": "integralist",
      "status": 200,
      "body": "<!doctype html> ... </html>",
      "mandatory": true
    },
    {
      "id": "slooow",
      "status": 408,
      "body": "Get http://localhost:3000/pugs: net/http: request canceled (Client.Timeout exceeded while awaiting headers)",
      "mandatory": false
    }
    {
      "id": "not-found",
      "status": 404,
      "body": "404 Not Found",
      "mandatory": false
    }
  ]
}

Note: the toplevel summary key's value will be failure if any mandatory components fail

Run within Docker

  • docker build -t my-golang-app .
  • docker run --rm --name go-tester -v "$PWD":/go/src/app -p 8080:8080 my-golang-app

If you're using Docker-Machine, then executing the following command will return the results of the running application:

curl $(docker-machine ip <name_of_vm>):8080

To test what's happening inside the container then execute the following command:

docker run -it -v "$PWD":/go/src/app -p 8080:8080 my-golang-app /bin/bash

Private Repos

If you need to use a private repo then I've set-up the Docker container to work with them, but it does require you to pick your poison:

  • Run the container interactively (-it) and manually enter SSH private key passphrase

OR

  • Run the container interactively (-it) and save your SSH private key passphrase into an expect script

The former is safer and so it's the default option.

The latter requires you to modify the ssh.exp file to include your passphrase and also to uncomment ssh-add /go/src/app/github_rsa from bootstrap.sh and put back in /ssh.exp. But it's your responsibility to make sure you don't commit that passphrase.

Yes the latter takes a few more steps, but there was no way I was going to automate that for you and make it easy for you to shoot yourself in the foot!

Regardless of which option you choose, you'll need to modify the Docker run command slightly (to use interactive mode -it). I'm not sure why though, if anyone has any ideas then please let me know.

So the command you need to execute, if you're pulling private repositories, is:

docker run -it -v "$HOME/.ssh/github_rsa":/go/src/app/github_rsa -v "$PWD":/go/src/app -p 8080:8080 my-golang-app

Note: I initially added both github_rsa and id_rsa to the .gitignore file and then switched to *_rsa to try and prevent as many mistakes as possible (i.e. avoiding committing your private key to a potentially public repo); so please do be careful if your private key uses a different name!

In Summary...

No, I'm not specifying any private repo dependencies:

docker run --rm -v "$PWD":/go/src/app -p 8080:8080 my-golang-app

Yes, I have some dependencies coming from private repositories:

docker run -it -v "$HOME/.ssh/github_rsa":/go/src/app/github_rsa -v "$PWD":/go/src/app -p 8080:8080 my-golang-app "ssh_setup"
  • run interactively (-it mode)
  • mount your ssh private key
  • specify ssh_setup

Build and run binary on host machine

The following command only needs to be run once (it downloads the gb tool):

go get -u github.com/constabulary/gb/...

Once you have gb, download the dependencies (specified within the vendor/manifest file) using:

gb vendor restore

Every time you make a change to your code, run:

gb build all && bin/requester ./src/page.yaml

Note: for other OS' and Arch's use something like GOOS=linux GOARCH=amd64 gb build

Run application locally on host machine

  • go run src/requester/main.go src/page.yaml
  • curl http://localhost:8080/ (better to check via a web browser)

Note: you can also use godo watch-server --watch to track changes and automatically re-run

If you want to see how latency/slow responses effect the application then try also running: https://github.com/Integralist/go-slow-http-server which was specifically designed to be used alongside go-requester for testing

Curl Timings

  • curl -w "@curl-format.txt" -o /dev/null -s http://localhost:8080/

Note: I've included a curl-format.txt file within the repo

Dependencies

I use http://getgb.io/ for handling dependencies. When using gb vendor fetch <pkg> it'll place dependencies into a vendor directory for you and thus allow gb build all to include them within your binary. So you gain a project specific workspace without affecting your global $GOPATH.

In order to not have a large repo (consisting of many dependency files), we .gitignore the vendor/src directory but we still commit the vendor/manifest file (which acts as a dependency lockfile). This means when pulling the repo for the first time you'd need to execute gb vendor restore.

TODO

  • Refactor code so that certain aspects are loaded from other packages
  • Add logic for loading page config remotely
  • Dynamically change port number when run as binary
  • Tests!

Licence

The MIT License (MIT)

Copyright (c) 2015 Mark McDonnell

go-requester's People

Contributors

integralist avatar karlhorky avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.