Skip to content

Commit

Permalink
add fileserver option for store serve
Browse files Browse the repository at this point in the history
  • Loading branch information
amartin120 committed Jan 22, 2024
1 parent 990ade9 commit e14453f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/hauler/cli/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func addStoreServe() *cobra.Command {

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 {
ctx := cmd.Context()

Expand Down
103 changes: 78 additions & 25 deletions cmd/hauler/cli/store/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/rancherfederal/hauler/pkg/store"

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

type ServeOpts struct {
Expand All @@ -25,53 +26,105 @@ type ServeOpts struct {
Port int
RootDir string
ConfigFile string
Daemon bool

Files bool

storedir string
}

func (o *ServeOpts) 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.BoolVarP(&o.Files, "files", "f", false, "Toggle file server instead of registry")
f.IntVarP(&o.Port, "port", "p", 0, "Port to listen on. Defaults to 5000 for registry and 8080 for file server.")
f.StringVar(&o.RootDir, "directory", "", "Directory to use for backend. Defaults to $PWD/registry for registry and $PWD/store-files for file server.")
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")

cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if o.Port == 0 {
o.Port = getDefaultPort(o.Files)
}
if o.RootDir == "" {
o.RootDir = getDefaultDirectory(o.Files)
}
return nil
}
}

func getDefaultPort(files bool) int {
if files {
return 8080
}
return 5000
}

func getDefaultDirectory(files bool) string {
if files {
return "store-files"
}
return "registry"
}

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

tr := server.NewTempRegistry(ctx, o.RootDir)
if err := tr.Start(); err != nil {
return err
}
if o.Files {
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
}

opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "registry://"+tr.Registry()); err != nil {
return err
}
} else { // start registry

tr.Close()
tr := server.NewTempRegistry(ctx, o.RootDir)
if err := tr.Start(); err != nil {
return err
}

cfg := o.defaultConfig()
if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile)
if err != nil {
opts := &CopyOpts{}
if err := CopyCmd(ctx, opts, s, "registry://"+tr.Registry()); err != nil {
return err
}
cfg = ucfg
}

r, err := server.NewRegistry(ctx, cfg)
if err != nil {
return err
}
tr.Close()

if err = r.ListenAndServe(); err != nil {
return err
cfg := o.defaultConfig()
if o.ConfigFile != "" {
ucfg, err := loadConfig(o.ConfigFile)
if err != nil {
return err
}
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
}

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 e14453f

Please sign in to comment.