示例#1
0
func ApiRouter(userRepository *repository.UserRepository, tRep *repository.TranslationRepository, tr t.Translator) http.Handler {
	transRepository = tRep
	translator = tr

	render = r.New(r.Options{})

	r := mux.NewRouter()
	r.HandleFunc("/", wrap(Default)).Methods("GET")
	r.HandleFunc("/translations", wrap(ApiTranslate)).Methods("GET")
	r.HandleFunc("/translations", wrap(Create)).Methods("POST")
	r.HandleFunc("/translations/{id:[a-z0-9-]+}", wrap(Save)).Methods("POST")
	r.HandleFunc("/translations/{id}", wrap(Get)).Methods("GET")
	r.HandleFunc("/translations/{id}/{lang:[a-z]{2}}", wrap(GetParticular)).Methods("GET")
	r.HandleFunc("/translations/{id}", wrap(Delete)).Methods("DELETE")
	r.HandleFunc("/translations/{id}/{lang:[a-z]{2}}", wrap(DeleteParticular)).Methods("DELETE")

	app := negroni.New()
	//	app.Use(negroni.NewRecovery())
	app.Use(negroni.NewLogger())
	app.Use(middleware.NewAuthMiddleware(
		"X-Auth-Key",
		"X-Auth-Secret",
		middleware.AuthConfig{
			Context: func(r *http.Request, authenticatedKey string) {
				context.Set(r, 0, authenticatedKey)
			},
			Client: func(key, secret string) bool {
				//				sec := userRepository.GetSecretByKey(key)
				log.Println(key, secret)
				return true
			},
		},
	))
	c := cors.New(cors.Options{})
	app.Use(c)
	app.UseHandler(r)

	return context.ClearHandler(app)
}
示例#2
0
文件: api.go 项目: postfix/pw
package main

import (
	"net/http"

	"github.com/zenazn/goji/web"
	renderpkg "gopkg.in/unrolled/render.v1"
)

type M map[string]interface{}

var (
	render = renderpkg.New(renderpkg.Options{})
)

func ApiGetVaults(c web.C, w http.ResponseWriter, r *http.Request) {
	store := c.Env["store"].(*PwStore)

	render.JSON(w, http.StatusOK, M{
		"vaults": store.vaults,
	})
}

func ApiGetVault(c web.C, w http.ResponseWriter, r *http.Request) {
	store := c.Env["store"].(*PwStore)
	vaultName := c.URLParams["vault"]

	var vault *Vault
	for _, v := range store.vaults {
		if v.Name == vaultName {
			vault = v
示例#3
0
	"net/http"

	log "github.com/Sirupsen/logrus"
	"github.com/codegangsta/negroni"
	"github.com/ericflo/gradientzoo/blobstorage"
	"github.com/ericflo/gradientzoo/models"
	"github.com/ericflo/gradientzoo/utils"
	"github.com/julienschmidt/httprouter"
	negronilogrus "github.com/meatballhat/negroni-logrus"
	"github.com/phyber/negroni-gzip/gzip"
	render "gopkg.in/unrolled/render.v1"
)

type Handler func(c *Context, w http.ResponseWriter, req *http.Request)

var rndr *render.Render = render.New()
var api *models.ApiCollection
var blob blobstorage.BlobStorage

func handle(handler Handler) httprouter.Handle {
	return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
		var authToken *models.AuthToken
		var user *models.User
		if authTokenId := req.Header.Get("X-Auth-Token-Id"); authTokenId != "" {
			var err error
			if authToken, err = api.AuthToken.ById(authTokenId); err != nil {
				log.WithFields(log.Fields{
					"authTokenId": authTokenId,
					"err":         err.Error(),
				}).Error("Could not get auth token by id")
			} else if authToken != nil {