ducky/subscriptions
2015-07-18
Parent:36e90e828dd0
ducky/subscriptions/subscriptionsd/server.go
Return errors from responses in client. When the client makes a request, non-200 responses _are not_ considered errors. So we need to check the response.Errors property, and if it has errors, _then_ we consider the request to have an error. To make this happen, we created an httpErrors type that fulfills the error interface and just wraps the response Errors property. Then callers can type-cast it and interrogate it.
1 package main
3 import (
4 "log"
5 "net/http"
6 "os"
8 "code.secondbit.org/ducky/subscriptions.hg"
9 "code.secondbit.org/ducky/subscriptions.hg/api"
10 "code.secondbit.org/trout.hg"
12 "github.com/stripe/stripe-go"
14 "golang.org/x/net/context"
15 )
17 func main() {
18 log.SetFlags(log.LstdFlags | log.Llongfile)
19 log.Printf("Running version '%s'\n", subscriptions.Version)
20 var subscriptionStore subscriptions.SubscriptionStore
21 if os.Getenv("SUBSCRIPTIONS_PG_DB") != "" {
22 p, err := subscriptions.NewPostgres(os.Getenv("SUBSCRIPTIONS_PG_DB"))
23 if err != nil {
24 log.Fatal(err)
25 }
26 subscriptionStore = p
27 } else {
28 subscriptionStore = subscriptions.NewMemstore()
29 }
30 var stripeClient subscriptions.Stripe
31 if os.Getenv("STRIPE_KEY") != "" {
32 stripeClient = subscriptions.NewStripe(os.Getenv("STRIPE_KEY"), stripe.GetBackend(stripe.APIBackend))
33 } else {
34 log.Fatal("STRIPE_KEY environment variable must be set!")
35 }
36 c := api.WithStripeClient(stripeClient, api.WithSubscriptionStore(subscriptionStore, context.Background()))
37 router := trout.Router{}
38 api.HandleSubscriptions(&router, c)
39 http.Handle("/", router)
40 log.Println("Listening on 9001")
41 log.Fatal(http.ListenAndServe("0.0.0.0:9001", nil))
42 }