Skip to content
Snippets Groups Projects
Select Git revision
  • 7b35b47747bb234863092de2b2e71a78ac44c5db
  • main default protected
  • dev protected
  • prod protected
  • t-6-deploy-app-on-kubernetes
  • t-5-allocate-vm-cloud
  • t4-build-publish-container
  • t3-build-container-image
  • t2-vagrant
9 results

webservice

  • Clone with SSH
  • Clone with HTTPS
  • Forked from DevOps (Lecture FB VI) / webservice
    21 commits behind the upstream repository.
    gjahn's avatar
    gjahn authored
    Somebody havent heard about SemVer yet. The Ctx.GetReqHeaders() return value
    slightly changed (string to []string), which now requires to first join the slice
    before checking whether it contains the given string
    7b35b477
    History

    Webservice

    A Go-based simple web service meant to be the subject of any tutorial or even used the project work.

    Prerequisites:

    • Go toolchain (install via system package manager or by hand)
    • [optional] Redis to persist state

    State:

    If the database host is not explicitly defined, then the state is ephemeral. For more information checkout the configuration code.

    Build:

    1. Install dependencies: go get -t ./...
    2. Run locally: go run .
    3. Execute unit tests: go test -race -v ./...
    4. Build artifact: go build -o ./artifact.bin ./*.go

    To build for another platform, set GOOS and GOARCH. To yield a static binary (fully self-contained, no dynamic linking) set CGO_ENABLED=0. For more details, please refer to the Makefile.

    Run:

    HOST=0.0.0.0 PORT=8080 ./artifact.bin

    Interact:

    Landing page

    plain text:

    curl http://localhost:8080

    HTML:

    curl --header 'Content-Type: text/html; charset=utf-8' http://localhost:8080
    # or just open in a browser
    Health check
    curl http://localhost:8080/health
    Server side environment variables

    List environment variables visible by the webservice process if environment is not production.

    curl http://localhost:8080/env
    State life cycle

    URL slug is used as identifier and the body is the actual data being stored. Please note, when writing (add or change) something, Content-Type must be set in the request header.

    Write an entry:

    curl \
      -X PUT \
      --header 'Content-Type: text/plain; charset=utf-8' \
      --data 'foo' \
      http://localhost:8080/state/bar

    Obtain an entry:

    curl \
      -X GET \
      http://localhost:8080/state/bar

    Remove an entry:

    curl \
      -X DELETE \
      --verbose \
      http://localhost:8080/state/bar

    List all existing entries (returns JSON or plain text, depending on the Accept header):

    curl \
      -X GET
      --header 'Accept: text/plain'\
      http://localhost:8080/states

    Upload an entire file:

    curl \
      -X PUT \
      --header 'Content-Type: application/pdf' \
      --upload-file ./example.pdf \
      http://localhost:8080/state/pdf-doc

    Download a file:

    curl \
      -X GET \
      --output ./example-copy.pdf \
      http://localhost:8080/state/pdf-doc