2024-08-25 23:15:11 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2024-09-06 16:34:20 -03:00
|
|
|
"log/slog"
|
2024-08-25 23:15:11 -03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
2024-09-03 21:51:36 -03:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
2024-08-25 23:15:11 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := new(Application)
|
|
|
|
|
2024-10-15 22:41:51 -03:00
|
|
|
parseEnv(app)
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
setupHandlers(mux, app)
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: app.port,
|
|
|
|
Handler: mux,
|
|
|
|
IdleTimeout: 10 * time.Second,
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseEnv(app *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-09-03 21:51:36 -03:00
|
|
|
|
2024-08-25 23:15:11 -03:00
|
|
|
app.auth.username = os.Getenv("AUTH_USERNAME")
|
|
|
|
app.auth.password = os.Getenv("AUTH_PASSWORD")
|
2024-09-03 21:51:36 -03:00
|
|
|
app.url = os.Getenv("ABYSS_URL")
|
|
|
|
app.key = os.Getenv("UPLOAD_KEY")
|
2024-09-06 16:34:20 -03:00
|
|
|
app.filesDir = os.Getenv("ABYSS_FILEDIR")
|
|
|
|
app.port = os.Getenv("ABYSS_PORT")
|
2024-08-25 23:15:11 -03:00
|
|
|
|
2024-10-15 22:41:51 -03:00
|
|
|
app.authText = os.Getenv("SHOULD_AUTH")
|
2024-10-15 15:21:05 -03:00
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-09-06 16:34:20 -03:00
|
|
|
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 == "" {
|
2024-09-06 16:34:20 -03:00
|
|
|
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-06 16:34:20 -03:00
|
|
|
}
|
|
|
|
|
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-10-15 22:41:51 -03:00
|
|
|
}
|
2024-09-19 10:28:23 -03:00
|
|
|
|
2024-10-15 22:41:51 -03:00
|
|
|
func setupHandlers(mux *http.ServeMux, app *Application) {
|
2024-09-18 09:55:26 -03:00
|
|
|
mux.HandleFunc("/", app.indexHandler)
|
2024-10-15 22:41:51 -03:00
|
|
|
|
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",
|
2024-10-16 21:38:20 -03:00
|
|
|
BasicAuth(app.fileListingHandler, app),
|
2024-09-19 10:28:23 -03:00
|
|
|
),
|
2024-08-25 23:15:11 -03:00
|
|
|
)
|
2024-10-15 22:41:51 -03:00
|
|
|
|
2024-10-15 14:18:55 -03:00
|
|
|
mux.HandleFunc("/last", app.lastUploadedHandler)
|
2024-10-15 22:41:51 -03:00
|
|
|
|
2024-10-17 10:52:33 -03:00
|
|
|
mux.HandleFunc("/token", BasicAuth(app.createTokenHandler, app))
|
|
|
|
|
2024-10-16 21:38:20 -03:00
|
|
|
mux.HandleFunc("/files/", app.fileHandler)
|
2024-10-16 14:05:49 -03:00
|
|
|
|
2024-10-15 22:41:51 -03:00
|
|
|
if app.authText == "yes" {
|
2024-10-16 21:38:20 -03:00
|
|
|
mux.HandleFunc("/upload", BasicAuth(app.uploadHandler, app))
|
2024-10-15 15:21:05 -03:00
|
|
|
slog.Warn("text uploading through the browser will be restricted")
|
|
|
|
} else {
|
|
|
|
mux.HandleFunc("/upload", app.uploadHandler)
|
|
|
|
slog.Warn("text uploading through the browser will NOT be restricted")
|
|
|
|
}
|
2024-08-25 23:15:11 -03:00
|
|
|
}
|