platforms

package
v0.0.0-...-1676a5e Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2020 License: MIT, MIT Imports: 6 Imported by: 0

README

platforms

Platform interfaces for jsonrouter

Documentation

Overview

Package platforms Examples

Appengine:

import (
	"gitlab.com/TheDarkula/jsonrouter/http"
	"gitlab.com/TheDarkula/jsonrouter/platforms/appengine"
	ht "net/http"
	"gitlab.com/TheDarkula/jsonrouter/openapi/v2"
	"gitlab.com/TheDarkula/jsonrouter//validation"
)

func TestServer() {
	spec := openapiv2.New("localhost", "TITLE")

	service, err = jsonrouter.New(spec)
	if (err != nil){
		// Handle Error //
	}

	api := service.Add("/api")

	apiProduct := api.Add("/product")

		apiProduct.Param(validation.StringExact(30), "productID").GET(
			app.api_product_get,
		).Description(
			"Gets the specified product",
		).Response(
			Product{},
		)

		apiProduct.Add("/new").POST(
			app.api_product_new,
		).Required(
			&validation.Payload{
				"name": validation.String(),
		}.Description(
			"Creates new product",
		).Response(
			Product{},
		)

	panic(
		ht.ListenAndServe(
			fmt.Sprintf(
				":%d",
				PORT,
			),
			service,
		),
	)
}

func (app *App) api_product_get(req http.Request) *http.Status {

	id := req.Param("productID").(string)
	product := getProduct(id)

	return req.Respond(product)
}

func (app *App) api_product_new(req http.Request) *http.Status {

	product := newProduct()
	name := req.BodyParam("name").(string)
	product.name = name

	return req.Respond(product)
}

Fasthttp:

import (
	"gitlab.com/TheDarkula/jsonrouter/http"
	"gitlab.com/TheDarkula/jsonrouter/logging/testing"
	"gitlab.com/TheDarkula/jsonrouter/platforms/fasthttp"
	"gitlab.com/TheDarkula/jsonrouter/openapi/v2"
	"gitlab.com/TheDarkula/jsonrouter//validation"

)

func TestServer() {
	log := logs.NewClient().NewLogger()
	spec := openapiv2.New("localhost", "TITLE")

	_, service = jsonrouter.New(log, spec)

	api := service.Add("/api")

	apiProduct := api.Add("/product")

		apiProduct.Param(validation.StringExact(30), "productID").GET(
			app.api_product_get,
		).Description(
			"Gets the specified product",
		).Response(
			Product{},
		)

		apiProduct.Add("/new").POST(
			app.api_product_new,
		).Required(
			&validation.Payload{
				"name": validation.String(),
		}.Description(
			"Creates new product",
		).Response(
			Product{},
		)

	panic(
		service.Serve(PORT),
	)
}

func (app *App) api_product_get(req http.Request) *http.Status {

	id := req.Param("productID").(string)
	product := getProduct(id)

	return req.Respond(product)
}

func (app *App) api_product_new(req http.Request) *http.Status {

	product := newProduct()
	name := req.BodyParam("name").(string)
	product.name = name

	return req.Respond(product)
}

Standard:

import (
	"gitlab.com/TheDarkula/jsonrouter/http"
	"gitlab.com/TheDarkula/jsonrouter/logging/testing"
	"gitlab.com/TheDarkula/jsonrouter/platforms/standard"
	"gitlab.com/TheDarkula/jsonrouter/openapi/v2"
	"gitlab.com/TheDarkula/jsonrouter//validation"
	ht "net/http"
)

type Product struct {
	name string
}

func TestServer() {
	log := logs.NewClient().NewLogger()
	spec := openapiv2.New("localhost", "TITLE")

	service, err = jsonrouter.New(log, spec)
	if (err != nil){
		// Handle error
	}

	api := service.Add("/api")

	apiProduct := api.Add("/product")

		apiProduct.Param(validation.StringExact(30), "productID").GET(
			app.api_product_get,
		).Description(
			"Gets the specified product",
		).Response(
			Product{},
		)

		apiProduct.Add("/new").POST(
			app.api_product_new,
		).Required(
			&validation.Payload{
				"name": validation.String(),
		}.Description(
			"Creates new product",
		).Response(
			Product{},
		)

	panic(
		ht.ListenAndServe(
			fmt.Sprintf(
				":%d",
				PORT,
			),
			service,
		),
	)
}

func (app *App) api_product_get(req http.Request) *http.Status {

	id := req.Param("productID").(string)
	product := getProduct(id)

	return req.Respond(product)
}

func (app *App) api_product_new(req http.Request) *http.Status {

	product := newProduct()
	name := req.BodyParam("name").(string)
	product.name = name

	return req.Respond(product)
}

Index

Constants

View Source
const (
	CONST_SPEC_PATH_V2 = "/openapi.spec.v2.json"
	CONST_SPEC_PATH_V3 = "/openapi.spec.v3.json"
)

Variables

This section is empty.

Functions

func AddMetricsEndpoints

func AddMetricsEndpoints(root *tree.Node)

func AddSpecEndpoints

func AddSpecEndpoints(root *tree.Node)

func RunStandardTests

func RunStandardTests(t *testing.T, req http.Request)

func StandardTests_headers

func StandardTests_headers(t *testing.T, req http.Request)

func StandardTests_parameters

func StandardTests_parameters(t *testing.T, req http.Request)

Types

type Router

type Router struct {
	http.Handler
	*tree.Node
}

func NewRouter

func NewRouter(node *tree.Node, handlerFunc func(res http.ResponseWriter, r *http.Request)) *Router

type WildcardRouter

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

func (*WildcardRouter) HandleFunc

func (router *WildcardRouter) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request))

func (*WildcardRouter) Handler

func (router *WildcardRouter) Handler(pattern *regexp.Regexp, handler http.Handler)

func (*WildcardRouter) Serve

func (router *WildcardRouter) Serve(port int) error

func (*WildcardRouter) ServeHTTP

func (router *WildcardRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
Package jsonrouter implements the http-router for net/http projects
Package jsonrouter implements the http-router for net/http projects

Jump to

Keyboard shortcuts

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