package server import ( "fmt" "io/fs" "net/http" "os" "path/filepath" "git.zervo.org/zervo/fileserver" "git.zervo.org/zervo/fileserver/internal/config" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/view" ) // Start starts the server. func Start(cfg *config.Config) error { app := iris.New() subFs, err := fs.Sub(fileserver.TemplateFS, "templates") if err != nil { return fmt.Errorf("Failed to sub fs") } tmpl := view.HTML(subFs, ".html") app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { files, err := os.ReadDir(cfg.ServeDirectory) if err != nil { ctx.StatusCode(http.StatusInternalServerError) ctx.WriteString("Could not read directory") return } var filenames []string for _, f := range files { if !f.IsDir() { filenames = append(filenames, f.Name()) } } fmt.Printf("Files: %v\n", filenames) ctx.ViewData("Files", filenames) ctx.View("index.html") }) app.Get("/download/{filename:string}", func(ctx iris.Context) { filename := ctx.Params().Get("filename") filePath := filepath.Join(cfg.ServeDirectory, filename) ctx.SendFile(filePath, filename) }) return app.Listen(":" + fmt.Sprint(cfg.Port)) }