vapi

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2019 License: BSD-3-Clause Imports: 11 Imported by: 0

README

go-vapi

vk styled api package for easy api developing

Website | Contributing

license GoDoc Coverage Status Build Status Go Report Card Release

Installation

go get -u github.com/riftbit/go-vapi

Usage

This is a minimal example.

import (
	"net/http"
	"log"
	"github.com/riftbit/go-vapi"
)

func main() {
	//Initializing VAPI (required)
	vapi.Initialize("/v1") // This method can receive middlewares as additional params

	//Add Apis to VAPI
	vapi.Server.RegisterService(new(ApiTodo), "todo")

	//Add Routes to VAPI
	vapi.Server.AddRoute("GET", "/", http.FileServer(http.Dir("./views/")))
	vapi.Server.AddRoute("GET", "/uploads", http.FileServer(http.Dir("./uploads/")))
	vapi.Server.AddRoute("GET", "/static/js/", http.FileServer(http.Dir("./static/js/")))
	vapi.Server.AddRoute("GET", "/static/css/", http.FileServer(http.Dir("./static/css/")))
	vapi.Server.AddRoute("GET", "/static/img/", http.FileServer(http.Dir("./static/img/")))

	log.Println("Started server on port",":8080")
	log.Fatal(http.ListenAndServe(":8080", vapi.Server.GetRouter()))
}

type ApiTodo struct {}

type ApiTodo_arg struct {
	XMLName    xml.Name    `xml:"todo" json:"-"`
	Title      string      `json:"title" xml:"title"`
	Body       string      `schema:"-" json:"body" xml:"body"`
	Tags       []string    `schema:"tags[]" json:"tags" xml:"tags"`
}

func (self *ApiTodo) Get(r *http.Request, Args *ApiTodo_arg, Reply *ApiTodo_arg) error {
	Reply.Tags=Args.Tags
	Reply.Title=Args.Title
	Reply.Body=Args.Body
	return nil
}

Initialize method can receive additional middlewares as params

    vapi.Initialize("/v1", middleware_log)

And middleware_log example is:

func middleware_log(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		log.Printf("URL Raw Query %v", r.URL.RawQuery)
		next.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}

And make request by curl (for json output)

curl -X POST -d 'title=New Todo' -d 'body=This is a new todo' -d 'tags[]=Todo' -d 'tags[]=Tag' http://127.0.0.1:8080/v1/todo.get

Or for XML output

curl -X POST -d 'title=New Todo' -d 'body=This is a new todo' -d 'tags[]=Todo' -d 'tags[]=Tag' http://127.0.0.1:8080/v1/todo.get?format=xml

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteResponse

func WriteResponse(ctx *fasthttp.RequestCtx, status int, resp ServerResponse)

WriteResponse write response to client with status code and server response struct

Types

type Error

type Error struct {
	// A Number that indicates the error type that occurred.
	ErrorHTTPCode int `json:"-"`

	// A Number that indicates the error type that occurred.
	ErrorCode int `json:"error_code"`

	// A String providing a short description of the error.
	// The message SHOULD be limited to a concise single sentence.
	ErrorMessage string `json:"error_msg"`

	// A Primitive or Structured value that contains additional information about the error.
	Data interface{} `json:"data"`
}

Error ... easyjson:json

func (*Error) Error

func (e *Error) Error() string

func (Error) MarshalEasyJSON added in v0.2.1

func (v Error) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Error) MarshalJSON added in v0.2.1

func (v Error) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Error) UnmarshalEasyJSON added in v0.2.1

func (v *Error) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Error) UnmarshalJSON added in v0.2.1

func (v *Error) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Marshaler added in v0.3.0

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

type ServerResponse

type ServerResponse struct {
	// The Object that was returned by the invoked method. This must be null
	// in case there was an error invoking the method.
	// As per spec the member will be omitted if there was an error.
	Response json.RawMessage `json:"response,omitempty"`

	// An Error object if there was an error invoking the method. It must be
	// null if there was no error.
	// As per spec the member will be omitted if there was no error.
	Error *Error `json:"error,omitempty"`
}

ServerResponse represents a JSON-RPC response returned by the server. easyjson:json

func (ServerResponse) MarshalEasyJSON added in v0.2.1

func (v ServerResponse) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (ServerResponse) MarshalJSON added in v0.2.1

func (v ServerResponse) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*ServerResponse) UnmarshalEasyJSON added in v0.2.1

func (v *ServerResponse) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*ServerResponse) UnmarshalJSON added in v0.2.1

func (v *ServerResponse) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type TestArgs added in v0.2.2

type TestArgs struct {
	ID  string `json:"id,omitempty"`
	Ttt string `json:"ttt,omitempty"`
}

TestArgs args for tests easyjson:json

func (TestArgs) MarshalEasyJSON added in v0.2.2

func (v TestArgs) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (TestArgs) MarshalJSON added in v0.2.2

func (v TestArgs) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*TestArgs) UnmarshalEasyJSON added in v0.2.2

func (v *TestArgs) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*TestArgs) UnmarshalJSON added in v0.2.2

func (v *TestArgs) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type TestReply added in v0.2.2

type TestReply struct {
	ID  string `json:"id,omitempty"`
	Ttt string `json:"ttt,omitempty"`
}

TestReply reply for tests easyjson:json

func (TestReply) MarshalEasyJSON added in v0.2.2

func (v TestReply) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (TestReply) MarshalJSON added in v0.2.2

func (v TestReply) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*TestReply) UnmarshalEasyJSON added in v0.2.2

func (v *TestReply) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*TestReply) UnmarshalJSON added in v0.2.2

func (v *TestReply) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Unmarshaler added in v0.3.0

type Unmarshaler interface {
	UnmarshalJSON([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.

By convention, to approximate the behavior of Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.

type VAPI

type VAPI struct {
	// contains filtered or unexported fields
}

VAPI - main structure

func NewServer

func NewServer() *VAPI

NewServer returns a new RPC server.

func (*VAPI) CallAPI

func (as *VAPI) CallAPI(ctx *fasthttp.RequestCtx, method string)

CallAPI call api method and process it. Modifying body after this function not recommended

func (*VAPI) GetServiceMap

func (as *VAPI) GetServiceMap() (map[string]string, error)

GetServiceMap returns an json api schema todo realize this function

func (*VAPI) RegisterService

func (as *VAPI) RegisterService(receiver interface{}, name string) error

RegisterService adds a new service to the api server.

The name parameter is optional: if empty it will be inferred from the receiver type name.

Methods from the receiver will be extracted if these rules are satisfied:

  • The receiver is exported (begins with an upper case letter) or local (defined in the package registering the service).
  • The method name is exported.
  • The method has three arguments: *fasthttp.RequestCtx, *args, *reply.
  • All three arguments are pointers.
  • The second and third arguments are exported or local.
  • The method has return type error.

All other methods are ignored.

Jump to

Keyboard shortcuts

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