Skip to content

How to implement a manager

nvcnvn edited this page Dec 9, 2014 · 3 revisions

Implement a new manager help you port the app to different database. This can be done by these step:

  1. Implement Manager (click the link for interface detail)
  2. Implement Configurator
  3. "Overide" auth.HANDLER_REGISTER

###Step 1, 2 Quite simple, but maybe a lot of work. You can find some test in https://github.com/kidstuff/auth-mongo-mngr/ to make sure your Manager and Configurator work.

###Step 3 Implement an http.Handler and call auth.BasicMngrHandler in ServeHTTP method.
For example, the auth-mongo-mngr does:

type mongoMngrHandler struct {
	db   *mgo.Database
	fn   auth.HandleFunc
	cond auth.Condition
}

func (h mongoMngrHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	cloneDB := h.db.Session.Clone().DB(h.db.Name)
	defer cloneDB.Session.Close()

	ctx := auth.AuthContext{}
	ctx.Auth = NewMgoManager(cloneDB)
	ctx.Settings = NewMgoConfigMngr(cloneDB)
	auth.BasicMngrHandler(&ctx, rw, req, &h.cond, h.fn)
}

Then "overide" the auth.HANDLER_REGISTER in your initial function.

func Initial(db *mgo.Database) {
	auth.HANDLER_REGISTER = func(fn auth.HandleFunc, owner bool, pri []string) http.Handler {
		return mongoMngrHandler{
			db: db,
			fn: fn,
			cond: auth.Condition{
				RequiredPri: pri,
				Owner:       owner,
			},
		}
	}
}

The auth-mongo-mngr require that developer must explicit call Initial function to choose which database to use in their application.

Clone this wiki locally