goa

package module
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2019 License: MIT Imports: 9 Imported by: 3

README

Goa

Build Status Codecov Go Doc Go Report Mentioned in Awesome Go 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. But you can expand functionality to meet your needs at will by middlware. It is flexible, light, high-performance and extensible.

Installation

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

Hello Goa

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

  app.Use(func(c *goa.Context) {
    c.String("Hello Goa!")
  })
  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"
  "log"
  "time"

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

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

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

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

  r.GET("/", func(c *goa.Context) {
    c.String("Hello Goa!")
  })

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

If you are unwilling to use goa-router, you can make a custom router middleware as you like.

Maintainers

@NicholasCao.

License

MIT

Documentation

Overview

Package goa implements a web framework called goa.

See https://goa-go.github.io for more information about goa.

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{}

	// whether handled response by c.ResponseWriter
	Handled bool
	// contains filtered or unexported fields
}

Context is used to receive requests and respond to requests.

func (*Context) Cookie added in v0.4.1

func (c *Context) Cookie(name string) (string, error)

Cookie returns the named cookie provided in the request or ErrNoCookie if not found.

func (*Context) Error added in v0.2.0

func (c *Context) Error(code int, msg string)

Error throw a http-error, it would be catched by goa.

func (*Context) FormFile added in v0.4.2

func (c *Context) FormFile(name string) (multipart.File, *multipart.FileHeader, error)

FormFile returns the first file for the provided form key.

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) GetStatus added in v0.3.0

func (c *Context) GetStatus() int

GetStatus returns c.status.

func (*Context) HTML added in v0.2.0

func (c *Context) HTML(html string)

HTML responds html.

func (*Context) JSON

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

JSON responds json-data.

func (*Context) Next added in v0.4.0

func (c *Context) Next()

Next implements the next middleware. For example,

app.Use(func(c *goa.Context) {
  //do sth
  c.Next()
  //do sth
})

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) ParseForm added in v0.2.1

func (c *Context) ParseForm(pointer interface{}) error

ParseForm can parse form-data and x-www-form-urlencoded, the latter is not available when the request method is get, require a pointer. Just like json, it also needs a "form" tag. Here is a example.

type Person struct {
	Name string `form:"name"`
	Age  int    `form:"age"`
}

p := &Person{} c.ParseForm(p)

func (*Context) ParseJSON added in v0.2.0

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

ParseJSON parses json-data, require a pointer.

func (*Context) ParseQuery added in v0.2.1

func (c *Context) ParseQuery(pointer interface{}) error

ParseQuery can parse query, require a pointer. Just like json, it also needs a "query" tag. Here is a example.

type Person struct {
	Name string `query:"name"`
	Age  int    `query:"age"`
}

p := &Person{} c.ParseQuery(p)

func (*Context) ParseString added in v0.2.0

func (c *Context) ParseString() (string, error)

ParseString returns string-data

func (*Context) ParseXML added in v0.2.0

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

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) SetCookie added in v0.4.1

func (c *Context) SetCookie(cookie *http.Cookie)

SetCookie adds a Set-Cookie header to the ResponseWriter's headers.

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) Status

func (c *Context) Status(code int)

Status sets the HTTP response code.

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 {
	Code int
	Msg  string
}

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

type Goa

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

Goa is the framework's instance.

func New

func New() *Goa

New returns the initialized Goa instance.

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)

ServeHTTP makes the app implement the http.Handler interface.

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)

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