Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Package middleware provides a standard set of middleware tools for luci | |
| 6 // appengine apps. It's built around "github.com/julienschmidt/httprouter". | |
| 7 // | |
| 8 // Usage Example | |
| 9 // | |
| 10 // Middleware is pretty basic to use. You pick one of the 'Base' functions, | |
| 11 // then layer middlewares, making the innermost middleware your actual handler | |
| 12 // function. | |
| 13 // | |
| 14 // BaseProd and BaseTest ensure that the context has a full compliment of | |
| 15 // luci/gae services, as well as a luci-go/common/logging service. | |
| 16 // | |
| 17 // import ( | |
|
dnj
2015/10/13 04:39:10
WDYT about moving this into a test example?
iannucci
2015/10/13 04:52:29
I don't think it would work? Is there a way to do
| |
| 18 // "log" | |
| 19 // | |
| 20 // "github.com/julienschmidt/httprouter" | |
| 21 // "github.com/luci/gae/service/datastore" | |
| 22 // "github.com/luci/luci-go/appengine/middleware" | |
| 23 // "github.com/luci/luci-go/common/logging" | |
| 24 // ) | |
| 25 // | |
| 26 // // Thing is just a silly datastore model for the example. | |
| 27 // type Thing struct{ | |
| 28 // ID string `gae:"$id"` | |
| 29 // } | |
| 30 // | |
| 31 // func myHandler(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) { | |
| 32 // if err := datastore.Get(c).Put(&Thing{ID: p.ByName("thing_id")}); err != nil { | |
| 33 // logging.Errorf(c, "failed to put thing: %s", err) | |
| 34 // fmt.Fprintf("error: %s", err) | |
| 35 // rw.WriteHeader(500) | |
| 36 // return | |
| 37 // } | |
| 38 // rw.Write([]byte("ok")) | |
| 39 // } | |
| 40 // | |
| 41 // func init() { | |
| 42 // router := httprouter.New() | |
| 43 // router.GET("/internal/make_thing/:obj_id", | |
| 44 // middleware.BaseProd(middleware.RequireCron(myHandler))) | |
| 45 // | |
| 46 // log.Fatal(http.ListenAndServe(":8080", router)) | |
| 47 // } | |
| 48 package middleware | |
| OLD | NEW |