testserver tweaks.

This commit is contained in:
Will Charczuk 2016-07-09 09:11:47 -07:00
parent 80d6be9849
commit f22f247d3b

View file

@ -8,12 +8,18 @@ import (
"github.com/wcharczuk/go-web" "github.com/wcharczuk/go-web"
) )
func main() { func chartHandler(rc *web.RequestContext) web.ControllerResult {
app := web.New() format, err := rc.RouteParameter("format")
app.SetName("Chart Test Server") if err != nil {
app.SetLogger(web.NewStandardOutputLogger()) format = "png"
app.GET("/", func(rc *web.RequestContext) web.ControllerResult { }
if format == "png" {
rc.Response.Header().Set("Content-Type", "image/png") rc.Response.Header().Set("Content-Type", "image/png")
} else {
rc.Response.Header().Set("Content-Type", "image/svg+xml")
}
c := chart.Chart{ c := chart.Chart{
Title: "A Test Chart", Title: "A Test Chart",
TitleStyle: chart.Style{ TitleStyle: chart.Style{
@ -46,12 +52,23 @@ func main() {
}, },
} }
err := c.Render(chart.PNG, rc.Response) if format == "png" {
err = c.Render(chart.PNG, rc.Response)
} else {
err = c.Render(chart.SVG, rc.Response)
}
if err != nil { if err != nil {
return rc.API().InternalError(err) return rc.API().InternalError(err)
} }
rc.Response.WriteHeader(http.StatusOK) rc.Response.WriteHeader(http.StatusOK)
return nil return nil
}) }
func main() {
app := web.New()
app.SetName("Chart Test Server")
app.SetLogger(web.NewStandardOutputLogger())
app.GET("/", chartHandler)
app.GET("/:format", chartHandler)
log.Fatal(app.Start()) log.Fatal(app.Start())
} }