banjo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2017 License: MPL-2.0 Imports: 10 Imported by: 0

README

BANjO

Build Status Software License GoDoc Coverage Status

banjo it's a simple web framework for building simple web applications

Install

$ go get github.com/nsheremet/banjo

Example Usage

Simple Web App - main.go

package main

import "banjo"

func main() {
  app := banjo.Create(banjo.DefaultConfig())
  
  app.Get("/", func(ctx *banjo.Context) {
    ctx.JSON(banjo.M{"foo":"bar"})
  })

  app.Run()
}

Example responses:

// ... Redirect To
  app.Get("/admin", func(ctx *banjo.Context) {
    ctx.RedirectTo("/")
  })
// ... HTML
  app.Get("/foo", func(ctx *banjo.Context) {
    ctx.HTML("<h1>Hello from BONjO!</h1>")
  })
// ... Return Params as JSON
  app.Post("/bar", func(ctx *banjo.Context) {
    ctx.JSON(banjo.M{
      "params": ctx.Request.Params
    })
    ctx.Response.Status = 201
  })

License

banjo is primarily distributed under the terms of Mozilla Public License 2.0.

See LICENSE for details.

Documentation

Overview

Package banjo is main package file Banjo Package Allows you to create your own simple web application

See more in examples/example.go file

Index

Constants

View Source
const DefaultHost = "127.0.0.1"

DefaultHost is default application host value

View Source
const DefaultPort = "4321"

DefaultPort is default application port value

View Source
const DubSeparator = "\r\n\r\n"

DubSeparator is a default duble separator in HTTP Requests

View Source
const HTTPVersion = "HTTP/1.1"

HTTPVersion it'a default HTTP version

View Source
const Separator = "\r\n"

Separator is a default separator in HTTP Requests

Variables

This section is empty.

Functions

This section is empty.

Types

type Banjo

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

Banjo struct Main package Struct

func Create

func Create(config Config) Banjo

Create Constructor:

Initialize Banjo instance

Params: - config {Config} Banjo configuration

Response: - banjo {Banjo} Banjo configuration

func (Banjo) Delete

func (banjo Banjo) Delete(url string, closure func(ctx *Context))

Delete function For handling DELETE Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Get

func (banjo Banjo) Get(url string, closure func(ctx *Context))

Get function For handling GET Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Head

func (banjo Banjo) Head(url string, closure func(ctx *Context))

Head function For handling HEAD Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Options

func (banjo Banjo) Options(url string, closure func(ctx *Context))

Options function For handling OPTIONS Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Patch

func (banjo Banjo) Patch(url string, closure func(ctx *Context))

Patch function For handling PATCH Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Post

func (banjo Banjo) Post(url string, closure func(ctx *Context))

Post function For handling POST Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Put

func (banjo Banjo) Put(url string, closure func(ctx *Context))

Put function For handling PUT Requests

Params: - url {string} HTTP Request URL - closure {func(r Request) Response} Closure for handling HTTP Request

Response: - None

func (Banjo) Run

func (banjo Banjo) Run()

Run function

Application starts listening for the requests, Parse them and runs code save id routes fields as {map[string]func(r Request) Response} All closures should return Response, you can use banjo.JSON, banjo.HTML etc methods All return your own Response struct

This is last methods, that should called in the end of the application

Params: - None

Response: - None

type Config

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

Config struct

Allows you to create configuration to your banjo application

func DefaultConfig

func DefaultConfig() Config

DefaultConfig function

Returns default configurations for Banjo application

Params: - None

Response: - config {Config} Config struct

type Context

type Context struct {
	Request  Request
	Response Response
}

Context struct

For easy cooperate with Requests & Response in action flow

func (*Context) HTML

func (ctx *Context) HTML(data string)

HTML function

Prepared Headers to return HTML page

Params: - data {string} HTML content

Response: - None

func (*Context) JSON

func (ctx *Context) JSON(data map[string]interface{})

JSON function

This func allows you to easy returning a JSON response Example usage: app.JSON({"foo" : "bar"})

Params: - data {map[string]interface{}}

Response: - None

func (*Context) RedirectTo

func (ctx *Context) RedirectTo(url string)

RedirectTo function

Allows you to redirect to another page

Params: - url {string} path to redirect

Response: - None

type Logger

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

Logger struct

Provides logging with timestamps and different logging levels With saving all messages to log file

func CreateLogger

func CreateLogger() Logger

CreateLogger function

Returns Logger with default filePath

func (Logger) Critical

func (logger Logger) Critical(message string)

Critical function

Log message with log level CRITICAL

Params: - message {string}

Response: - None

func (Logger) Error

func (logger Logger) Error(message string)

Error function

Log message with log level ERROR

Params: - message {string}

Response: - None

func (Logger) Info

func (logger Logger) Info(message string)

Info function

Log message with log level INFO

Params: - message {string}

Response: - None

func (Logger) Warning

func (logger Logger) Warning(message string)

Warning function

Log message with log level WARNING

Params: - message {string}

Response: - None

type M

type M map[string]interface{}

M type is map[string]interface{} alias

type Parser

type Parser struct{}

Parser struct

Used for easy access to ParserIntr functions, this is empty struct

func (Parser) Request

func (p Parser) Request(rawData string) Request

Request function for parsing Raw HTTP Request to banjo.Request struct

Params: - data {string} Raw HTTP Request

Response: - request {banjo.Request}

func (Parser) Response

func (p Parser) Response(data Response) string

Response prepared banjo.Response struct to Raw HTTP Response string

Params: - data {banjo.Response} prepared banjo.Response struct

Response: - response {string} Raw HTTP Response string

type Request

type Request struct {
	Headers     map[string]string
	MapParams   map[string]string
	Files       []map[string]string
	Params      string
	Method      string
	URL         string
	HTTPVersion string
}

Request struct using for passing as parameter to callback functions.

type Response

type Response struct {
	Headers map[string]string
	Body    string
	Status  int
}

Response struct Using as returned value for callback function

type Routes

type Routes struct {
	GET     map[string]func(ctx *Context)
	POST    map[string]func(ctx *Context)
	PUT     map[string]func(ctx *Context)
	PATCH   map[string]func(ctx *Context)
	OPTIONS map[string]func(ctx *Context)
	HEAD    map[string]func(ctx *Context)
	DELETE  map[string]func(ctx *Context)
}

Routes struct

Used for storing users closures The struct have one map for each method table table type: map[string]func(ctx *Context)

func CreateRoutes

func CreateRoutes() Routes

CreateRoutes function

Create Routes with empty method tables

Params: - None

Response: - routes {Routes} Routes struct with empty map fields

func (Routes) Block

func (routes Routes) Block(method string, url string) func(ctx *Context)

Block function

Returns func(request Request) Response closure

Params: - method {string} HTTP Request Method - url {string} HTTP Request URL

Response: - closure {func(request Request) Response} Returns closure with user

func (Routes) Push

func (routes Routes) Push(method string, url string, closure func(ctx *Context))

Push function

Adding new element to one of fields in Routes struct Element should be passed by url {string}, value type of {func(request Request) Response}

Params: - method {string} HTTP Request Method - url {string} HTTP Request URL - closure {func(request Request) Response}

Response: - None

Jump to

Keyboard shortcuts

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