neo

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2017 License: MIT Imports: 17 Imported by: 10

README

Neo

Build Status GoDoc

Go Web Framework

Installation

# framework
go get github.com/ivpusic/neo

# CLI tool
go get github.com/ivpusic/neo/cmd/neo

Documentation

Project Site

API Documentation

Example

Create Neo application:

neo new myapp
cd myapp
package main

import (
    "github.com/ivpusic/neo"
)

func main() {
    app := neo.App()

    app.Get("/", func(ctx *neo.Ctx) (int, error) {
        return 200, ctx.Res.Text("I am Neo Programmer")
    })

    app.Start()
}

Run it:

neo run main.go

License

MIT

Documentation

Overview

Go Web Framework.

Hello world example:

package main

import (
    "github.com/ivpusic/neo"
)

func main() {
   app := neo.App()

   app.Get("/", func(this *neo.Ctx) {
       this.Res.Text("I am Neo Programmer", 200)
   })

   app.Start()
}

Here you can find Neo API documentation. For tutorials visit project website. http://178.62.122.135/

Index

Constants

View Source
const (
	BYTE     = 1.0
	KILOBYTE = 1024 * BYTE
	MEGABYTE = 1024 * KILOBYTE
	GIGABYTE = 1024 * MEGABYTE
	TERABYTE = 1024 * GIGABYTE
)
View Source
const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	DELETE  = "DELETE"
	OPTIONS = "OPTIONS"
	HEAD    = "HEAD"
)

supported HTTP methods

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application struct {
	// Event emmiter/receiver
	ebus.EBus

	// Application Configuration parameters
	Conf *Conf

	// Application logger instance
	Logger *golog.Logger
	// contains filtered or unexported fields
}

Representing Neo application instance

func App

func App() *Application

Getting Neo Application instance. This is singleton function. First time when we call this method function will try to parse configuration for Neo application. It will look for configuration file provided by “--config“ CLI argument (if exist).

func (*Application) Region

func (a *Application) Region() *Region

Making new region instance. You can create multiple regions.

func (*Application) Serve

func (a *Application) Serve(url string, path string)

Defining paths for serving static files. For example if we say: a.Serve("/some", "./mypath") then if we require “/some/js/file.js“, Neo will look for file at “./mypath/js/file.js“.

func (*Application) ServeHTTP

func (a *Application) ServeHTTP(w http.ResponseWriter, req *http.Request)

Handler interface “ServeHTTP“ implementation. Method will accept all incomming HTTP requests, and pass requests to appropriate handlers if they are defined.

func (*Application) Start

func (a *Application) Start()

Starting application instance. This will run application on port defined by configuration.

func (*Application) Templates

func (a *Application) Templates(templates ...string)

