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

goa

package module
v0.1.1 Latest Latest
Warning

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

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

README

Goa

CI 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

Installation

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

Hello Goa

func main() {
  app := New()

  app.Use(func(c *goa.Context, next func()) {
    c.String("Hello Goa!")
    next()
  })
  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 json(c *goa.Context) {
  c.JSON(goa.M{
    "string": "string",
    "int":    1,
    "json": goa.M{
      "key": "value",
    },
  })
}

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

  router.GET("/", func(c *goa.Context) {
    c.String("hello world")
  })
  router.GET("/json", json)

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

License

MIT

Documentation

Overview

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

	Params Params
	Keys   map[string]interface{}
	// contains filtered or unexported fields
}

func (*Context) Get

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

Context 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) JSON

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

func (*Context) Param

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

Param returns the value of the URL param or "".

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, location string)

func (*Context) Render

func (c *Context) Render(data []byte)

func (*Context) Set

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

Context set value.

func (*Context) Status

func (c *Context) Status(code int) *Context

Status sets the HTTP response code. And return context, so c.Status(200).JSON(...) is supported.

func (*Context) String

func (c *Context) String(str string)

func (*Context) XML

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

type Goa

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

func New

func New() *Goa

func (*Goa) Listen

func (app *Goa) Listen(addr string)

func (*Goa) ServeHTTP

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

func (*Goa) Use

func (app *Goa) Use(m Middleware)

type HandleRequest

type HandleRequest func(*Context)

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

type Middlewares

type Middlewares []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
test-server
test-server

Jump to

Keyboard shortcuts

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