Coder Social home page Coder Social logo

go-rest-server's Introduction

go-rest-server

What is this?

A simple HTTP server written in Go using a RESTful style API to interact with it.

It doesn't do much, just demonstrates the layout of an Go HTTP server, and might be a good starting point.

Features

  • Uses Gorilla web toolkit for routing, and middleware for logging requests.
  • POST body is limited using http.MaxBytesReader, so the server isn't overloaded from malicious requests.
  • Configurable options for address, port, timeouts, max body size, and performance debugging.

Usage

Starting the server

Using docker-compose:

docker-compose up -d

Alternatively, you can run make build which places binaries for Linux, macOS, and Windows in the build directory.

Run the resulting binaries directly with -h to see command line options!

Example API usage

View data by GETing the index route /:

curl http://localhost:8000

Create some data by POSTing to the resource route:

curl http://localhost:8000/resource -d '{"Name":"Bob"}' -H "Content-Type: application/json"

View data by GETing the resource/{id} route:

curl http://localhost:8000/resource/1

What now?

There's a lot more to be added to make this a real application! Here's a few examples:

  • Access data from a database, or some other pluggable source (e.g. for testing)
  • Check for data, output HTTP status not found if missing
  • Add support for DELETE requests
  • Authentication
  • Versioned API, e.g. https://mysite.com/v1/resource/1
  • Use some kind of standard specification, e.g. OpenAPI or JSON API
  • Validate POST data matches expectations, e.g. correct fields provided
  • Tests

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.