74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
func main() {
|
|
app := new(Application)
|
|
|
|
godotenv.Load()
|
|
|
|
app.auth.username = os.Getenv("AUTH_USERNAME")
|
|
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")
|
|
}
|
|
|
|
if app.auth.password == "" {
|
|
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(
|
|
"/tree/",
|
|
app.basicAuth(app.treeHandler),
|
|
)
|
|
|
|
srv := &http.Server{
|
|
Addr: app.port,
|
|
Handler: mux,
|
|
IdleTimeout: time.Minute,
|
|
ReadTimeout: 10 * time.Second,
|
|
WriteTimeout: 60 * time.Second,
|
|
}
|
|
|
|
log.Printf("starting server on %s", srv.Addr)
|
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
|
log.Fatalf("Failed to start server: %v", err)
|
|
}
|
|
}
|