Files
stfs/cmd/stbak/cmd/serve_http.go

153 lines
3.9 KiB
Go

package cmd
import (
"context"
"log"
"net/http"
"time"
sfs "github.com/pojntfx/stfs/internal/fs"
"github.com/pojntfx/stfs/internal/handlers"
"github.com/pojntfx/stfs/internal/keys"
"github.com/pojntfx/stfs/internal/logging"
"github.com/pojntfx/stfs/internal/persisters"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/operations"
"github.com/pojntfx/stfs/pkg/tape"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
laddrFlag = "laddr"
cacheFlag = "cache"
)
var serveHTTPCmd = &cobra.Command{
Use: "http",
Aliases: []string{"htt", "h"},
Short: "Serve tape or tar file and the index over HTTP (read-only)",
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
}
if err := keys.CheckKeyAccessible(viper.GetString(encryptionFlag), viper.GetString(identityFlag)); err != nil {
return err
}
return keys.CheckKeyAccessible(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
},
RunE: func(cmd *cobra.Command, args []string) error {
pubkey, err := keys.ReadKey(viper.GetString(signatureFlag), viper.GetString(recipientFlag))
if err != nil {
return err
}
recipient, err := keys.ParseSignerRecipient(viper.GetString(signatureFlag), pubkey)
if err != nil {
return err
}
privkey, err := keys.ReadKey(viper.GetString(encryptionFlag), viper.GetString(identityFlag))
if err != nil {
return err
}
identity, err := keys.ParseIdentity(viper.GetString(encryptionFlag), privkey, viper.GetString(passwordFlag))
if err != nil {
return err
}
tm := tape.NewTapeManager(
viper.GetString(driveFlag),
viper.GetInt(recordSizeFlag),
false,
)
metadataPersister := persisters.NewMetadataPersister(viper.GetString(metadataFlag))
if err := metadataPersister.Open(); err != nil {
return err
}
root, err := metadataPersister.GetRootPath(context.Background())
if err != nil {
return err
}
logger := logging.NewLogger()
ops := operations.NewOperations(
config.BackendConfig{
GetWriter: tm.GetWriter,
CloseWriter: tm.Close,
GetReader: tm.GetReader,
CloseReader: tm.Close,
GetDrive: tm.GetDrive,
CloseDrive: tm.Close,
},
config.MetadataConfig{
Metadata: metadataPersister,
},
config.PipeConfig{
Compression: viper.GetString(compressionFlag),
Encryption: viper.GetString(encryptionFlag),
Signature: viper.GetString(signatureFlag),
RecordSize: viper.GetInt(recordSizeFlag),
},
config.CryptoConfig{
Recipient: recipient,
Identity: identity,
Password: viper.GetString(passwordFlag),
},
logger.PrintHeaderEvent,
)
stfs := sfs.NewFileSystem(
ops,
config.MetadataConfig{
Metadata: metadataPersister,
},
logger.PrintHeader,
)
var fs afero.Fs
if viper.GetBool(cacheFlag) {
fs = afero.NewCacheOnReadFs(afero.NewBasePathFs(stfs, root), afero.NewMemMapFs(), time.Hour)
} else {
fs = afero.NewBasePathFs(stfs, root)
}
log.Println("Listening on", viper.GetString(laddrFlag))
return http.ListenAndServe(
viper.GetString(laddrFlag),
handlers.PanicHandler(
http.FileServer(
afero.NewHttpFs(fs),
),
),
)
},
}
func init() {
serveHTTPCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record")
serveHTTPCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key of recipient that has been encrypted for")
serveHTTPCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key")
serveHTTPCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to the public key to verify with")
serveHTTPCmd.PersistentFlags().StringP(laddrFlag, "a", "localhost:1337", "Listen address")
serveHTTPCmd.PersistentFlags().BoolP(cacheFlag, "n", true, "Enable in-memory caching")
viper.AutomaticEnv()
serveCmd.AddCommand(serveHTTPCmd)
}