gomagic

module
v0.0.0-...-842eef1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2016 License: MIT

README

About gomagic

gomagic is a middleware magicbox,it is not a framework,but a collection of useful middleware

Web Magic Useage

$ go get github.com/scottkiss/gomagic/webmagic
package main

import (
  "github.com/scottkiss/gomagic/webmagic"
  "log"
)

func main() {
  app := webmagic.Application()
  //handler get request
  //eg. http://localhost:8888/hello/100
  app.Get("/hello/:id", handler)
  //handler get request
  app.Get("/world/:id", handlerXml)
  //handler post request
  app.Post("/post", handlerPost)
  app.Run(":8888")
}

type User struct {
  Id   string
  Name string
}

//response json
func handler(ctx *webmagic.Context) {
  id := ctx.PathParam("id")
  log.Println(id)
  user := &User{Id: id, Name: "hello"}
  ctx.Output.Json(user, true)
}

//response xml
func handlerXml(ctx *webmagic.Context) {
  id := ctx.PathParam("id")
  log.Println(id)
  user := &User{Id: id, Name: "world"}
  ctx.Output.Xml(user)
}


func handlerPost(ctx *webmagic.Context) {
  user := &User{}
  ctx.Input.ReadJson(user)
  ctx.Output.Json(user, true)
}

License

View the LICENSE file

Directories

Path Synopsis
webmagic-middlewares

Jump to

Keyboard shortcuts

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