Skip to content

Commit

Permalink
Merge pull request #159 from amartin120/store-fileserver
Browse files Browse the repository at this point in the history
Store fileserver
  • Loading branch information
amartin120 authored Jan 22, 2024
2 parents f76160d + d9e298b commit cc917af
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 21 deletions.
56 changes: 48 additions & 8 deletions cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,65 @@ func addStoreLoad() *cobra.Command {
}

func addStoreServe() *cobra.Command {
o := &store.ServeOpts{RootOpts: rootStoreOpts}

cmd := &cobra.Command{
Use: "serve",
Short: "Expose the content of a local store through an OCI compliant server",
Short: "Expose the content of a local store through an OCI compliant registry or file server",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
cmd.AddCommand(
addStoreServeRegistry(),
addStoreServeFiles(),
)

return cmd
}

// RegistryCmd serves the embedded registry
func addStoreServeRegistry() *cobra.Command {
o := &store.ServeRegistryOpts{RootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "registry",
Short: "Serve the embedded registry",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

s, err := o.Store(ctx)
if err != nil {
return err
}

return store.ServeCmd(ctx, o, s)
},
}
o.AddFlags(cmd)
return store.ServeRegistryCmd(ctx, o, s)
},
}

return cmd
o.AddFlags(cmd)

return cmd
}

// FileServerCmd serves the file server
func addStoreServeFiles() *cobra.Command {
o := &store.ServeFilesOpts{RootOpts: rootStoreOpts}
cmd := &cobra.Command{
Use: "fileserver",
Short: "Serve the file server",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

s, err := o.Store(ctx)
if err != nil {
return err
}

return store.ServeFilesCmd(ctx, o, s)
},
}

o.AddFlags(cmd)

return cmd
}

func addStoreSave() *cobra.Command {
Expand Down
66 changes: 55 additions & 11 deletions cmd/hauler/cli/store/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,29 @@ import (
"github.com/rancherfederal/hauler/pkg/store"

"github.com/rancherfederal/hauler/internal/server"
"github.com/rancherfederal/hauler/pkg/log"
)

type ServeOpts struct {
type ServeRegistryOpts struct {
*RootOpts

Port int
RootDir string
ConfigFile string
Daemon bool

storedir string
}

func (o *ServeOpts) AddFlags(cmd *cobra.Command) {
func (o *ServeRegistryOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.IntVarP(&o.Port, "port", "p", 5000, "Port to listen on")
f.StringVar(&o.RootDir, "directory", "registry", "Directory to use for registry backend (defaults to '$PWD/registry')")
f.IntVarP(&o.Port, "port", "p", 5000, "Port to listen on.")
f.StringVar(&o.RootDir, "directory", "registry", "Directory to use for backend. Defaults to $PWD/registry")
f.StringVarP(&o.ConfigFile, "config", "c", "", "Path to a config file, will override all other configs")
f.BoolVarP(&o.Daemon, "daemon", "d", false, "Toggle serving as a daemon")
}

// ServeCmd serves the embedded registry almost identically to how distribution/v3 does it
func ServeCmd(ctx context.Context, o *ServeOpts, s *store.Layout) error {
func ServeRegistryCmd(ctx context.Context, o *ServeRegistryOpts, s *store.Layout) error {
l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version)

tr := server.NewTempRegistry(ctx, o.RootDir)
Expand All @@ -55,7 +54,7 @@ func ServeCmd(ctx context.Context, o *ServeOpts, s *store.Layout) error {

tr.Close()

cfg := o.defaultConfig()
cfg := o.defaultRegistryConfig()
if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile)
if err != nil {
Expand All @@ -64,14 +63,59 @@ func ServeCmd(ctx context.Context, o *ServeOpts, s *store.Layout) error {
cfg = ucfg
}

l.Infof("starting registry on port [%d]", o.Port)
r, err := server.NewRegistry(ctx, cfg)
if err != nil {
return err
}

if err = r.ListenAndServe(); err != nil {
return err
}

return nil
}

type ServeFilesOpts struct {
*RootOpts

Port int
RootDir string

storedir string
}

func (o *ServeFilesOpts) AddFlags(cmd *cobra.Command) {
f := cmd.Flags()

f.IntVarP(&o.Port, "port", "p", 8080, "Port to listen on.")
f.StringVar(&o.RootDir, "directory", "store-files", "Directory to use for backend. Defaults to $PWD/store-files")
}

func ServeFilesCmd(ctx context.Context, o *ServeFilesOpts, s *store.Layout) error {
l := log.FromContext(ctx)
ctx = dcontext.WithVersion(ctx, version.Version)

opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "dir://"+o.RootDir); err != nil {
return err
}

cfg := server.FileConfig{
Root: o.RootDir,
Port: o.Port,
}

f, err := server.NewFile(ctx, cfg)
if err != nil {
return err
}

l.Infof("starting file server on port [%d]", o.Port)
if err := f.ListenAndServe(); err != nil {
return err
}

return nil
}

Expand All @@ -84,7 +128,7 @@ func loadConfig(filename string) (*configuration.Configuration, error) {
return configuration.Parse(f)
}

func (o *ServeOpts) defaultConfig() *configuration.Configuration {
func (o *ServeRegistryOpts) defaultRegistryConfig() *configuration.Configuration {
cfg := &configuration.Configuration{
Version: "0.1",
Storage: configuration.Storage{
Expand Down
3 changes: 1 addition & 2 deletions internal/server/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ type FileConfig struct {
// TODO: Better configs
func NewFile(ctx context.Context, cfg FileConfig) (Server, error) {
r := mux.NewRouter()
r.Handle("/", handlers.LoggingHandler(os.Stdout, http.FileServer(http.Dir(cfg.Root))))

r.PathPrefix("/").Handler(handlers.LoggingHandler(os.Stdout, http.StripPrefix("/", http.FileServer(http.Dir(cfg.Root)))))
if cfg.Root == "" {
cfg.Root = "."
}
Expand Down

0 comments on commit cc917af

Please sign in to comment.