top

Package genapi implements a generic skeleton we can use as the basis for an api service. It will set up command line arguments, connections to backend databases, handle test modes which might affect those databases, register itself with skyapi, and more.

Basic definition

To use first initialize a GenAPI instance somewhere accessible by your entire application, and give it an RPC type

package myapi

var GA = genapi.GenAPI{
	Name:      "my-api",
	RedisInfo: &genapi.RedisInfo{}
	Services:  []interface{}{MyAPI{}},
}

type MyAPI struct{}

func (_ MyAPI) Foo(r *http.Request, args *struct{}, res *struct{}) error {
	return GA.Cmd("INCR", "MyKey").Err
}

API Mode

To actually read command-line arguments, set up database connections, listen on a random port, register with skyapi, and start handling requests, simply call APIMode() from your main method:

package main

func main() {
	myapi.GA.APIMode()
}

In APIMode the genapi will also listen for SIGTERM signals, and if it receives one will unregister with skyapi, and exit once all ongoing requests are completed.

Test Mode

When testing your api you can call TestMode from your test's init function, and call RPC to get an instance of an http.Handler you can make calls against:

package myapi // myapi_test.go

import . "testing"

func init() {
	GA.TestMode()
}

func TestSomeThing(t *T) {
	h := GA.RPC()
	// test against h
}

CLI Mode

Finally, there are times when you want a command-line binary which will be made alongside the actual api binary, and which will share resources and possibly database connections. In this case you can use the CLIMode method and then access the GenAPI from your main method as normal:

package main

func main() {
	myapi.GA.CLIMode()
	myapi.GA.Cmd("DECR", "MyKey")
}

Imports 17 package(s)

  1. github.com/gorilla/rpc/v2
  2. golang.org/x/net/context
  3. github.com/mediocregopher/lever
  4. github.com/levenlabs/golib/timeutil
  5. gopkg.in/mgo.v2
  6. github.com/levenlabs/golib/rpcutil
  7. github.com/mediocregopher/radix.v2/pool
  8. github.com/armon/go-proxyproto
  9. github.com/mediocregopher/okq-go.v2
  10. github.com/miekg/dns
  11. github.com/mediocregopher/skyapi/client
  12. github.com/levenlabs/gatewayrpc
  13. github.com/levenlabs/golib/mgoutil
  14. github.com/mediocregopher/radix.v2/util
  15. github.com/levenlabs/go-srvclient
  16. github.com/levenlabs/go-llog
  17. github.com/levenlabs/golib/radixutil

Test imports 3 package(s)

  1. github.com/levenlabs/golib/testutil
  2. github.com/stretchr/testify/assert
  3. github.com/stretchr/testify/require