Beispiel #1
0
func main() {
	if !checkFlags() {
		os.Exit(1)
	}

	sessionStore := sessions.NewCookieStore([]byte(*sessionHashKey), []byte(*sessionBlockKey))

	// OpenID Connect Providers

	// Google
	oidcGoogleLoginRoute := "/logingoogle"
	oidcGoogleCBRoute := "/gcallback"
	oidcGoogle := &oidc.Google{
		ClientID:     *oidcGoogleClientID,
		ClientSecret: *oidcGoogleClientSecret,
		RedirectURI:  *publicURL + oidcGoogleCBRoute,
		SessionStore: sessionStore,
	}

	// PayPal
	oidcPaypalLoginRoute := "/loginpaypal"
	oidcPaypalCBRoute := "/pcallback"
	oidcPaypal := &oidc.Paypal{
		ClientID:     *oidcPaypalClientID,
		ClientSecret: *oidcPaypalClientSecret,
		RedirectURI:  *publicURL + oidcPaypalCBRoute,
		SessionStore: sessionStore,
	}

	// Dynamodb
	cfg := &aws.Config{}
	if *dynamodbEndpoint != "" {
		cfg.Endpoint = aws.String(*dynamodbEndpoint)
	}
	sess := session.New(cfg)
	if *debug {
		sess.Config.LogLevel = aws.LogLevel(aws.LogDebug)
	}

	// Model
	var m model.Model
	m = awsdynamo.NewModelFromSession(sess)

	// Controller
	// OAuth / OpenID Connect
	authCGoogle := controller.NewAuthController(m.UserPeer(), oidcGoogle, "google")
	authCPaypal := controller.NewAuthController(m.UserPeer(), oidcPaypal, "paypal")

	// Post Controller
	postContrData := &postDataProvider{
		PostPeer: m.PostPeer(),
		UserPeer: m.UserPeer(),
	}
	postController := &controller.PostController{
		Model: postContrData,
	}

	// Middleware
	baseChain := xhandler.Chain{}
	baseChain.UseC(xhandler.TimeoutHandler(2 * time.Second))

	// Session management
	sessionMiddleware := middleware.Session{}
	sessionMiddleware.Init([]byte(*sessionHashKey), []byte(*sessionBlockKey))
	baseChain.UseC(sessionMiddleware.Enable("posty-session"))

	// Chain for authenticated routes
	authedChain := xhandler.Chain{}
	authedChain = append(authedChain, baseChain...)
	authedChain.UseC(middleware.AuthenticatedFilter("/login"))
	authedChain.UseC(middleware.UserContext())

	// Chain for authenticated routes with json response
	jsonChain := xhandler.Chain{}
	jsonChain = append(jsonChain, authedChain...)
	jsonChain.UseC(middleware.JSONWrapper())

	// Chain for unauthenticated routes
	unauthedChain := xhandler.Chain{}
	unauthedChain = append(unauthedChain, baseChain...)
	unauthedChain.UseC(middleware.UnauthenticatedFilter("/"))

	// Main Context
	ctx := context.Background()
	route := func(chain xhandler.Chain, handler xhandler.HandlerC) web.Handler {
		return handle(ctx, chain.HandlerC(handler))
	}

	// Routes
	mux := web.New()
	mux.Get("/api/posts", route(jsonChain, xhandler.HandlerFuncC(postController.Posts)))
	mux.Post("/api/posts", route(jsonChain, xhandler.HandlerFuncC(postController.Create)))
	mux.Delete("/api/posts/:id", route(jsonChain, xhandler.HandlerFuncC(postController.Remove)))
	// OIDC Routes
	mux.Get(oidcGoogleLoginRoute, route(unauthedChain, authCGoogle.Login()))
	mux.Get(oidcGoogleCBRoute, route(unauthedChain, authCGoogle.Callback("/")))
	mux.Get(oidcPaypalLoginRoute, route(unauthedChain, authCPaypal.Login()))
	mux.Get(oidcPaypalCBRoute, route(unauthedChain, authCPaypal.Callback("/")))
	mux.Get("/logout", route(authedChain, authCGoogle.Logout("/login")))

	// Static file
	mux.Get("/login", route(unauthedChain, serveSingleFile(filepath.Join(*frontendPath, "login.html"))))
	mux.Get("/", route(authedChain, serveSingleFile(filepath.Join(*frontendPath, "index.html"))))
	mux.Get("/static/*", route(baseChain, serveFiles(filepath.Join(*frontendPath, "/static"), "/static/")))

	log.Infof("Listening on %s", *listen)
	log.Fatal(http.ListenAndServe(":8080", gctx.ClearHandler(mux)))
}
Beispiel #2
0
func setup() {
	mmodel = awsdynamo.NewModelFromSession(sess)
}