# README
Go remoteStorage Library
An implementation of the remoteStorage protocol written in Go.
go get -u github.com/cvanloo/rmsgo
Example Usage
package main
import (
"os"
"github.com/cvanloo/rmsgo"
)
const (
PersistFile = "/var/rms/persist"
RemoteRoot = "/storage/"
StorageRoot = "/var/rms/storage/"
)
func main() {
opts, err := rmsgo.Configure(RemoteRoot, StorageRoot)
if err != nil {
log.Fatal(err)
}
opts.UseErrorHandler(func(err error) {
log.Fatalf("remote storage: unhandled error: %v", err)
})
opts.UseAuthentication(func(r *http.Request, bearer string) (rmsgo.User, bool) {
// [!] TODO: Your authentication logic here...
// Return one of your own users.
return rmsgo.ReadWriteUser{}, true
})
persistFile, err := os.Open(PersistFile)
if err != nil {
log.Fatal(err)
}
// Restore server state
err = rmsgo.Load(persistFile)
if err != nil {
log.Fatal(err)
}
// Register remote storage endpoints to the http.DefaultServeMux
rmsgo.Register(nil)
http.ListenAndServe(":8080", nil) // [!] TODO: Use TLS
// At shutdown: persist server state
err = rmsgo.Persist(persistFile)
if err != nil {
log.Fatal(err)
}
}
With Request Logging
func logger(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lrw := rmsgo.NewLoggingResponseWriter(w)
// [!] pass request on to remote storage server
next.ServeHTTP(lrw, r)
duration := time.Since(start)
// - Mom! Can we have slog?
// - No darling, we have slog at home.
// slog at home:
log.Printf("%v", map[string]any{
"method": r.Method,
"uri": r.RequestURI,
"duration": duration,
"status": lrw.Status,
"size": lrw.Size,
})
})
}
func main() {
opts, err := rmsgo.Configure(RemoteRoot, StorageRoot)
if err != nil {
log.Fatal(err)
}
// [!] Register custom middleware
opts.UseMiddleware(logger)
// [!] Other configuration...
rmsgo.Register(nil)
http.ListenAndServe(":8080", nil) // [!] TODO: Use TLS
}
All Configuration Options
- [Required]
Setup
- remoteRoot: URL path below which the server is accessible. (e.g. "/storage/")
- storageRoot: Location on server's file system to store remoteStorage documents. (e.g. "/var/rms/storage/")
- [Recommended]
UseAuthentication
configure how requests are authenticated and control access permissions of users. - [Recommended]
UseAllowedOrigins
allow-list of hosts that may make requests to the server. Per default any host is allowed. - [Optional]
UseAllowOrigin
for more control, specify a function that decides based on the request if it is allowed or not. If this option is specified,UseAllowedOrigins
has no effect. - [Not Recommended]
AllowAnyReadWrite
allow even unauthenticated requests to create, read, and delete any documents on the server. Has no effect ifUseAuthentication
is specified. - [Optional]
UseErrorHandler
to catch unhandled errors. Default behavior is tolog.Printf
the error. - [Optional]
UseMiddleware
to intercept requests before they are passed to the remote storage handler.
Register
registers the remote storage handler to a ServeMux.
# Functions
AddDocument adds a new document to the storage tree and returns a reference to it.
Configure initializes the remote storage server with the default configuration.
No description provided by the author
No description provided by the author
No description provided by the author
LDGet retrieves a value of type T from a nested ld+json map.
Load deserializes XML data from persistFile and adds the documents and folders to the storage tree.
Migrate traverses the root directory and copies any files contained therein into the remoteStorage root (cfg.Sroot).
No description provided by the author
ParseETag decodes an ETag previously encoded by (ETag).String().
Persist serializes the storage tree to XML.
No description provided by the author
Register the remote storage server (with middleware if configured) to the mux using Rroot + '/' as pattern.
RemoveDocument deletes a document from the storage tree and invalidates the etags of its ancestors.
Reset (re-) initializes the storage tree, so that it only contains a root folder.
Retrieve a document or folder identified by rname.
UpdateDocument updates an existing document in the storage tree with new information and invalidates etags of the document and its ancestors.
No description provided by the author
WriteError formats and writes err to w.
# Variables
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
No description provided by the author
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
Sentinel error values.
No description provided by the author
No description provided by the author
No description provided by the author
StatusCodes maps errors to their respective HTTP status codes.
# Structs
No description provided by the author
HttpError contains detailed error information, intended to be shown to users.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author
# Type aliases
No description provided by the author
No description provided by the author
No description provided by the author
ETag is a short and unique identifier assigned to a specific version of a remoteStorage resource.
LDjson aliases instead of defines a new type to make unmarshalling easier.
No description provided by the author
No description provided by the author