Git Product home page Git Product logo

gopi's Introduction

GOPI

Go makes starting HTTP servers and spawning API endpoints super straightforward. For a simple servers, it's totally easy peasy. For bigger projects, however, which may require a server with many endpoints e.g. REST endpoints for 10-20 entities, it quickly becomes painful to manage existing and bring up new endpoints. At least,that's the pain I went through.

GOPI is a library to make that pain go away, and make HTTP endpoints management super manageable. There are a few simple concepts:

Route

A Route is an entire specification required to setup a particular endpoint. It basically needs four things:

  • Method (string): GET, POST, PUT, DELETE etc.
  • Path (string): The endpoint for this particular API
  • Version (int): Allows us to version the particular route by pre-pending "v1" or "v2" etc. to the path.
  • HandlerFunc (http.HandlerFunc): The Main handler function for this route
  • Authenticate (bool): Optional - if passed as true, the optional middleware setup for authentication will be called.

e.g.

   routes := []gopi.Route{
       {
           Method:      http.MethodGet,
           Version:     1,
           Path:        "ping",
           HandlerFunc: HandlePingRequest,
       }
   }
   
   func HandlePingRequest(w http.ResponseWriter, r *http.Request) {
       w.Write([]byte(`Pong!`))
   }

Middleware Func

Middleware Funcs are pieces of code that are run right before/after a request for a particular route is being processed by its handler. There are three kinds of Middleware funcs:

  1. Pre Middleware Funcs which are run before the HTTP request is passed to the handler
  2. Post Middleware Funcs, which are run after the request has been returned from handler
  3. Authenticate Middleware, a special kind of Pre Middleware which is run only when 'Authenticate' is set to true.

GOPI comes with some standard useful Middleware Funcs that are helpful in setting up a REST server e.g. api.LoggerMiddleware (which logs all the requests to Std. Out), api.SetJSONHeaderMiddleware (which sets the Content-Type: application/json header for the response). No standard authenticate middleware is provided with the library yet, so users are free to implement their own.

Server

A server takes in a bunch of routes and optionally some middleware funcs, and sets up a HTTP server for them.

// params: address, port, authenticate middleware, pre-middlewares, post-middlewares
err := gopi.StartServer("127.0.0.1", 8080, routes, nil, nil, nil)
if err != nil {
    log.Fatal(err)
}

Example

package main

import (
    "log"
	"net/http"

	api "github.com/teejays/gopi"

)

func main() {

    // Define Routes
    routes := []api.Route{
        
        // Ping Handler (/v1/ping)
        {
            Method:      http.MethodGet, // Get or Post, or what..
            Version:     1,
            Path:        "ping", // endpoint
            HandlerFunc: HandlePingRequest, // Need to define these handlers
        },
        // Ping Handler - Authenticated (v2/ping)
        {
            Method:       http.MethodGet,
            Version:      2,
            Path:         "ping",
            HandlerFunc:  HandlePingRequest,
            Authenticate: true, // if you want this endpoint to always run the 'Authenticate' middleware
        },
        // Create Something
        {
            Method:       http.MethodPost,
            Version:      1,
            Path:         "something",
            HandlerFunc:  HandleCreateSomething,
        },
        // Get Something by ID
        {
            Method:       http.MethodGet,
            Version:      1,
            Path:         "something/{id}",
            HandlerFunc:  HandleGetSomething,
        },
    }

	// - Middlewares (defined above outside this function)
    preMiddlewareFuncs := []api.MiddlewareFunc{api.MiddlewareFunc(api.LoggerMiddleware)}
    postMiddlewareFuncs := []api.MiddlewareFunc{api.SetJSONHeaderMiddleware}
    authMiddlewareFunc := nil

    err := api.StartServer("127.0.0.1", 8080, routes, authMiddlewareFunc, preMiddlewareFuncs, postMiddlewareFuncs)
    if err != nil {
        log.Fatal(err)
    }

    return

}

// Define your handlers: the requests for the routes are eventually passed to the handlers specified in the route

func HandlePingRequest(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(`Pong!`))
}

func HandleCreateSomething(w http.ResponseWriter, r *http.Request) {
    // Do stuff...

    // for now we just...
	api.WriteResponse(w, http.StatusCreated, "something is created")
}

func HandleGetSomething(w http.ResponseWriter, r *http.Request) {
    // Do stuff...

    // for now we just...
	api.WriteError(w, http.StatusNotFound, err, false, nil)
}

gopi's People

Contributors

teejays avatar

Watchers

 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.