auth

Paddy 2015-12-14 Parent:b7e685839a1b

182:cd5f07f9811b Go to Latest

auth/authd/server.go

Update nsq import path. go-nsq has moved to nsqio/go-nsq, so we need to update the import path appropriately.

History
1 package main
3 import (
4 "encoding/base64"
5 "html/template"
6 "log"
7 "net/http"
8 "os"
10 "code.secondbit.org/auth.hg"
11 "code.secondbit.org/events.hg"
12 "github.com/gorilla/mux"
13 )
15 func main() {
16 log.SetFlags(log.LstdFlags | log.Llongfile)
17 log.Printf("Running version '%s'\n", auth.Version)
18 var config auth.Config
19 var jwtSecret string
20 var err error
21 if os.Getenv("JWT_SECRET") == "" {
22 log.Fatal("JWT_SECRET must be set.")
23 } else {
24 jwtSecret = os.Getenv("JWT_SECRET")
25 }
26 if os.Getenv("JWT_SECRET_IS_BASE64_ENCODED") == "true" {
27 config.JWTPrivateKey, err = base64.StdEncoding.DecodeString(jwtSecret)
28 if err != nil {
29 panic(err)
30 }
31 } else {
32 config.JWTPrivateKey = []byte(jwtSecret)
33 }
34 if os.Getenv("AUTH_PG_DB") != "" {
35 p, err := auth.NewPostgres(os.Getenv("AUTH_PG_DB"))
36 if err != nil {
37 panic(err)
38 }
39 config.ClientStore = &p
40 config.AuthCodeStore = &p
41 config.ProfileStore = &p
42 config.TokenStore = &p
43 config.SessionStore = &p
44 } else {
45 store := auth.NewMemstore()
46 config.ClientStore = store
47 config.AuthCodeStore = store
48 config.ProfileStore = store
49 config.TokenStore = store
50 config.SessionStore = store
51 }
52 config.Template = template.Must(template.New("base").ParseGlob("./templates/*.gotmpl"))
53 config.LoginURI = "/login"
54 if os.Getenv("AUTH_NSQD_ADDR") != "" {
55 publisher, err := events.NewNSQPublisher("code.secondbit.org/auth/authd-"+auth.Version, os.Getenv("AUTH_NSQD_ADDR"))
56 if err != nil {
57 log.Fatal(err)
58 }
59 config.EventsPublisher = publisher
60 } else {
61 config.EventsPublisher = events.NewStdoutPublisher()
62 }
63 err = config.Init()
64 if err != nil {
65 log.Fatal(err)
66 }
67 context, err := auth.NewContext(config)
68 if err != nil {
69 panic(err)
70 }
72 router := mux.NewRouter()
73 auth.RegisterOAuth2(router, context)
74 auth.RegisterSessionHandlers(router, context)
75 auth.RegisterProfileHandlers(router, context)
76 auth.RegisterClientHandlers(router, context)
77 http.Handle("/", router)
78 log.Println("Listening on port 9000")
79 log.Fatal(http.ListenAndServe("0.0.0.0:9000", nil))
80 }