ringo

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2015 License: MIT Imports: 13 Imported by: 0

README

Ringo GoDoc Build Status

Yet another MVC web framework for Go, inspired from rails, gin.

Install

go get github.com/jjyr/ringo

Usage

hello world
// hello.go
package main

import "github.com/jjyr/ringo"

func main() {
	app := ringo.NewApp()
	app.GET("/", func(c *ringo.Context) {
		c.String(200, "hello")
	})
	app.Run("localhost:8000")
}

controller
package main

import (
	"fmt"

	"github.com/jjyr/ringo"
)

// user model
// when use Bind(BindJSON) validate will auto perform, details: gopkg.in/go-playground/validator.v8
type user struct {
	Name string `json:"name" validate:"required"`
}

// user controller
// controller contains several built in RESTful routes will auto added if accord methods is defined
// built in routes:
// method(handler) -> route path
// New -> [GET]/new-user
// Create -> [POST]/user
// Edit -> [GET]/user/:id/edit
// Update -> [PUT/PATCH]/user/:id
// List -> [GET]/user
// Get -> [GET]/user/:id
// Delete -> [DELETE]/user/:id
type UserController struct {
	ringo.Controller
	users []user
}

func (ctl *UserController) List(c *ringo.Context) {
	c.JSON(200, ctl.users)
}

func (ctl *UserController) Delete(c *ringo.Context) {
	idx := -1
	name := c.Params.ByName("id")
	for i, u := range ctl.users {
		if name == u.Name {
			// found
			idx = i
			break
		}
	}
	if idx >= 0 {
		ctl.users = append(ctl.users[:idx], ctl.users[idx+1:]...)
		c.String(200, "ok")
	} else {
		c.String(404, "not found")
	}
}

func (ctl *UserController) Create(c *ringo.Context) {
	u := user{}
	if err := c.BindJSON(&u); err == nil {
		ctl.users = append(ctl.users, u)
		c.JSON(200, u)
	} else {
		c.String(400, "format error")
	}
}

// customized action
func (ctl *UserController) Greet(c *ringo.Context) {
	name := c.Params.ByName("id")
	var u *user = nil
	for _, user := range ctl.users {
		if name == user.Name {
			u = &user
			break
		}
	}
	if u == nil {
		c.String(404, "not found")
	} else {
		c.String(200, fmt.Sprintf("Hello, I'm %s", u.Name))
	}
}

var userController *UserController

// init userController
func init() {
	userController = &UserController{}
	// register customized route
	// Post -> [POST]/user/:id/greeting
	userController.AddRoutes(ringo.ControllerRouteOption{Handler: "Greet", Member: true, Method: "POST", Path: "greeting"})
}

func main() {
	app := ringo.NewApp()
	app.AddController(userController, nil)
	app.Run("localhost:8000")
}

Contribute

  • Feel free to open issue
  • Feel free to send pull request
  • New feature pull request should include test case

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetControllerName

func GetControllerName(c Controllerable) string

GetControllerName return controller name, generate by type name if not manually set

Types

type App

type App struct {
	*Router

	*ControllerManage
	// contains filtered or unexported fields
}

func NewApp

func NewApp() *App

func (*App) Run

func (app *App) Run(addr string) error

func (*App) ServeHTTP

func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*App) Use

func (app *App) Use(middlewreFunc MiddlewareFunc)

type Context

type Context struct {
	Request *http.Request
	http.ResponseWriter
	Params Params
	// contains filtered or unexported fields
}

func NewContext

func NewContext() *Context

func (*Context) Bind

func (c *Context) Bind(obj interface{}) error

Bind

func (*Context) BindJSON

func (c *Context) BindJSON(obj interface{}) error

func (*Context) BindWith

func (c *Context) BindWith(obj interface{}, b binding.Binder) error

func (*Context) ContentType

func (c *Context) ContentType() string

func (*Context) Deadline

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done

func (c *Context) Done() <-chan struct{}

func (*Context) Err

func (c *Context) Err() error

func (*Context) File

func (c *Context) File(file string)

func (*Context) FindControllerRoutePath

func (c *Context) FindControllerRoutePath(name string, handler string) string

FindControllerRoutePath Find controller action path, return first path if multi route registered, panic if not found

