gifs/api
gifs/api/context.go
Upload is no longer async, memstorage is parallel-safe. Upload no longer needs to be run async (it can be run inside a goroutine), so it now returns stuff instead of taking a channel as an argument. This will make it easier to implement, as all the async stuff is an abstraction above, and therefore doesn't need to be worried about for each reimplementation. The memstorage type is no longer exported and can now be safely used by multiple goroutines, thanks to the sync package.
| paddy@0 | 1 package api |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "code.google.com/p/goauth2/oauth/jwt" |
| paddy@0 | 5 "code.google.com/p/google-api-go-client/storage/v1beta2" |
| paddy@0 | 6 _ "github.com/go-sql-driver/mysql" |
| paddy@0 | 7 ) |
| paddy@0 | 8 |
| paddy@0 | 9 type Context struct { |
| paddy@0 | 10 Storage Storage |
| paddy@0 | 11 Datastore Datastore |
| paddy@0 | 12 UsageTracker *UsageTracker |
| paddy@0 | 13 Bucket string |
| paddy@0 | 14 RootDomain string |
| paddy@0 | 15 } |
| paddy@0 | 16 |
| paddy@0 | 17 func NewMemStorage() Storage { |
| paddy@0 | 18 return Memstorage{} |
| paddy@0 | 19 } |
| paddy@0 | 20 |
| paddy@0 | 21 func NewMemDatastore() Datastore { |
| paddy@0 | 22 return &Memstore{ |
| paddy@0 | 23 collections: map[string]Collection{}, |
| paddy@0 | 24 domains: map[string]Domain{}, |
| paddy@0 | 25 items: map[string]Item{}, |
| paddy@0 | 26 users: map[string]User{}, |
| paddy@0 | 27 } |
| paddy@0 | 28 } |
| paddy@0 | 29 |
| paddy@0 | 30 func NewGCSStorage(gcsClientEmail, gcsTokenURI string, gcsPemBytes []byte) (Storage, error) { |
| paddy@0 | 31 t := jwt.NewToken(gcsClientEmail, storage.DevstorageFull_controlScope, gcsPemBytes) |
| paddy@0 | 32 t.ClaimSet.Aud = gcsTokenURI |
| paddy@0 | 33 transport, err := jwt.NewTransport(t) |
| paddy@0 | 34 if err != nil { |
| paddy@0 | 35 return nil, err |
| paddy@0 | 36 } |
| paddy@0 | 37 client := transport.Client() |
| paddy@0 | 38 gcsService, err := storage.New(client) |
| paddy@0 | 39 if err != nil { |
| paddy@0 | 40 return nil, err |
| paddy@0 | 41 } |
| paddy@0 | 42 return &GoogleCloudStorage{gcsService}, nil |
| paddy@0 | 43 } |