| OLD | NEW |
| 1 package frontend | 1 package frontend |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "encoding/json" |
| 4 "html/template" | 5 "html/template" |
| 5 "net/http" | 6 "net/http" |
| 6 "time" | 7 "time" |
| 7 | 8 |
| 8 "google.golang.org/appengine" | 9 "google.golang.org/appengine" |
| 9 | 10 |
| 11 "github.com/luci/gae/service/datastore" |
| 10 "github.com/luci/luci-go/appengine/gaemiddleware" | 12 "github.com/luci/luci-go/appengine/gaemiddleware" |
| 13 "github.com/luci/luci-go/common/logging" |
| 11 "github.com/luci/luci-go/server/router" | 14 "github.com/luci/luci-go/server/router" |
| 12 "github.com/luci/luci-go/server/templates" | 15 "github.com/luci/luci-go/server/templates" |
| 13 ) | 16 ) |
| 14 | 17 |
| 18 const ( |
| 19 defaultQueueName = "default" |
| 20 deleteKeysQueueName = "delete-keys" |
| 21 ) |
| 22 |
| 15 func init() { | 23 func init() { |
| 16 r := router.New() | 24 r := router.New() |
| 17 baseMW := base() | |
| 18 | 25 |
| 19 » r.GET("/testfile", baseMW, getHandler) | 26 » baseMW := gaemiddleware.BaseProd() |
| 20 » r.GET("/testfile/", baseMW, getHandler) | 27 » getMW := baseMW.Extend(templatesMiddleware()) |
| 28 |
| 29 » gaemiddleware.InstallHandlers(r, baseMW) |
| 30 |
| 31 » r.GET("/testfile", getMW, getHandler) |
| 32 » r.GET("/testfile/", getMW, getHandler) |
| 33 » r.POST("/testfile/upload", baseMW.Extend(withParsedUploadForm), uploadHa
ndler) |
| 34 |
| 35 » r.GET("/builders", baseMW, getBuildersHandler) |
| 36 » r.GET("/updatebuilders", baseMW, updateBuildersHandler) |
| 37 » r.GET("/builderstate", baseMW, getBuilderStateHandler) |
| 38 » r.GET("/updatebuilderstate", baseMW, updateBuilderStateHandler) |
| 39 |
| 40 » r.POST( |
| 41 » » "/internal/delete-keys", |
| 42 » » baseMW.Extend(gaemiddleware.RequireTaskQueue(deleteKeysQueueName
)), |
| 43 » » deleteKeysHandler, |
| 44 » ) |
| 21 | 45 |
| 22 http.DefaultServeMux.Handle("/", r) | 46 http.DefaultServeMux.Handle("/", r) |
| 23 } | 47 } |
| 24 | 48 |
| 25 // base returns the root middleware chain. | 49 // templatesMiddleware returns the templates middleware. |
| 26 func base() router.MiddlewareChain { | 50 func templatesMiddleware() router.Middleware { |
| 27 » templateBundle := &templates.Bundle{ | 51 » return templates.WithTemplates(&templates.Bundle{ |
| 28 Loader: templates.FileSystemLoader("templates"), | 52 Loader: templates.FileSystemLoader("templates"), |
| 29 DebugMode: appengine.IsDevAppServer(), | 53 DebugMode: appengine.IsDevAppServer(), |
| 30 FuncMap: template.FuncMap{ | 54 FuncMap: template.FuncMap{ |
| 31 "timeParams": func(t time.Time) string { | 55 "timeParams": func(t time.Time) string { |
| 32 return t.Format(paramsTimeFormat) | 56 return t.Format(paramsTimeFormat) |
| 33 }, | 57 }, |
| 34 "timeJS": func(t time.Time) int64 { | 58 "timeJS": func(t time.Time) int64 { |
| 35 return t.Unix() * 1000 | 59 return t.Unix() * 1000 |
| 36 }, | 60 }, |
| 37 }, | 61 }, |
| 62 }) |
| 63 } |
| 64 |
| 65 // deleteKeysHandler is task queue handler for deleting keys. |
| 66 func deleteKeysHandler(ctx *router.Context) { |
| 67 c, w, r := ctx.Context, ctx.Writer, ctx.Request |
| 68 |
| 69 keys := struct { |
| 70 Keys []string `json:"keys"` |
| 71 }{} |
| 72 if err := json.NewDecoder(r.Body).Decode(&keys); err != nil { |
| 73 logging.WithError(err).Errorf(c, "deleteKeysHandler: error decod
ing") |
| 74 w.WriteHeader(http.StatusOK) // Prevent retrying with the same r
.Body. |
| 75 return |
| 38 } | 76 } |
| 39 | 77 |
| 40 » return gaemiddleware.BaseProd().Extend( | 78 » dkeys := make([]*datastore.Key, 0, len(keys.Keys)) |
| 41 » » templates.WithTemplates(templateBundle), | 79 » for _, k := range keys.Keys { |
| 42 » ) | 80 » » dk, err := datastore.NewKeyEncoded(k) |
| 81 » » if err != nil { |
| 82 » » » logging.WithError(err).Errorf(c, "deleteKeysHandler") |
| 83 » » » w.WriteHeader(http.StatusInternalServerError) |
| 84 » » » return |
| 85 » » } |
| 86 » » dkeys = append(dkeys, dk) |
| 87 » } |
| 88 |
| 89 » if err := datastore.Get(c).Delete(dkeys); err != nil { |
| 90 » » logging.WithError(err).Errorf(c, "deleteKeysHandler") |
| 91 » » w.WriteHeader(http.StatusInternalServerError) |
| 92 » » return |
| 93 » } |
| 94 |
| 95 » w.WriteHeader(http.StatusOK) |
| 43 } | 96 } |
| OLD | NEW |