gokoa

package module
v0.0.0-...-1f1f7cd Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2020 License: MIT Imports: 10 Imported by: 0

README

GoKoa

GitHub Workflow Status GitHub repo size GitHub last commit GitHub

Koa styled web framework written in Go.

Read this in other languages: English | 简体中文

Table of Contents

Introduction

GoKoa is a Koa styled web framework, which aims at reducing learning cost in grasping web development in Go for Node.js developers.

GoKoa is written in Go, which is beneficial to improving performance in handling HTTP requests.

Prerequisites

  1. Go >= 1.13

Installation

You can easily fetch the newest version of GoKoa by executing go get:

$ go get github.com/azxj/gokoa

Quick Start

package main

import (
	"github.com/azxj/gokoa"
)

func main() {
	app := gokoa.NewApplication(nil)

	app.Use(func(ctx *gokoa.Context, fn func() error) error {
		ctx.SetBody("hello gokoa")
		return nil
	})

	app.Listen(8080)
}

What Are NOT Implemented

  1. lack of event emitting, except for app.OnError()
  2. not able to customize HTTP reason phrase (don't do that cause it breaks the best practice)
  3. not able to bypass GoKoa's response handling (actually it is deprecated by Koa, too)
  4. not able to access the socket related to a HTTP connection
  5. lack of headerSent property

Documentation

Application
Context
Request
Response

Development

How To Test

You can easily run unit tests by executing go test:

$ go test github.com/azxj/gokoa

Documentation

Overview

Package gokoa is a Koa styled web framework written in Go.

package main

import (
	"github.com/azxj/gokoa"
)

func main() {
	app := gokoa.NewApplication(nil)

	app.Use(func(ctx *gokoa.Context, fn func() error) error {
		ctx.SetBody("hello gokoa")
		return nil
	})

	app.Listen(8080)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application struct {

	// Env is the deploying environment variable, default to GOKOA_ENV
	// or "development".
	Env string

	// Keys is the signed cookie keys, which will be used to sign and
	// verify client cookies, default to an empty array.
	Keys []string

	// Proxy is equal to true when fields in the proxy header will be
	// trusted, default to false.
	Proxy bool

	// SubdomainOffset is the offset of subdomain to be ignored, default
	// to 0 (means no ignoring).
	SubdomainOffset int
	// contains filtered or unexported fields
}

An Application represents a HTTP web server, which handle HTTP requests by processing HTTP responses.

func NewApplication

func NewApplication(config ApplicationConfig) *Application

NewApplication returns a new Application initialized with the given config.

The config can be nil, which causes the Application to use default configuration settings.

Values in key-value pairs must be in the valid type, otherwise NewApplication will panic.

func (*Application) Callback

func (app *Application) Callback() func(res http.ResponseWriter, req *http.Request)

Callback returns a function that composes all middlewares registered into the Application, creates a new Context, a new Request and a new Response for an incoming connection, and handles this HTTP request.

func (*Application) Listen

func (app *Application) Listen(port int) (*http.Server, error)

Listen causes the Application to create a new HTTP server with a single handler which composes all middlewares registered in, and listen on the given TCP port for incoming connections.

Listen returns the created http.Server when ListenAndServe() does NOT returns an error, otherwise returns it.

func (*Application) OnError

func (app *Application) OnError(handler ErrorHandler)

OnError registers a new ErrorHandler into the Application.

func (*Application) Use

func (app *Application) Use(middleware Middleware) *Application

Use registers the given middleware into the Application.

Use returns the Application itself, which enables chained function call instead of function calls in multiple lines.

type ApplicationConfig

type ApplicationConfig map[string]interface{}

An ApplicationConfig is a container which stores settings for configuring an Application.

The ApplicationConfig is organized as key-value pairs, where the value is of limited type.

type Context

type Context struct {
	Request  *Request
	Response *Response

	// State is the recommended namespace for passing information
	// through different middlewares.
	State map[string]interface{}
	// contains filtered or unexported fields
}

A Context contains information related to a single HTTP request.

func NewContext

func NewContext() *Context

NewContext returns a new empty Context.

NewContext allocates memory for State, which means that a key-value pair can be directly added to State, without calling make() by yourself.

func (*Context) GetBody

func (ctx *Context) GetBody() []byte

func (*Context) GetStatus

func (ctx *Context) GetStatus() int

func (*Context) SetBody

func (ctx *Context) SetBody(body interface{})

func (*Context) SetStatus

func (ctx *Context) SetStatus(statusCode int)

type ErrorHandler

type ErrorHandler func(err error)

An ErrorHandler is a function, which is responsible for handling error returned by any middleware.

type Middleware

type Middleware func(ctx *Context, next func() error) error

A Middleware is a single function, which will be registered into an Application to be executed during HTTP request handling.

type Request

type Request struct {
	// Req is the primitive HTTP request.
	Req *http.Request
	// contains filtered or unexported fields
}

A Request represents a HTTP request received by the Application.

func NewRequest

func NewRequest() *Request

NewRequest returns a new empty Request.

func (*Request) GetMethod

func (request *Request) GetMethod() string

GetMethod returns the HTTP request method.

type Response

type Response struct {
	// Res is the primitive HTTP response.
	Res http.ResponseWriter
	// contains filtered or unexported fields
}

A Response represents a HTTP response sent back by the Application.

func NewResponse

func NewResponse() *Response

NewResponse returns a new empty Response.

func (*Response) Get

func (response *Response) Get(field string) string

Get returns the value corresponding to the key in the HTTP response header.

func (*Response) GetBody

func (response *Response) GetBody() []byte

GetBody returns the HTTP response body.

func (*Response) GetLength

func (response *Response) GetLength() int

GetLength returns the HTTP response Content-Length header.

func (*Response) GetStatus

func (response *Response) GetStatus() int

GetStatus returns the HTTP response status code.

func (*Response) Has

func (response *Response) Has(field string) bool

Has returns true when the specific key-value pair exists in the HTTP header.

func (*Response) Remove

func (response *Response) Remove(field string)

Remove deletes the key-value pair from the HTTP response header.

func (*Response) Set

func (response *Response) Set(field string, value string)

Set assigns the key-value pair to the HTTP response header.

func (*Response) SetBody

func (response *Response) SetBody(body interface{}) error

SetBody assigns the given object to the HTTP response body.

The given object can be either a string, a byte array, an io.Reader, or a map containing key-value pairs. Whenever what type the given object is, it will be transformed into a byte array.

func (*Response) SetLength

func (response *Response) SetLength(length int)

SetLength assigns the given integer to the HTTP response Content-Length header.

func (*Response) SetStatus

func (response *Response) SetStatus(statusCode int)

SetStatus assigns the given integer to the HTTP status code.

func (*Response) SetType

func (response *Response) SetType(contentType string)

SetType assigns the given string to the HTTP response Content-Type header.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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