2020-10-15 15:02:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/acme/autocert"
|
|
|
|
)
|
|
|
|
|
2020-11-09 15:40:12 +00:00
|
|
|
type autocertCache struct{}
|
2020-10-15 15:02:58 +00:00
|
|
|
|
|
|
|
func (c *autocertCache) Get(ctx context.Context, key string) ([]byte, error) {
|
|
|
|
var data []byte
|
2020-11-09 15:40:12 +00:00
|
|
|
row, err := appDbQueryRow("select data from autocert where key = @key", sql.Named("key", key))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = row.Scan(&data)
|
2020-10-15 15:02:58 +00:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, autocert.ErrCacheMiss
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *autocertCache) Put(ctx context.Context, key string, data []byte) error {
|
|
|
|
startWritingToDb()
|
|
|
|
defer finishWritingToDb()
|
2020-11-09 15:40:12 +00:00
|
|
|
_, err := appDbExec("insert or replace into autocert (key, data, created) values (@key, @data, @created)", sql.Named("key", key), sql.Named("data", data), sql.Named("created", time.Now().String()))
|
2020-10-15 15:02:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *autocertCache) Delete(ctx context.Context, key string) error {
|
|
|
|
startWritingToDb()
|
|
|
|
defer finishWritingToDb()
|
2020-11-09 15:40:12 +00:00
|
|
|
_, err := appDbExec("delete from autocert where key = @key", sql.Named("key", key))
|
2020-10-15 15:02:58 +00:00
|
|
|
return err
|
|
|
|
}
|