示例#1
0
func main() {
	app.InitContex()
	defer app.GetContext().Destroy()

	api.EnsureIndexes()
	r := api.GetRouter()
	port := ":" + app.GetContext().Params.Port
	http.ListenAndServe(port, r)
}
示例#2
0
func logg(msg string) {
	context := app.GetContext()

	if context != nil && context.LogEntries != nil {
		context.LogEntries.Println(msg)
	}

	log.Println(msg)
}
示例#3
0
// 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)
		}
	}
}
示例#4
0
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())
	}
}
示例#5
0
func Query() *mgo.Collection {
	return app.GetContext().DB.C("article")
}