Skip to content
forked from ryankurte/go-api

An API helper framework build on top of gocraft/web

License

Notifications You must be signed in to change notification settings

harisxue/go-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-api

A typed API builder project to remove a bunch of boilerplate and repetition from golang API projects. This wires together a bunch of common dependencies, uses some reflection based magic to provide typed API functions and common error handling, and is intended to encode some best practices in API security.

Status

Build Status Documentation Release

Overview

  • core collects components and exposes the user API
  • formats provide format encoding/decoding functions
  • options provide base api server options and option parsing
  • plugins provide plugins for meta analysis of the API implementation
  • security provide security extensions for the API implementation
  • servers provide base server handling (ie. http server, AWS lambda)
  • wrappers provide wrapping functions for typed api endpoints

Usage

Install with go get github.com/ryankurte/go-api.

Create an application options object that inherits from options.Base and load with options.Parse(&o). Options are parsed using jessevdk/go-flags.

import (
    "github.com/ryankurte/go-api/lib/options"
)

type AppConfig struct {
    options.Base
    ...
}

...

o := AppConfig{}
err := options.Parse(&o)
if err != nil {
    os.Exit(0)
} 

Create a base application context with type handlers and a base api.API router, the attach handlers to the API router.

Handler functions support input parameters:

  • (ctx ContextType)
  • (ctx ContextType, i InputType)
  • (ctx ContextType, i InputType, http.header)

And output parameters:

  • (OutputType, error)
  • (OutputType, int, error)
  • (OutputType, int, http.Header, error)

Where ContextType, InputType and OutputType are user defined structs and int is a http.Status code. Input and output types are validated after decoding and prior to encoding using asaskevich/govalidator. The underlying mux is provided by gocraft/web.

import (
    "github.com/ryankurte/go-api/lib/options"
)

type AppContext struct {
	...
}

func (c *AppContext) FakeEndpoint(a AppContext, i Request) (Response, error) {
	...
}

ctx := AppContext{"Whoop whoop"}
api, err := api.New(ctx, &o.Base)
if err != nil {
    log.Print(err)
    os.Exit(-1)
}

err = api.RegisterEndpoint("/", "POST", (*AppContext).FakeEndpoint)

You can then launch a server with api.Run() and exit wth api.Close().

Check out example.go for a working example.


If you have any questions, comments, or suggestions, feel free to open an issue or a pull request.

About

An API helper framework build on top of gocraft/web

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%