func (*Context) Get

func (c *Context) Get(key string) (value interface{}, exists bool)

Get value by key

func (*Context) JSON

func (c *Context) JSON(statusCode int, content interface{})

func (*Context) Redirect

func (c *Context) Redirect(code int, location string)

func (*Context) Render

func (c *Context) Render(statusCode int, r render.Renderable)

func (*Context) Rendered

func (c *Context) Rendered() bool

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set key value pair

func (*Context) String

func (c *Context) String(statusCode int, format string, contents ...interface{})

render

func (*Context) Value

func (c *Context) Value(key interface{}) interface{}

type Controller

type Controller struct {
	Name string
	// contains filtered or unexported fields
}

func (*Controller) AddRoutes

func (c *Controller) AddRoutes(routeOptions ...ControllerRouteOption)

AddRoutes add customize route to controller

func (*Controller) ControllerName

func (c *Controller) ControllerName() string

func (*Controller) GetRoutes

func (c *Controller) GetRoutes() []ControllerRouteOption

func (*Controller) SetControllerName

func (c *Controller) SetControllerName(name string)

type ControllerManage

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

func (*ControllerManage) AddController

func (m *ControllerManage) AddController(c Controllerable, controllerOption *ControllerOption)

AddController add newcontroller

func (*ControllerManage) FindControllerRoutePath

func (m *ControllerManage) FindControllerRoutePath(name string, handler string, paramvalue ...string) string

FindControllerRoutePath Find controller action path, return first path if multi route registered, panic if not found paramvalue are pairs of param/value like "id", "2"

type ControllerOption

type ControllerOption struct {
	// controller path prefix
	Prefix string
	// override controller name
	Name string
}

type ControllerRouteOption

type ControllerRouteOption struct {
	// handler method name
	Handler string
	// http method
	Method string
	// http methods
	Methods []string
	// route path
	Path string
	// route prefix
	Prefix string
	// override controller name
	Name string
	// route suffix
	Suffix string
	// as member route, like: "/users/1/xxx"
	Member bool
	// as collection route, like: "/users/xxx"
	Collection bool
}

ControllerRouteOption options to customize controller route

type Controllerable

type Controllerable interface {
	ControllerName() string
	SetControllerName(string)
	AddRoutes(...ControllerRouteOption)
	GetRoutes() []ControllerRouteOption
}

type HandlerFunc

type HandlerFunc func(c *Context)

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

type Params

type Params httprouter.Params

func (*Params) ByName

func (params *Params) ByName(name string) string

type ResponseWriter

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

func (*ResponseWriter) CloseNotify

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

Implements the http.CloseNotify interface

func (*ResponseWriter) Flush

func (w *ResponseWriter) Flush()

Implements the http.Flush interface

func (*ResponseWriter) Hijack

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

Implements the http.Hijack interface

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(content []byte) (n int, err error)

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(statusCode int)

func (*ResponseWriter) Written

func (w *ResponseWriter) Written() bool

type Router

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

func NewRouter

func NewRouter() *Router

NewRouter new router

func (*Router) AddRoute

func (r *Router) AddRoute(path string, method string, handler HandlerFunc)

func (*Router) DELETE

func (r *Router) DELETE(path string, handler HandlerFunc)

func (*Router) GET

func (r *Router) GET(path string, handler HandlerFunc)

func (*Router) HEAD

func (r *Router) HEAD(path string, handler HandlerFunc)

func (*Router) MatchRoute

func (r *Router) MatchRoute(path string, method string) (handler HandlerFunc, params Params, redirect bool)

func (*Router) Mount

func (router *Router) Mount(mountPath string, mountedRouter *Router)

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler HandlerFunc)

func (*Router) PATCH

func (r *Router) PATCH(path string, handler HandlerFunc)

func (*Router) POST

func (r *Router) POST(path string, handler HandlerFunc)

func (*Router) PUT

func (r *Router) PUT(path string, handler HandlerFunc)

Directories

Path Synopsis
Package binding idea & some code from gin binding https://github.com/gin-gonic/gin/tree/master/binding , thanks gin
Package binding idea & some code from gin binding https://github.com/gin-gonic/gin/tree/master/binding , thanks gin

Jump to

Keyboard shortcuts

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