diff --git a/abyss.go b/abyss.go index 4026fc2..e49c570 100644 --- a/abyss.go +++ b/abyss.go @@ -2,6 +2,7 @@ package main import ( "log" + "log/slog" "net/http" "os" "time" @@ -9,11 +10,6 @@ import ( "github.com/joho/godotenv" ) -const ( - filesDir = "./files" - port = ":8999" -) - func main() { app := new(Application) @@ -23,6 +19,8 @@ func main() { app.auth.password = os.Getenv("AUTH_PASSWORD") app.url = os.Getenv("ABYSS_URL") app.key = os.Getenv("UPLOAD_KEY") + app.filesDir = os.Getenv("ABYSS_FILEDIR") + app.port = os.Getenv("ABYSS_PORT") if app.auth.username == "" { log.Fatal("basic auth username must be provided") @@ -32,6 +30,26 @@ func main() { log.Fatal("basic auth password must be provided") } + if app.url == "" { + slog.Warn("no root url detected, defaulting to localhost.") + } + + if app.key == "" { + slog.Warn("no upload key detected") + } + + if app.filesDir == "" { + slog.Warn("file dir is not set, running on default ./files") + app.filesDir = "./files" + } + + if app.port == "" { + slog.Info("running on default port") + app.port = ":3235" + } else { + slog.Info("running on modified port") + } + mux := http.NewServeMux() mux.HandleFunc("/", app.fileHandler) mux.HandleFunc( @@ -40,7 +58,7 @@ func main() { ) srv := &http.Server{ - Addr: port, + Addr: app.port, Handler: mux, IdleTimeout: time.Minute, ReadTimeout: 10 * time.Second, diff --git a/generate_config.sh b/generate_config.sh index 5a7a415..a869388 100755 --- a/generate_config.sh +++ b/generate_config.sh @@ -5,6 +5,13 @@ read -p "Server domain name - this is the end url of where abyss will be hosted read -p "Upload key - this is the key you will need to have to be able to make uploads to the server []: " -e UPLOAD_KEY +read -p "Files Directory - this is the dir location for storing the files [./files]: " -e ABYSS_FILEDIR +if [ -z $ABYSS_FILEDIR ]; then + ABYSS_FILEDIR="./files" +fi + +read -p "Server port - this is the port the server will run on; type just the port number. []: " -e ABYSS_PORT + read -p "Auth username - this is the username to access /tree (show all uploaded files) [admin]: " -e AUTH_USERNAME if [ -z $AUTH_USERNAME ]; then AUTH_USERNAME="admin" @@ -19,6 +26,12 @@ cat << EOF > .env # This is the full name of the final domain for the server. Example: paste.abyss.dev ABYSS_URL=$ABYSS_URL +# Where abyss will store files. It's fine to leave it empty. Defaults to "./files" +ABYSS_FILEDIR=$ABYSS_FILEDIR + +# The port the server will run on, it's fine to leave it empty. Defaults to :3235 +ABYSS_PORT=:$ABYSS_PORT + # This is the username of the user for accessing /tree AUTH_USERNAME=$AUTH_USERNAME diff --git a/handlers.go b/handlers.go index 955e32c..ba7d8f2 100644 --- a/handlers.go +++ b/handlers.go @@ -16,8 +16,10 @@ type Application struct { username string password string } - url string - key string + url string + key string + filesDir string + port string } func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { @@ -27,7 +29,7 @@ func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { } name := filepath.Clean(r.URL.Path) - path := filepath.Join(filesDir, name) + path := filepath.Join(app.filesDir, name) if !filepath.IsLocal(path) { http.Error(w, "Wrong url", http.StatusBadRequest) @@ -42,7 +44,7 @@ func (app *Application) fileHandler(w http.ResponseWriter, r *http.Request) { } func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) { - http.StripPrefix("/tree/", http.FileServer(http.Dir(filesDir))).ServeHTTP(w, r) + http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r) } func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { @@ -63,8 +65,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { } defer file.Close() - if _, err := os.Stat(filesDir); err != nil { - if err := os.Mkdir(filesDir, 0750); err != nil { + if _, err := os.Stat(app.filesDir); err != nil { + if err := os.Mkdir(app.filesDir, 0750); err != nil { http.Error(w, "Error creating storage directory", http.StatusInternalServerError) } } @@ -73,7 +75,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { filename := fmt.Sprintf("%d%s", time, filepath.Ext(handler.Filename)) - filepath := fmt.Sprintf("%s/%s", filesDir, filename) + filepath := fmt.Sprintf("%s/%s", app.filesDir, filename) dst, err := os.Create(filepath) if err != nil { @@ -86,7 +88,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { } if app.url == "" { - fmt.Fprintf(w, "http://localhost%s/%s\n", port, filename) + fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename) } else { fmt.Fprintf(w, "http://%s/%s\n", app.url, filename) }