-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabyss.go
103 lines (82 loc) · 1.99 KB
/
abyss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"log"
"log/slog"
"net/http"
"os"
"time"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
slog.Warn("no .env file detected, getting env from running process")
}
app := &Application{
auth: struct {
username string
password string
}{
username: os.Getenv("AUTH_USERNAME"),
password: os.Getenv("AUTH_PASSWORD"),
},
url: os.Getenv("ABYSS_URL"),
key: os.Getenv("UPLOAD_KEY"),
filesDir: os.Getenv("ABYSS_FILEDIR"),
port: os.Getenv("ABYSS_PORT"),
authUpload: os.Getenv("SHOULD_AUTH"),
}
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) {
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"
}
if app.port == "" {
slog.Info("running on default port")
app.port = ":3235"
} else {
slog.Info("running on modified port")
app.port = ":" + app.port
}
if app.url == "" {
slog.Warn("no root url detected, defaulting to localhost.")
app.url = "localhost" + app.port
}
}
func setupHandlers(mux *http.ServeMux, app *Application) {
mux.HandleFunc("/", app.indexHandler)
mux.Handle(
"/tree/",
http.StripPrefix(
"/tree",
BasicAuth(app.fileListingHandler, app),
),
)
mux.HandleFunc("/last", app.lastUploadedHandler)
mux.HandleFunc("/token", BasicAuth(app.createTokenHandler, app))
mux.HandleFunc("/files/", app.fileHandler)
}