From 63efefedd49623dbcfbe1810b0d137b4e7b5800e Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Mon, 21 Aug 2023 12:23:35 +0100
Subject: [PATCH] Implement a filesystem storage backend

Fixes #8.
---
 cmd/acmeserver/acmeserver.go |  2 ++
 upload/config.go             | 42 ++++++++++++++++++++++++++++++++++--
 upload/replds.go             |  4 ++--
 3 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/cmd/acmeserver/acmeserver.go b/cmd/acmeserver/acmeserver.go
index 87209209..1792bf68 100644
--- a/cmd/acmeserver/acmeserver.go
+++ b/cmd/acmeserver/acmeserver.go
@@ -58,6 +58,8 @@ func defaultConfig() *Config {
 	config.ACME.HTTP.Enabled = true
 	config.ACME.AccountKeyPath = "/var/lib/acme/account.key"
 	config.ACME.KeyType = common.KeyTypeECDSA
+	config.Output.Type = "file"
+	config.Output.FS.Path = "/var/lib/acme/certs"
 	return &config
 }
 
diff --git a/upload/config.go b/upload/config.go
index c499a61a..8b66e76b 100644
--- a/upload/config.go
+++ b/upload/config.go
@@ -1,7 +1,13 @@
 package upload
 
-// Config for the output storage layer.
-type Config struct {
+import (
+	"context"
+	"fmt"
+
+	"git.autistici.org/ai3/tools/acmeserver/common"
+)
+
+type ReplDSConfig struct {
 	Endpoint string `yaml:"endpoint"`
 	Prefix   string `yaml:"prefix"`
 	TLS      struct {
@@ -10,3 +16,35 @@ type Config struct {
 		CA   string `yaml:"ca"`
 	} `yaml:"tls"`
 }
+
+type FSConfig struct {
+	Path string `yaml:"path"`
+}
+
+const (
+	configTypeFS     = "file"
+	configTypeReplDS = "replds"
+)
+
+// Config for the output storage layer.
+type Config struct {
+	Type   string       `yaml:"type"`
+	ReplDS ReplDSConfig `yaml:"replds"`
+	FS     FSConfig     `yaml:"file"`
+}
+
+// Uploader is the interface to the storage backend.
+type Uploader interface {
+	Upload(context.Context, string, *common.Credentials) error
+}
+
+func New(config *Config) (Uploader, error) {
+	switch config.Type {
+	case configTypeFS:
+		return newFS(&config.FS)
+	case configTypeReplDS:
+		return newReplDS(&config.ReplDS)
+	default:
+		return nil, fmt.Errorf("unknown storage type '%s'", config.Type)
+	}
+}
diff --git a/upload/replds.go b/upload/replds.go
index caf0139a..5ca71f60 100644
--- a/upload/replds.go
+++ b/upload/replds.go
@@ -40,8 +40,8 @@ type ReplStorage struct {
 	conn   *grpc.ClientConn
 }
 
-// New creates a new ReplStorage.
-func New(config *Config) (*ReplStorage, error) {
+// newReplDS creates a new ReplStorage.
+func newReplDS(config *ReplDSConfig) (*ReplStorage, error) {
 	var creds credentials.TransportCredentials
 
 	if config.TLS.Cert != "" && config.TLS.Key != "" && config.TLS.CA != "" {
-- 
GitLab