Esempio n. 1
0
func main() {
	app.InitContex()
	defer app.GetContext().Destroy()

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

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

	log.Println(msg)
}
Esempio n. 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)
		}
	}
}
Esempio n. 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())
	}
}
Esempio n. 5
0
func Query() *mgo.Collection {
	return app.GetContext().DB.C("article")
}