If you are planning to return templates from Neo route handler, then you have to compile them. This method will accept list of paths/files and compile them. You can use also paths with wildcards (example: /some/path/*).

type ApplicationConf

type ApplicationConf struct {
	Test         string
	Args         []string
	Addr         string
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	// default to http.DefaultMaxHeaderBytes
	MaxHeaderBytes int
	// Default 10MB
	MaxBodyBytes int64
	Logger       LoggerConf
}

type Conf

type Conf struct {
	Hotreload HotReloadConf
	App       ApplicationConf
	Neo       NeoConf
}

Neo Application configuration

func (*Conf) Parse

func (c *Conf) Parse(path string)

Will try to parse TOML configuration file.

type Cookie map[string]*http.Cookie

Map of cookies which will be sent to client.

func (Cookie) Del

func (cookies Cookie) Del(key string)

func (Cookie) Get

func (cookies Cookie) Get(key string) *http.Cookie

func (Cookie) Set

func (cookies Cookie) Set(key string, value string)

func (Cookie) SetCustom

func (cookies Cookie) SetCustom(cookie *http.Cookie)

Set *http.Cookie instance to Response

type Ctx

type Ctx struct {
	ebus.EBus

	// Wrapped http.Request object.
	Req *Request

	// Object with utility methods for writing responses to http.ResponseWriter
	Res *Response

	// Context data map
	Data CtxData

	// general purpose session instance
	Session Session

	// list of errors happened in current http request
	Errors []error
}

Representing context for this request.

func (*Ctx) Error

func (c *Ctx) Error(err error)

Append error to list of errors for current http request. This function can be used from any part of your code which has access to current context

func (*Ctx) HasErrors

func (c *Ctx) HasErrors() bool

HasErrors checks if we have errors in current http request

type CtxData

type CtxData map[string]interface{}

Map which will hold your Request contextual data.

func (CtxData) Del

func (r CtxData) Del(key string)

Delete contextual data on key

func (CtxData) Get

func (r CtxData) Get(key string) interface{}

Get context data on key

func (CtxData) Set

func (r CtxData) Set(key string, value interface{})

Add new contextual data on key with provided value

type HotReloadConf

type HotReloadConf struct {
	Suffixes []string
	Ignore   []string
}

type LoggerConf

type LoggerConf struct {
	Name  string
	Level string
}

type Middleware

type Middleware func(*Ctx, Next)

Representing middleware handler. It accepts Neo Context and Next function for calling next middleware in chain.

type NeoConf

type NeoConf struct {
	Logger LoggerConf
}

type Next

type Next func()

type Region

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

func (Region) Delete

func (m Region) Delete(path string, fn handler) *Route

Registering route handler for “DELETE“ request on provided path

func (Region) Get

func (m Region) Get(path string, fn handler) *Route

Registering route handler for “GET“ request on provided path

func (Region) Head

func (m Region) Head(path string, fn handler) *Route

Registering route handler for “HEAD“ request on provided path

func (Region) Options

func (m Region) Options(path string, fn handler) *Route

Registering route handler for “OPTIONS“ request on provided path

func (Region) Post

func (m Region) Post(path string, fn handler) *Route

Registering route handler for “POST“ request on provided path

func (*Region) Prefix added in v0.2.0

func (r *Region) Prefix(prefix string) *Region

func (Region) Put

func (m Region) Put(path string, fn handler) *Route

Registering route handler for “PUT“ request on provided path

func (Region) Use

func (m Region) Use(fn Middleware)

Adding new middleware into chain of middlewares.

type Request

type Request struct {
	*http.Request
	Params        UrlParam
	ResolvedRoute string
}

Wrapped http.Request. It contains utility methods for dealing with content of incomming http.Request instance.

func (*Request) JsonBody

func (r *Request) JsonBody(instance interface{}) error

Parse incomming Body as JSON

func (*Request) StringBody

func (r *Request) StringBody() (string, error)

type Response

type Response struct {
	Status int

	Cookie Cookie
	// contains filtered or unexported fields
}

Server response representation.

func (*Response) File

func (r *Response) File(path string) error

Find file, and send it to client.

func (*Response) Header

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

Get Header. Implements ResponseWriter.Header.

func (*Response) Json

func (r *Response) Json(obj interface{}) error

Will produce JSON string representation of passed object, and send it to client

func (*Response) Raw

func (r *Response) Raw(data []byte) error

Send Raw data to client.

func (*Response) Redirect

func (r *Response) Redirect(url string) error

Redirect to url with status

func (*Response) SkipFlush added in v0.2.0

func (r *Response) SkipFlush()

If it is called, Neo will skip calling ResponseWriter's write method. Usecase for this is when we render HTML template for example, because Neo uses Go html/template for writing to ResponseWriter.

func (*Response) Text

func (r *Response) Text(text string) error

Will send provided Text to client.

func (*Response) Tpl

func (r *Response) Tpl(name string, data interface{}) error

Will look for template, render it, and send rendered HTML to client. Second argument is data which will be passed to client.

func (*Response) Write

func (r *Response) Write(b []byte) (int, error)

Write raw response. Implements ResponseWriter.Write.

func (*Response) WriteHeader

func (r *Response) WriteHeader(s int)

Write Header. Implements ResponseWriter.WriterHeader.

func (*Response) Writer

func (r *Response) Writer() http.ResponseWriter

Get http.ResponseWriter directly

func (*Response) Xml

func (r *Response) Xml(obj interface{}) error

Will produce XML string representation of passed object, and send it to client

type Route

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

func (Route) Use

func (m Route) Use(fn Middleware)

Adding new middleware into chain of middlewares.

type Session

type Session struct {
	// is user authenticated?
	Authenticated bool
	// data about logged user
	User interface{}
	// other data
	Data interface{}
}

Purpose of this struct is to be used by authentication and authorization middlewares. Saving session is common in many web applications, and this struct is trying to provide type-safe version of it.

type Static

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

func (*Static) Serve

func (s *Static) Serve(url string, path string)

Serving static files from some directory. If provided url is for example “/assets“, then all requests starting with “/assets“ will be served from directory provided by “path“ parameter.

type UrlParam

type UrlParam map[string]string

Representing named url parameters. For example if we have url like: “/some/:name/other/:lastname“, then named parameters are “name“ and “lastname“.

func (UrlParam) Exist

func (u UrlParam) Exist(key string) bool

func (UrlParam) Get

func (u UrlParam) Get(key string) string

Directories

Path Synopsis
cmd
neo
middlewares
logger
Middleware for logging all requests and respones to Neo server.
Middleware for logging all requests and respones to Neo server.

Jump to

Keyboard shortcuts

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