nanoapi

package module
v0.0.0-...-55bb704 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2022 License: MIT Imports: 13 Imported by: 7

README

nanoapi

Nano API Implementation in GO

Please refer to nanoapi-test for a full example on how to use nano. Also refer to envfile for settingg up it correctly.

TL;DR

The idea is to create a very simple API interface on top of standard router like the one shown below.

Please refer to nanoapi-test, for a full api setup w nano.

The implementation relies on top of generics, to make things really simple, as shown below.

nanoapi.H("b", func(i string) (out time.Time, err error) {
    out = time.Now()
    return
})
http.ListenAndServe(":8080", nil)

Later on you call it as:

res, err = http.Get("http://localhost:8080/b")

if err != nil {
    log.Printf("Err: " + err.Error())
    t.Fatal()
}

Details

In case more control is required, method H (Handler) can be used with a nanoapi.C (C abstracts a Command) struct, like this:

nanoapi.H(&nanoapi.C[string, string]{
		Name: "a",
		F: func(i string) (out string, err error) {
			out = "Got: " + i
			return
		},
		Help: "A function",
	})

Nano api will provide you some methods (in case you call nanoapi.Setup first):

  • __list: lists all available commands

  • __help/cmd: Provides you a JSON object with in, out format , cmd message and Help message (in case you use H to register CMD), like this:

{"name":"b","in":"","out":"0001-01-01T00:00:00Z","help":"Function b"}

Important

  • At this moment, no methods are bound to API - we want it simple
  • Registration is done directly to defaultMux - we want it simple
  • It only supports stdlib router - we want it simple

Your contribution / feedback is welcome.

Documentation

Overview

Implements a simple command oriented API Wrapper

Index

Constants

View Source
const PERM_ALL = "*"
View Source
const PERM_AUTH = "+"

Variables

View Source
var CheckPerm func(ctx context.Context, p string) bool = func(ctx context.Context, p string) bool {
	return true
}

Functions

func AddDoc

func AddDoc(d *Opts)

func All

func All[IN, OUT any](path string, f func(ctx context.Context, in IN) (OUT, error), o ...*Opts)

func CtxCmd

func CtxCmd(c context.Context) string

CtxCmd - gets all session data from cache.

func CtxDone

func CtxDone(c context.Context) func()

CtxDone - returns function to inform response is done

func CtxMPF

func CtxMPF(c context.Context) *multipart.Form

CtxMPF - returns multipart.Form if request is multipart, otherwise nil. Include files

func CtxReadJson

func CtxReadJson(c context.Context, in interface{}) error

func CtxReq

func CtxReq(c context.Context) *http.Request

CtxReq - provides access to Reques from context, suggar syntax.

func CtxRes

CtxRes - provides access to Response from context, suggar syntax.

func CtxVar

func CtxVar(c context.Context, v string) string

func CtxVarInt

func CtxVarInt(c context.Context, v string) int64

func CtxVars

func CtxVars(c context.Context) map[string]string

func Delete

func Delete[OUT any](path string, f func(ctx context.Context) (OUT, error), o ...*Opts)

func DumpDoc

func DumpDoc(rw http.ResponseWriter, r *http.Request)

func Get

func Get[OUT any](path string, f func(ctx context.Context) (OUT, error), o ...*Opts)
func Head[OUT any](path string, f func(ctx context.Context) (OUT, error), o ...*Opts)

func ListenDefault

func ListenDefault() error

func ListenDefaultTLS

func ListenDefaultTLS(c string, k string) error

func MW

func MW(h func(next http.Handler) http.Handler)

func Mime

func Mime(ext string) string

func MimeF

func MimeF(f string) string

func Mux

func Mux() *gmux.Router

func Post

func Post[IN, OUT any](path string, f func(ctx context.Context, in IN) (OUT, error), o ...*Opts)

func Put

func Put[IN, OUT any](path string, f func(ctx context.Context, in IN) (OUT, error), o ...*Opts)

func Raw

func Raw(path string, f func(rw http.ResponseWriter, r *http.Request), o ...*Opts)

func ReqCmd

func ReqCmd(r *http.Request) string

ReqCmd - gets cmd from request

func Setup

func Setup() error

Setup - prepares nanoapi. For timebeing only registers __list command

func SetupWDeps

func SetupWDeps(ds ...func() error) error

func Static

func Static(webpath string, fsroot string)

Types

type NONE

type NONE struct{}

NONE type is used in case you dont need an IN or OUT type.

type Opts

type Opts struct {
	Perm   string      `json:"perm"`
	Doc    string      `json:"doc"`
	Path   string      `json:"path"`
	Method string      `json:"method"`
	Where  string      `json:"where"`
	In     interface{} `json:"in"`
	Out    interface{} `json:"out"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL