Help us shape the future of Go! Take the 2026 Go Developer Survey

goa

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2019 License: MIT Imports: 9 Imported by: 3

README

Goa

CI Codecov Go Doc Go Report PR's Welcome

Goa is under construction, if you are familiar with koa or go and interested in this project, please join us.

What is goa?

goa = go + koa

Just like koa, goa is also not bundled with any middleware. It is flexible, light and extensible.

Installation

$ go get github.com/goa-go/goa

Hello Goa

func main() {
  app := goa.New()

  app.Use(func(c *goa.Context, next func()) {
    c.Body = "Hello Goa!"
    next()
  })
  log.Fatal(app.Listen(":3000"))
}

Middleware

Goa is a web framework based on middleware. Here is an example of using goa-router and logger.

package main

import (
  "fmt"
  "time"

  "github.com/goa-go/goa"
  "github.com/goa-go/goa/router"
)

func logger(c *goa.Context, next func()) {
  start := time.Now()

  fmt.Printf(
    "[%s] <-- %s %s\n",
    start.Format("2006-6-2 15:04:05"),
    c.Method,
    c.URL,
  )
  next()
  fmt.Printf(
    "[%s] --> %s %s %d %s\n",
    time.Now().Format("2006-6-2 15:04:05"),
    c.Method,
    c.URL,
    time.Since(start).Nanoseconds()/1e6,
    "ms",
  )
}

func main() {
  app := goa.New()
  router := router.New()

  router.GET("/", func(c *goa.Context) {
    c.Body = "Hello Goa!"
  })

  app.Use(logger)
  app.Use(router.Routes())
  log.Fatal(app.Listen(":3000"))
}

If you are unwilling to use goa-router, you can make a custom router middleware as you like. With middleware, you can expand functionality to meet your needs at will.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Request        *http.Request
	ResponseWriter http.ResponseWriter

	Method string
	URL    *url.URL
	Path   string
	Header http.Header

	Params Params
	Keys   map[string]interface{}

	// Response status code.
	Status int

	// Content-Type
	Type string

	// Body will be wrote in response,
	// use it just like `c.Body = ...`.
	// Only string and struct will be supported.
	// If body is struct, will parse it as json.
	// If u want to respond xml or other type data, u can `c.XML(...)` or encode it into string.
	Body interface{}
	// contains filtered or unexported fields
}

Context is used to receive requests and respond to requests.

func (*Context) Error added in v0.2.0

func (c *Context) Error(err Error)

Error throw a http-error.

func (*Context) Get

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

Get value, return (value, exists).

func (*Context) GetQuery

func (c *Context) GetQuery(key string) (string, bool)

GetQuery returns the keyed url query value and isExit if it exists, return (value, true) otherwise it returns ("", false)

func (*Context) GetQueryArray

func (c *Context) GetQueryArray(key string) ([]string, bool)

GetQueryArray returns a slice of value for a given query key. And returns whether at least one value exists for the given key.

func (*Context) HTML added in v0.2.0

func (c *Context) HTML(str string)

HTML responds html.

func (*Context) JSON

func (c *Context) JSON(json interface{})

JSON responds json-data.

func (*Context) Param

func (c *Context) Param(key string) string

Param returns the value of the URL param or "". When using goa-router, it works.

func (*Context) Parse added in v0.2.0

func (c *Context) Parse(p parser.Parser)

Parse handles parser.

func (*Context) ParseJSON added in v0.2.0

func (c *Context) ParseJSON(pointer interface{})

ParseJSON parses json-data, require a pointer.

func (*Context) ParseString added in v0.2.0

func (c *Context) ParseString() string

ParseString returns string-data

func (*Context) ParseXML added in v0.2.0

func (c *Context) ParseXML(pointer interface{})

ParseXML parses xml-data, require a pointer.

func (*Context) PostForm

func (c *Context) PostForm(key string) string

PostForm returns the value from a POST form or "".

func (*Context) Query

func (c *Context) Query(key string) string

Query returns the keyed url query value or ""

func (*Context) Redirect

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

Redirect replies to the request with a redirect to url and a status code.

func (*Context) Set

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

Set value.

func (*Context) SetHeader added in v0.2.0

func (c *Context) SetHeader(key string, value string)

SetHeader sets http response header. It should be called before Status and Respond.

func (*Context) String

func (c *Context) String(str string)

String responds string-data.

func (*Context) XML

func (c *Context) XML(xml interface{})

XML responds xml-data.

type Error added in v0.2.0

type Error struct {
	Msg    string
	Status int
}

Error is used like c.Error(goa.Error{...}). It will create a http-error.

type Goa

type Goa struct {
	Context *Context
	// contains filtered or unexported fields
}

Goa is the framework's instance.

func New

func New() *Goa

New returns the initialized Goa instance.

func (*Goa) ComposeMiddlewares added in v0.2.0

func (app *Goa) ComposeMiddlewares()

ComposeMiddlewares composes middleware, it doesn't need to be called manually except in testing, but httptest is only available after ComposeMiddlewares is called.

func (*Goa) Listen

func (app *Goa) Listen(addr string) error

Listen starts server with the addr.

func (*Goa) ServeHTTP

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

func (*Goa) Use

func (app *Goa) Use(m Middleware)

Use a middleware.

type M

type M map[string]interface{}

M is a convenient alias for a map[string]interface{} map. Use is as c.JSON(&goa.M{...})

type Middleware

type Middleware func(*Context, func())

Middleware is based part of goa, any processing will take place here. should be used liked app.Use(middleware).

type Middlewares

type Middlewares []Middleware

Middlewares is []Middleware.

type Param

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

type Params

type Params []Param

Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.

func (Params) Get

func (ps Params) Get(name string) string

Get returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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