func main() { app.InitContex() defer app.GetContext().Destroy() api.EnsureIndexes() r := api.GetRouter() port := ":" + app.GetContext().Params.Port http.ListenAndServe(port, r) }
func logg(msg string) { context := app.GetContext() if context != nil && context.LogEntries != nil { context.LogEntries.Println(msg) } log.Println(msg) }
// EnsureIndexes sets the indexes for the Articles document func EnsureIndexes() { indexes := []mgo.Index{ mgo.Index{Key: []string{"slug"}, Unique: true, DropDups: true, Background: true}, mgo.Index{Key: []string{"-created_at"}, Background: true}, } doc := app.GetContext().DB.C("articles") for _, index := range indexes { if err := doc.EnsureIndex(index); err != nil { panic(err) } } }
func (req *Request) Error(e error) { if req == nil { return } err, casted := e.(*apierror.ApiError) if !casted { err = apierror.NewServerError(e.Error()).(*apierror.ApiError) } switch err.Code() { case http.StatusInternalServerError: logger.Errorf("%s - %s", err.Error(), req) http.Error(req.Response, `{"error":"Something went wrong"}`, http.StatusInternalServerError) default: if app.GetContext().Params.Debug { logger.Errorf("%s - %s", err.Error(), req) } http.Error(req.Response, fmt.Sprintf(`{"error":"%s"}`, err.Error()), err.Code()) } }
func Query() *mgo.Collection { return app.GetContext().DB.C("article") }