Cleanup, fixes, cloud fs support

This commit is contained in:
Tyler
2020-06-14 04:29:16 -04:00
parent 8bcb538e10
commit b651ccd250
11 changed files with 228 additions and 190 deletions
+27 -9
View File
@@ -5,12 +5,14 @@ import (
"flag"
"fmt"
"github.com/go-ini/ini"
"github.com/spf13/afero"
"golang.org/x/crypto/openpgp"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
"strings"
"sync"
@@ -37,6 +39,8 @@ var (
flagShowVersion = flag.Bool("version", false, "Show deb-simple version")
conf = Conf{}
pgpEntity *openpgp.Entity
fs afero.Fs
baseTempDir string
)
func main() {
@@ -57,6 +61,18 @@ func main() {
log.Fatalln("unable to marshal config file, exiting...", err)
}
baseTempDir = path.Join(os.TempDir(), "deb-simple")
if _, err := os.Stat(baseTempDir); err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(baseTempDir, 0755); err != nil {
return
}
}
}
fs = afero.NewBasePathFs(afero.NewOsFs(), conf.Repo.Root)
if err := createDirs(conf); err != nil {
log.Println(err)
log.Fatalln("error creating directory structure, exiting")
@@ -82,10 +98,12 @@ func main() {
mux := http.NewServeMux()
mux.Handle("/", http.StripPrefix("/", http.FileServer(http.Dir(conf.Repo.Root))))
mux.HandleFunc("/rescan", rescanHandler)
mux.HandleFunc("/upload", uploadHandler)
mux.HandleFunc("/delete", deleteHandler)
httpFs := afero.NewHttpFs(fs)
mux.Handle("/", http.StripPrefix("/", http.FileServer(httpFs)))
mux.HandleFunc("/rescan", requireAuth(rescanHandler))
mux.HandleFunc("/upload", requireAuth(uploadHandler))
mux.HandleFunc("/delete", requireAuth(deleteHandler))
bind := fmt.Sprintf(":%d", conf.Http.Port)
@@ -161,20 +179,20 @@ func setupPgp(config Conf) error {
func createDirs(config Conf) error {
for _, distro := range config.Repo.DistroNames() {
for _, arch := range config.Repo.ArchitectureNames() {
if _, err := os.Stat(config.ArchPath(distro, arch)); err != nil {
if _, err := fs.Stat(config.ArchPath(distro, arch)); err != nil {
if os.IsNotExist(err) {
log.Printf("Directory for %s (%s) does not exist, creating", distro, arch)
if err := os.MkdirAll(config.ArchPath(distro, arch), 0755); err != nil {
if err := fs.MkdirAll(config.ArchPath(distro, arch), 0755); err != nil {
return fmt.Errorf("error creating directory for %s (%s): %s", distro, arch, err)
}
} else {
return fmt.Errorf("error inspecting %s (%s): %s", distro, arch, err)
}
}
if _, err := os.Stat(config.PoolPath(distro, arch)); err != nil {
if _, err := fs.Stat(config.PoolPath(distro, arch)); err != nil {
if os.IsNotExist(err) {
log.Printf("Directory for %s (%s) does not exist, creating", distro, arch)
if err := os.MkdirAll(config.PoolPath(distro, arch), 0755); err != nil {
if err := fs.MkdirAll(config.PoolPath(distro, arch), 0755); err != nil {
return fmt.Errorf("error creating directory for %s (%s): %s", distro, arch, err)
}
} else {
@@ -202,7 +220,7 @@ func copyFile(oldPath, newPath string) error {
defer old.Close()
n, err := os.Create(newPath)
n, err := fs.Create(newPath)
if err != nil {
return err