rest

package module
v0.0.0-...-9cde170 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2019 License: MIT Imports: 9 Imported by: 0

README

Go REST

Summary

Go REST is designed for providing a library for creating a REST API client in go.
It includes support for easy session and cookie management.

Example

package main

import (
	"fmt"

	"github.com/theredcameron/rest"
)

type Runner struct {
	Run  string `json:"run"`
	Away string `json:"away"`
	Fast string `json:"fast"`
}

func main() {
	endpoints := rest.Endpoints{
		{
			Description: "Test description",
			Method:      "GET",
			Path:        "/api/{id}",
			F:           Test,
		},
	}

	meta := &rest.CookieMeta{
		File:      "test",
		Path:      "./",
		MaxAge:    3000,
		Key:       []byte("key"),
		StoreName: "test",
	}

	router, err := rest.NewRouter(endpoints, meta)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Starting on port 4000")
	err = router.Start("4000")
	if err != nil {
		fmt.Println(err)
	}
}

func Test(r *rest.Request) (interface{}, error) {
	run := Runner{
		Run:  "running",
		Away: r.Params["away"],
		Fast: r.Vars["id"],
	}

	if r.GetCookieValue("testing") == nil {
		r.SetCookieValue("testing", "run")
	}

	return run, nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandlerFunc

func NewHandlerFunc(f func(*Request) (interface{}, error), meta *CookieMeta, globalHeaders Headers) http.HandlerFunc

Types

type CookieMeta

type CookieMeta struct {
	File      string
	Path      string
	MaxAge    int
	Key       []byte
	StoreName string
}

type Endpoint

type Endpoint struct {
	Description string
	Method      string
	Path        string
	F           func(*Request) (interface{}, error)
}

type Endpoints

type Endpoints []Endpoint

type Headers

type Headers map[string]string

type Request

type Request struct {
	Vars   map[string]string
	Body   []byte
	Params map[string]string
	// contains filtered or unexported fields
}

func NewRequest

func NewRequest(r *http.Request, meta *CookieMeta) (*Request, error)

func (*Request) GetCookieValue

func (this *Request) GetCookieValue(key interface{}) interface{}

func (*Request) SetCookieValue

func (this *Request) SetCookieValue(key, value interface{})

type Router

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

func NewRouter

func NewRouter(meta *CookieMeta, globalHeaders Headers, endpoints ...Endpoints) (*Router, error)

func (*Router) Start

func (this *Router) Start(port string) error

Jump to

Keyboard shortcuts

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