Help us shape the future of Go! Take the 2026 Go Developer Survey

svapi

package module
v0.4.0 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: 7 Imported by: 0

README

go-svapi

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-svapi

Usage

This is a minimal example.

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

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

View Source
const ContentTypeHtml = "text/html; charset=utf-8"
View Source
const ContentTypeJson = "application/json; charset=utf-8"
View Source
const ContentTypeProtobuf = "application/protobuf"
View Source
const XmlContentTypeXml = "application/xml; charset=utf-8"
View Source
const XmlContentTypeXmlAtom = "application/atom+xml; charset=utf-8"
View Source
const XmlContentTypeXmlRss = "application/rss+xml; charset=utf-8"

Variables

This section is empty.

Functions

func WriteResponseBytes

func WriteResponseBytes(ctx *fasthttp.RequestCtx, status int, contentType string, resp []byte)

WriteResponseBytes write response to client with status code, body and content type

func WriteResponseString

func WriteResponseString(ctx *fasthttp.RequestCtx, status int, contentType string, resp string)

WriteResponseString write response to client with status code, body and content type

Types

type ErrorHandlerFunction

type ErrorHandlerFunction func(ctx *fasthttp.RequestCtx, err error)

SendPushFunction type that define function that used to send push

type SVAPI

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

SVAPI - main structure

func NewServer

func NewServer() *SVAPI

NewServer returns a new RPC server.

func (*SVAPI) CallAPI

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

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

func (*SVAPI) GetServiceMap

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

GetServiceMap returns an json api schema todo realize this function

func (*SVAPI) RegisterService

func (as *SVAPI) 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