ducky/subscriptions

Paddy 2015-06-22

4:36e90e828dd0 Go to Latest

ducky/subscriptions/subscriptionsd/server.go

Add an API and subscriptionsd . Create a barebones implementation of the API, including only methods to create a Subscription and retrieve the Subscription associated with a user. Also create a subscriptiond service that will bootstrap the service and stores, and get everything stood up.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/subscriptionsd/server.go	Mon Jun 22 18:50:02 2015 -0400
     1.3 @@ -0,0 +1,42 @@
     1.4 +package main
     1.5 +
     1.6 +import (
     1.7 +	"log"
     1.8 +	"net/http"
     1.9 +	"os"
    1.10 +
    1.11 +	"code.secondbit.org/ducky/subscriptions.hg"
    1.12 +	"code.secondbit.org/ducky/subscriptions.hg/api"
    1.13 +	"code.secondbit.org/trout.hg"
    1.14 +
    1.15 +	"github.com/stripe/stripe-go"
    1.16 +
    1.17 +	"golang.org/x/net/context"
    1.18 +)
    1.19 +
    1.20 +func main() {
    1.21 +	log.SetFlags(log.LstdFlags | log.Llongfile)
    1.22 +	log.Printf("Running version '%s'\n", subscriptions.Version)
    1.23 +	var subscriptionStore subscriptions.SubscriptionStore
    1.24 +	if os.Getenv("SUBSCRIPTIONS_PG_DB") != "" {
    1.25 +		p, err := subscriptions.NewPostgres(os.Getenv("SUBSCRIPTIONS_PG_DB"))
    1.26 +		if err != nil {
    1.27 +			log.Fatal(err)
    1.28 +		}
    1.29 +		subscriptionStore = p
    1.30 +	} else {
    1.31 +		subscriptionStore = subscriptions.NewMemstore()
    1.32 +	}
    1.33 +	var stripeClient subscriptions.Stripe
    1.34 +	if os.Getenv("STRIPE_KEY") != "" {
    1.35 +		stripeClient = subscriptions.NewStripe(os.Getenv("STRIPE_KEY"), stripe.GetBackend(stripe.APIBackend))
    1.36 +	} else {
    1.37 +		log.Fatal("STRIPE_KEY environment variable must be set!")
    1.38 +	}
    1.39 +	c := api.WithStripeClient(stripeClient, api.WithSubscriptionStore(subscriptionStore, context.Background()))
    1.40 +	router := trout.Router{}
    1.41 +	api.HandleSubscriptions(&router, c)
    1.42 +	http.Handle("/", router)
    1.43 +	log.Println("Listening on 9001")
    1.44 +	log.Fatal(http.ListenAndServe("0.0.0.0:9001", nil))
    1.45 +}