abyss/abyss.go

84 lines
1.7 KiB
Go
Raw Normal View History

2024-08-25 23:15:11 -03:00
package main
import (
"log"
"log/slog"
2024-08-25 23:15:11 -03:00
"net/http"
"os"
"time"
"github.com/joho/godotenv"
2024-08-25 23:15:11 -03:00
)
func main() {
app := new(Application)
2024-09-18 14:00:04 -03:00
err := godotenv.Load()
if err != nil {
slog.Warn("no .env file detected, getting env from running process")
}
2024-08-25 23:15:11 -03:00
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")
2024-08-25 23:15:11 -03:00
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.key == "" {
slog.Warn("no upload key detected")
}
if app.filesDir == "" {
slog.Warn("file dir is not set, running on default ./files")
app.filesDir = "./files"
}
2024-09-18 10:16:03 -03:00
if app.port == "" {
slog.Info("running on default port")
app.port = ":3235"
} else {
slog.Info("running on modified port")
2024-09-18 10:16:03 -03:00
app.port = ":" + app.port
}
2024-09-19 10:28:23 -03:00
if app.url == "" {
slog.Warn("no root url detected, defaulting to localhost.")
app.url = "localhost" + app.port
}
2024-08-25 23:15:11 -03:00
mux := http.NewServeMux()
2024-09-18 09:55:26 -03:00
mux.HandleFunc("/", app.indexHandler)
2024-09-19 10:28:23 -03:00
mux.Handle(
2024-08-25 23:15:11 -03:00
"/tree/",
2024-09-19 10:28:23 -03:00
http.StripPrefix(
"/tree",
app.basicAuth(app.fileListingHandler),
),
2024-08-25 23:15:11 -03:00
)
2024-10-15 14:18:55 -03:00
mux.HandleFunc("/last", app.lastUploadedHandler)
mux.HandleFunc("/upload", app.basicAuth(app.uploadHandler))
2024-08-25 23:15:11 -03:00
srv := &http.Server{
Addr: app.port,
2024-08-25 23:15:11 -03:00
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)
}
}