-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstori.go
61 lines (51 loc) · 1.4 KB
/
stori.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package storih
import (
"net/http"
"cloud.google.com/go/storage"
"github.com/noypi/gae"
logih "github.com/noypi/gae/logi/handler"
"github.com/noypi/gae/registry"
"github.com/noypi/router"
"google.golang.org/api/option"
)
type _gaestoriKey int
const (
GAEStoriKey _gaestoriKey = iota
)
func GetGAEStori(c router.Store) gae.StorInt {
if o, exists := c.Get(GAEStoriKey); exists {
return o.(gae.StorInt)
}
return nil
}
func AddGAEStori(storiname, jwtpath string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c := router.ContextW(w)
ERR := router.GetErrLog(c)
gaectx := gae.GetGAEContext(c)
logger := logih.GetGAELogi(c)
ts, err := gae.GetTokenSource(gaectx, jwtpath, storage.ScopeFullControl)
if nil != err {
ERR.PrintStackTrace(5)
ERR.Ln("AddGAEStori: failed to create TokenSource, err=", err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
o, err := registry.GetStori(storiname, map[string]interface{}{
gae.StoriBucket: "",
gae.StoriContext: gaectx,
gae.StoriLogger: logger,
gae.StoriOpts: []option.ClientOption{
option.WithScopes(storage.ScopeFullControl),
option.WithTokenSource(ts),
},
})
if nil != err {
ERR.PrintStackTrace(5)
ERR.Ln("AddGAEStori: failed to create GAE StorageInt, err=", err.Error())
c.AbortWithStatus(http.StatusInternalServerError)
return
}
c.Set(GAEStoriKey, o)
}
}