2017-04-28 21:57:10 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2017-04-29 05:36:11 +00:00
|
|
|
"github.com/asdine/storm"
|
2017-04-28 21:57:10 +00:00
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Storage is the parent interface for the database backends of route.
|
|
|
|
type Storage interface {
|
|
|
|
// routes
|
2017-12-03 02:00:13 +00:00
|
|
|
GetRoute(ctx context.Context, id string) (Route, error)
|
2017-04-29 02:51:49 +00:00
|
|
|
GetAllRoutes(ctx context.Context, user string) ([]Route, error)
|
2017-04-28 21:57:10 +00:00
|
|
|
PutRoute(ctx context.Context, domain, kind string) (Route, error)
|
|
|
|
DeleteRoute(ctx context.Context, id string) error
|
|
|
|
|
|
|
|
// tokens
|
|
|
|
GetToken(ctx context.Context, token string) (Token, error)
|
2017-04-29 02:47:24 +00:00
|
|
|
GetTokenID(ctx context.Context, id string) (Token, error)
|
2017-04-28 21:57:10 +00:00
|
|
|
GetTokensForOwner(ctx context.Context, owner string) ([]Token, error)
|
|
|
|
PutToken(ctx context.Context, token, owner string, scopes []string) (Token, error)
|
|
|
|
DeleteToken(ctx context.Context, id string) error
|
|
|
|
DeactivateToken(ctx context.Context, id string) error
|
|
|
|
|
|
|
|
// certificates
|
|
|
|
GetCert(ctx context.Context, key string) ([]byte, error)
|
|
|
|
PutCert(ctx context.Context, key string, data []byte) error
|
|
|
|
DeleteCert(ctx context.Context, key string) error
|
2017-04-29 04:18:19 +00:00
|
|
|
|
|
|
|
Close() error
|
2017-04-28 21:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type storageManager struct {
|
|
|
|
Storage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storageManager) Get(ctx context.Context, key string) ([]byte, error) {
|
2017-04-29 05:36:11 +00:00
|
|
|
data, err := s.GetCert(ctx, key)
|
|
|
|
if err != nil {
|
|
|
|
if err == storm.ErrNotFound {
|
|
|
|
return nil, autocert.ErrCacheMiss
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
2017-04-28 21:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storageManager) Put(ctx context.Context, key string, data []byte) error {
|
|
|
|
return s.PutCert(ctx, key, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *storageManager) Delete(ctx context.Context, key string) error {
|
|
|
|
return s.DeleteCert(ctx, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache creates an autocert.Cache from a Storage instance.
|
|
|
|
func Cache(s Storage) autocert.Cache {
|
|
|
|
return autocert.Cache(&storageManager{
|
|
|
|
Storage: s,
|
|
|
|
})
|
|
|
|
}
|