diff --git a/cmd/tokidoki/main.go b/cmd/tokidoki/main.go index 94f64ec..0375380 100644 --- a/cmd/tokidoki/main.go +++ b/cmd/tokidoki/main.go @@ -16,15 +16,16 @@ import ( func main() { var ( - addr string - authURL string + addr string + authURL string + storageURL string ) flag.StringVar(&addr, "addr", ":8080", "listening address") - // TODO: make flag mandatory instead of default value - flag.StringVar(&authURL, "auth.url", "imaps://imap.migadu.com:993", "auth backend URL") + flag.StringVar(&authURL, "auth.url", "", "auth backend URL (required)") + flag.StringVar(&storageURL, "storage.url", "", "storage backend URL (required)") flag.Parse() - if len(flag.Args()) != 0 { + if len(flag.Args()) != 0 || authURL == "" || storageURL == "" { flag.Usage() os.Exit(1) } @@ -41,8 +42,7 @@ func main() { } mux.Use(authProvider.Middleware()) - //backend := storage.NewPostgreSQL() - backend, err := storage.NewFilesystem("./teststorage") + backend, err := storage.NewFromURL(storageURL) if err != nil { log.Fatalf("failed to load storage backend: %s", err.Error()) } diff --git a/storage/url.go b/storage/url.go new file mode 100644 index 0000000..2cdd891 --- /dev/null +++ b/storage/url.go @@ -0,0 +1,24 @@ +package storage + +import ( + "fmt" + "net/url" + + "github.com/emersion/go-webdav/carddav" +) + +func NewFromURL(storageURL string) (carddav.Backend, error) { + u, err := url.Parse(storageURL) + if err != nil { + return nil, fmt.Errorf("error parsing storage URL: %s", err.Error()) + } + + switch u.Scheme { + case "file": + return NewFilesystem(u.Path) + case "postgresql": + return NewPostgreSQL(), nil + default: + return nil, fmt.Errorf("no storage provider found for %s:// URL", u.Scheme) + } +}