Ejemplo n.º 1
0
func Serve(port int, dbUrl string) {
	apiUrl = util.Getenv("API_URL")
	appUrl = util.Getenv("APP_URL")
	webUrl = util.Getenv("WEB_URL")
	webUrl = util.Getenv("BLOG_URL")

	appServer = util.NewServer()
	// Set up templates
	tBox := rice.MustFindBox("templates")
	sBox := rice.MustFindBox("static")
	appServer.RegisterTemplateLayout("l", "_login", tBox)
	appServer.RegisterTemplateLayout("s", "_signup", tBox)
	appServer.RegisterTemplateLayout("a", "_app", tBox)
	// Set up static files & 404
	appServer.Register404(tBox, "404.html")
	appServer.RegisterStaticHandler(sBox, "/static")

	// Open databse session
	var err error
	dbSession, err = mgo.Dial(dbUrl)
	if err != nil {
		panic(err)
	}
	data.Index(dbSession.DB(""))
	appServer.Database = dbSession
	appServer.DatabaseName = dbSession.DB("").Name

	// -----
	// App Routes
	// -----
	registerAccountHandlers(appServer.Router, appServer)
	registerSignupHandlers(appServer.Router, appServer)
	registerAuthHandlers(appServer.Router, appServer)
	registerHomeHandlers(appServer.Router, appServer)

	// Start the engines!
	fmt.Println("K ---> App running on port:", port)
	log.Fatal(appServer.Serve(port))
}
Ejemplo n.º 2
0
func signupPostHandler(c *util.Context) error {
	// Setup stripe client
	stripeClient := &stripe.Client{}
	stripeClient.Init(util.Getenv("STRIPE_PRIVATE_KEY"), nil, nil)

	// Gater form values
	fullname := c.Request.FormValue("fullname")
	accountName := c.Request.FormValue("account")
	email := c.Request.FormValue("email")
	password := c.Request.FormValue("password")
	plan := c.Request.FormValue("plan")
	token := c.Request.FormValue("stripeToken")

	// Store new user in mongo
	user := data.User{
		Email:    email,
		Fullname: fullname,
	}
	user.SetPassword(password)
	err := user.Create(c.C("users"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Now create his account
	account := data.Account{
		Name: accountName,
		Plan: plan,
	}
	account.Users = []bson.ObjectId{user.ID}
	err = account.Create(c.C("accounts"))
	if err != nil {
		log.Print(err)
		return err
	}

	// Create and Customer, subscribe him, start trial, associate card (1-step)
	customer := &stripe.CustomerParams{
		Token: token,
		Desc:  fullname,
		Email: email,
		Plan:  plan,
		Params: stripe.Params{
			Meta: map[string]string{
				"uid": user.ID.String(),
			},
		},
	}
	newCustomer, err := stripeClient.Customers.Create(customer)

	// Save new imformation (stripe id) and add account id to user accounts
	user.StripeId = newCustomer.Id
	user.Accounts = []bson.ObjectId{account.ID}
	c.C("users").UpdateId(user.ID, user)

	if err != nil {
		log.Print(err)
		return err
	}

	c.Redirect(c.RouteUrl("login") + "?next=" + url.QueryEscape(appUrl))
	return nil
}
Ejemplo n.º 3
0
import (
	"encoding/gob"
	"flag"
	"os"
	"os/signal"

	"github.com/joho/godotenv"
	"github.com/kiasaki/kauthie/admin"
	"github.com/kiasaki/kauthie/app"
	"github.com/kiasaki/kauthie/util"
	"github.com/kiasaki/kauthie/work"
	"gopkg.in/mgo.v2/bson"
)

var (
	DatabaseUrl = flag.String("database", util.Getenv("MONGOHQ_URL", util.Getenv("DATABASE_URL", "mongodb://localhost:27017/kauthie")), "Mongo database url")
	Port        = flag.Int("port", 1337, "Port to start the app server on")
	AdminPort   = flag.Int("admin_port", 1334, "Port to start the admin server on")

	StartApp   = flag.Bool("app", false, "Should kauthie start her app server?")
	StartWork  = flag.Bool("work", false, "Should kauthie start some workers?")
	StartAdmin = flag.Bool("admin", false, "Should kauthie start her admin server?")
)

func init() {
	gob.Register(bson.ObjectId(""))
}

func main() {
	godotenv.Load()
	flag.Parse()
Ejemplo n.º 4
0
func envHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/javascript")
	fmt.Fprintln(w, "var __env = __env || {};")
	fmt.Fprintf(w, "__env.stripePublishableKey = '%s';\n", util.Getenv("STRIPE_PUBLIC_KEY"))
}