Code Monkey home page Code Monkey logo

counter-ctrl's Introduction

Counter Controller

Golang server with API interface for controlling an external Counter program

Installing Go

It might be easiest to download a Golang Docker image and use that for development:

docker pull golang:1.10

You can find other available tags on the docker hub page

Launching the Docker container

Set HOME so we can use the local cache in ~/.cache/go-build

chex -i golang:1.10 -a "-e HOME=/opt/shared/${USER}" bash

Dependencies

We need the mux library to route requests

go get github.com/gorilla/mux

Building and Running the Binary

make all
make run

Starting and Stopping

Use curl to send start and stop commands to the go server process

$ curl -v -s -X POST -H "Content-Type: application/json" -d '{"start":"8"}' localhost:4723/start
*   Trying ::1...
* Connected to localhost (::1) port 4723 (#0)
> POST /start HTTP/1.1
> Host: localhost:4723
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 13
> 
* upload completely sent off: 13 out of 13 bytes
< HTTP/1.1 201 Created
< Content-Type: application/json
< Date: Fri, 20 Jul 2018 04:18:43 GMT
< Content-Length: 11
< 
* Connection #0 to host localhost left intact
{"pid":645}
$
$
$
$ curl -v -s -X GET localhost:4723/stop
*   Trying ::1...
* Connected to localhost (::1) port 4723 (#0)
> GET /stop HTTP/1.1
> Host: localhost:4723
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Fri, 20 Jul 2018 04:19:10 GMT
< Content-Length: 17
< 
* Connection #0 to host localhost left intact
{"status":"done"}

Useful articles:

  1. Learning Go: the tour
  2. Building a RESTful API with Golang
  3. Starting a command: func (*Cmd) Start
  4. Killing a child process and all of its children in Go
  5. Building and Testing a REST API in Go with Gorilla Mux and PostgreSQL

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.