auth

Paddy 2015-04-11 Parent:de5e09680f6b Child:0ff23f3a4ede

157:202e991accc2 Go to Latest

auth/authd/server.go

Wire up the postgres database for authd. Have authd use the AUTH_PG_DB environment variable to detect support for the postgres *Stores, and if postgres is supported, use it. If postgres isn't supported, fall back on the in-memory store. Also create-if-not-exists the test scopes, instead of panicking when the scope already exists.

History
     1.1 --- a/authd/server.go	Tue Apr 07 02:51:13 2015 -0400
     1.2 +++ b/authd/server.go	Sat Apr 11 14:13:52 2015 -0400
     1.3 @@ -4,6 +4,7 @@
     1.4  	"html/template"
     1.5  	"log"
     1.6  	"net/http"
     1.7 +	"os"
     1.8  
     1.9  	"code.secondbit.org/auth.hg"
    1.10  	"github.com/gorilla/mux"
    1.11 @@ -11,25 +12,30 @@
    1.12  
    1.13  func main() {
    1.14  	log.SetFlags(log.LstdFlags | log.Llongfile)
    1.15 -	p, err := auth.NewPostgres("dbname=testdb sslmode=disable")
    1.16 -	if err != nil {
    1.17 -		panic(err)
    1.18 +	var config auth.Config
    1.19 +	if os.Getenv("AUTH_PG_DB") != "" {
    1.20 +		p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
    1.21 +		if err != nil {
    1.22 +			panic(err)
    1.23 +		}
    1.24 +		config.ClientStore = &p
    1.25 +		config.AuthCodeStore = &p
    1.26 +		config.ProfileStore = &p
    1.27 +		config.TokenStore = &p
    1.28 +		config.SessionStore = &p
    1.29 +		config.ScopeStore = &p
    1.30 +	} else {
    1.31 +		store := auth.NewMemstore()
    1.32 +		config.ClientStore = store
    1.33 +		config.AuthCodeStore = store
    1.34 +		config.ProfileStore = store
    1.35 +		config.TokenStore = store
    1.36 +		config.SessionStore = store
    1.37 +		config.ScopeStore = store
    1.38  	}
    1.39 -	store := auth.NewMemstore()
    1.40 -	if err != nil {
    1.41 -		panic(err)
    1.42 -	}
    1.43 -	config := auth.Config{
    1.44 -		ClientStore:   &p,
    1.45 -		AuthCodeStore: store,
    1.46 -		ProfileStore:  &p,
    1.47 -		TokenStore:    store,
    1.48 -		SessionStore:  store,
    1.49 -		ScopeStore:    &p,
    1.50 -		Template:      template.Must(template.New("base").ParseGlob("./templates/*.gotmpl")),
    1.51 -		LoginURI:      "/login",
    1.52 -	}
    1.53 -	err = config.Init()
    1.54 +	config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
    1.55 +	config.LoginURI = "/login"
    1.56 +	err := config.Init()
    1.57  	if err != nil {
    1.58  		log.Fatal(err)
    1.59  	}
    1.60 @@ -40,8 +46,8 @@
    1.61  	err = context.CreateScopes([]auth.Scope{
    1.62  		{ID: "testscope", Name: "Test Scope"},
    1.63  	})
    1.64 -	if err != nil {
    1.65 -		panic(err)
    1.66 +	if err != nil && err != auth.ErrScopeAlreadyExists {
    1.67 +		log.Fatal(err)
    1.68  	}
    1.69  
    1.70  	router := mux.NewRouter()