horo

package module
v0.0.0-...-53a64c4 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2017 License: MIT Imports: 10 Imported by: 0

README

Horo GoDoc

Horo is context friendly, Simple Web framework.

Install

go get -u github.com/k2wanko/horo

Usage

package main

import (
    "net/http"

    "golang.org/x/net/context"

    "github.com/k2wanko/horo"
    "github.com/k2wanko/horo-middleware"
)

func Index(c context.Context) error {
    return horo.Text(c, http.StatusOK, "Hello World!")
}

func main() {
    h := horo.New()
    h.Use(middleware.Logger(), middleware.Recover())

    h.GET("/", Index)

    h.ListenAndServe(":8080")
}

more examples

License

MIT

Documentation

Overview

Package horo is context friendly, Simple Web framework.

Basic Example:

package main

import (
    "net/http"

    "golang.org/x/net/context"

    "github.com/k2wanko/horo"
    "github.com/k2wanko/horo-middleware"
)

func Index(c context.Context) error {
    return horo.Text(c, http.StatusOK, "Hello World!")
}

func main() {
    h := horo.New()
    h.Use(middleware.Logger(), middleware.Recover())

    h.GET("/", Index)

    h.ListenAndServe(":8080")
}

Google App Engine Example:

package main

import (
    "net/http"

    "golang.org/x/net/context"

    "github.com/k2wanko/horo"
    "github.com/k2wanko/horo-middleware"
)

func Index(c context.Context) error {
    return horo.Text(c, 200, fmt.Sprintf("Hello, %s", appengine.AppID(c)))
}

func init() {
    h := horo.New()
    h.Use(middleware.Recover(), middleware.AppContext())

    h.GET("/", Index)

    http.Handle("/", h)
}
Example (Appengine)
package main

import (
	"net/http"

	"github.com/k2wanko/horo"
	"golang.org/x/net/context"
)

func main() {
	// Write in func init()
	h := horo.New()
	h.GET("/", func(c context.Context) error {
		return horo.Text(c, 200, "Hello, World")
	})
	http.Handle("/", h)
}
Output:

Example (Basic)
h := horo.New()
h.Use(middleware.Logger())
h.GET("/", func(c context.Context) error {
	return horo.Text(c, 200, "Hello, World")
})
h.ListenAndServe(":8080")
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotContext is thrown if the context does not have Horo context.
	ErrNotContext = errors.New("Not Context")

	// ErrInvalidRedirectCode is thrown if invalid redirect code.
	ErrInvalidRedirectCode = errors.New("invalid redirect status code")
)

Functions

func DefaultErrorHandler

func DefaultErrorHandler(c context.Context, err error)

DefaultErrorHandler invoke HTTP Error Handler

func HTML

func HTML(c context.Context, code int, html string) (err error)

HTML send a HTML response.

func JSON

func JSON(c context.Context, code int, i interface{}) (err error)

JSON send a JSON response.

func MethodNotAllowed

func MethodNotAllowed(c context.Context) error

MethodNotAllowed is default MethodNotAllowed handler.

func NoContent

func NoContent(c context.Context, code int) error

NoContent send no body.

func NotFound

func NotFound(c context.Context) error

NotFound is default not found handler.

func Param

func Param(c context.Context, name string) (v string)

Param returns url param.

func Redirect

func Redirect(c context.Context, code int, url string) error

Redirect redirect the request status code.

func Request

func Request(c context.Context) (r *http.Request)

Request returns *http.Request from context.

func RequestID

func RequestID(ctx context.Context) (id string)

RequestID returns request id from context.

func Text

func Text(c context.Context, code int, s string) (err error)

Text send a Text response.

Types

type ErrorHandlerFunc

type ErrorHandlerFunc func(context.Context, error)

ErrorHandlerFunc is error handling function.

type HTTPError

type HTTPError struct {
	Code    int
	Message string
}

HTTPError handling a request.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type HandlerFunc

type HandlerFunc func(context.Context) error

HandlerFunc is server HTTP requests.

type Horo

type Horo struct {
	ErrorHandler               ErrorHandlerFunc
	NotFound, MethodNotAllowed HandlerFunc
	Logger                     log.Logger
	RequestIDGenerator         RequestIDGenerator
	// contains filtered or unexported fields
}

Horo freamwork instance.

func New

func New() (h *Horo)

New is create Horo instance.

func (*Horo) DELETE

func (h *Horo) DELETE(path string, hf HandlerFunc, mw ...MiddlewareFunc)

DELETE registers a new DELETE handler

func (*Horo) GET

func (h *Horo) GET(path string, hf HandlerFunc, mw ...MiddlewareFunc)

GET registers a new GET handler

func (*Horo) HEAD

func (h *Horo) HEAD(path string, hf HandlerFunc, mw ...MiddlewareFunc)

HEAD registers a new HEAD handler

func (*Horo) ListenAndServe

func (h *Horo) ListenAndServe(addr string) error

ListenAndServe is Start HTTP Server

func (*Horo) OPTIONS

func (h *Horo) OPTIONS(path string, hf HandlerFunc, mw ...MiddlewareFunc)

OPTIONS registers a new OPTIONS handler

func (*Horo) PATCH

func (h *Horo) PATCH(path string, hf HandlerFunc, mw ...MiddlewareFunc)

PATCH registers a new PATCH handler

func (*Horo) POST

func (h *Horo) POST(path string, hf HandlerFunc, mw ...MiddlewareFunc)

POST registers a new POST handler

func (*Horo) PUT

func (h *Horo) PUT(path string, hf HandlerFunc, mw ...MiddlewareFunc)

PUT registers a new PUT handler

func (*Horo) ServeHTTP

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

ServeHTTP implements http.Handler interface.

func (*Horo) Use

func (h *Horo) Use(mw ...MiddlewareFunc)

Use is add middleware.

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc is process middleware.

type RequestIDGenerator

type RequestIDGenerator interface {
	RequestID(c context.Context) string
}

RequestIDGenerator is generate requestID method interface.

var (
	// DefaultRequestIDGenerator is application request id generator.
	DefaultRequestIDGenerator RequestIDGenerator = &reqGen{}
)

type ResponseWriter

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

ResponseWriter is horo ResponseWriter

func Response

func Response(c context.Context) (w *ResponseWriter)

Response returns ResponseWriter from context.

func (*ResponseWriter) CloseNotify

func (r *ResponseWriter) CloseNotify() <-chan bool

CloseNotify implements http.CloseNotifier

func (*ResponseWriter) Committed

func (r *ResponseWriter) Committed() bool

Committed is responsed flag.

func (*ResponseWriter) Flush

func (r *ResponseWriter) Flush()

Flush implements http.Flusher

func (*ResponseWriter) Header

func (r *ResponseWriter) Header() http.Header

Header implements http.ResponseWriter

func (*ResponseWriter) Hijack

func (r *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements http.Hijacker

func (*ResponseWriter) Reset

func (r *ResponseWriter) Reset(w http.ResponseWriter)

Reset cleanup struct

func (*ResponseWriter) Size

func (r *ResponseWriter) Size() int64

Size returns response size.

func (*ResponseWriter) Status

func (r *ResponseWriter) Status() int

Status returns http status.

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(b []byte) (n int, err error)

Write implements http.ResponseWriter

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(code int)

WriteHeader implements http.ResponseWriter

Directories

Path Synopsis
Package log is horo logger.
Package log is horo logger.

Jump to

Keyboard shortcuts

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