aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/nsheridan/autocert-wkfs-cache
diff options
context:
space:
mode:
authorNiall Sheridan <nsheridan@gmail.com>2017-01-13 00:06:46 +0000
committerNiall Sheridan <nsheridan@gmail.com>2017-01-13 20:56:41 +0000
commit57224ffa79aac59155a0f6a4ad47f224cac736fd (patch)
tree763b75c73fa6755f5d3733f24ec474081e987f5b /vendor/github.com/nsheridan/autocert-wkfs-cache
parentadca4cec3bf97d73e082d4c986443ffc8fee6fe1 (diff)
Use wkfs to manage the lets encrypt cache
Diffstat (limited to 'vendor/github.com/nsheridan/autocert-wkfs-cache')
-rw-r--r--vendor/github.com/nsheridan/autocert-wkfs-cache/cache.go85
1 files changed, 85 insertions, 0 deletions
diff --git a/vendor/github.com/nsheridan/autocert-wkfs-cache/cache.go b/vendor/github.com/nsheridan/autocert-wkfs-cache/cache.go
new file mode 100644
index 0000000..e829ef2
--- /dev/null
+++ b/vendor/github.com/nsheridan/autocert-wkfs-cache/cache.go
@@ -0,0 +1,85 @@
+package wkfscache
+
+import (
+ "os"
+ "path/filepath"
+
+ "go4.org/wkfs"
+
+ "golang.org/x/crypto/acme/autocert"
+ "golang.org/x/net/context"
+)
+
+type Cache string
+
+// Get reads a certificate data from the specified file name.
+func (d Cache) Get(ctx context.Context, name string) ([]byte, error) {
+ name = filepath.Join(string(d), name)
+ var (
+ data []byte
+ err error
+ done = make(chan struct{})
+ )
+ go func() {
+ data, err = wkfs.ReadFile(name)
+ close(done)
+ }()
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case <-done:
+ }
+ if os.IsNotExist(err) {
+ return nil, autocert.ErrCacheMiss
+ }
+ return data, err
+}
+
+// Put writes the certificate data to the specified file name.
+// The file will be created with 0600 permissions.
+func (d Cache) Put(ctx context.Context, name string, data []byte) error {
+ if err := wkfs.MkdirAll(string(d), 0700); err != nil {
+ return err
+ }
+
+ done := make(chan struct{})
+ var err error
+ go func() {
+ defer close(done)
+ if err := wkfs.WriteFile(filepath.Join(string(d), name), data, 0600); err != nil {
+ return
+ }
+ // prevent overwriting the file if the context was cancelled
+ if ctx.Err() != nil {
+ return // no need to set err
+ }
+ }()
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-done:
+ }
+ return err
+}
+
+// Delete removes the specified file name.
+func (d Cache) Delete(ctx context.Context, name string) error {
+ name = filepath.Join(string(d), name)
+ var (
+ err error
+ done = make(chan struct{})
+ )
+ go func() {
+ err = wkfs.Remove(name)
+ close(done)
+ }()
+ select {
+ case <-ctx.Done():
+ return ctx.Err()
+ case <-done:
+ }
+ if err != nil && !os.IsNotExist(err) {
+ return err
+ }
+ return nil
+}