coco

package module
v0.0.0-...-7a48d6d Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 7 Imported by: 2

README

Coco

Build Status codecov Codacy Badge GitHub
Coco is an http web framework for golang.

Install

go get -u github.com/olivetree123/coco

Tutorial

import (
    "github.com/olivetree123/coco"
)

func HelloHandler(c *coco.Coco) coco.Result {
    return coco.APIResponse("Hello Coco !")
}

func main() {
    c := coco.NewCoco()
    c.AddRouter("GET", "/", HelloHandler)
    c.Run()
}

Params

// GET, /api?name=123
params := c.Request.URL.Query()
params.Get("name")

// GET, param in url, /api/:name
name := c.Params.ByName("name")

// POST JSON
var param DBCreateParam
decoder := json.NewDecoder(c.Request.Body)
err := decoder.Decode(&param)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MimeTypeByName

func MimeTypeByName(name string) string

Types

type Coco

type Coco struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter
	Params         httprouter.Params
	Header         map[string]string
	// contains filtered or unexported fields
}

Coco Framework

func NewCoco

func NewCoco() *Coco

func (*Coco) AddRouter

func (c *Coco) AddRouter(method string, path string, h Handler)

func (*Coco) Run

func (c *Coco) Run(host string, port int) error

func (*Coco) ServeHTTP

func (c *Coco) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现了该方法的对象,可以作为 http.ListenAndServe 的 handler http server 在监听到请求之后会把请求交给 ServeHTTP 处理,主要处理方式就是路由并分发给相应的 handler

type Handler

type Handler func(coco *Coco) Result

Handler 所有 handler 都会被转换成该类型

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Result

type Result struct {
	Type       string      `json:"-"` // object or binary
	Code       int         `json:"code"`
	Data       interface{} `json:"data"`
	BinaryData []byte      `json:"-"`
	Message    string      `json:"message"`
	FileName   string      `json:"-"`
}

Result 返回结果

func APIResponse

func APIResponse(data interface{}) Result

func ErrorResponse

func ErrorResponse(code int) Result

func FileResponse

func FileResponse(fileName string, content []byte) Result

Jump to

Keyboard shortcuts

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