https://github.com/ryankurte/go-api
An API helper framework build on top of gocraft/web
https://github.com/ryankurte/go-api
api framework golang helper
Last synced: 6 months ago
JSON representation
An API helper framework build on top of gocraft/web
- Host: GitHub
- URL: https://github.com/ryankurte/go-api
- Owner: ryankurte
- License: mit
- Created: 2018-02-09T00:51:49.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-02T02:37:49.000Z (about 8 years ago)
- Last Synced: 2025-03-09T13:18:50.048Z (about 1 year ago)
- Topics: api, framework, golang, helper
- Language: Go
- Homepage:
- Size: 38.1 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
[](https://travis-ci.org/ryankurte/go-api)
[](https://godoc.org/github.com/ryankurte/go-api)
[](https://github.com/ryankurte/go-api)
## Overview
- [core](lib/) collects components and exposes the user API
- [formats](lib/formats) provide format encoding/decoding functions
- [options](lib/options) provide base api server options and option parsing
- [plugins](lib/plugins) provide plugins for meta analysis of the API implementation
- [security](lib/security) provide security extensions for the API implementation
- [servers](lib/servers) provide base server handling (ie. http server, AWS lambda)
- [wrappers](lib/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](https://github.com/jessevdk/go-flags).
``` go
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](https://github.com/asaskevich/govalidator).
The underlying mux is provided by [gocraft/web](https://github.com/gocraft/web).
``` go
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](example.go) for a working example.
------
If you have any questions, comments, or suggestions, feel free to open an issue or a pull request.