-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
38 lines (31 loc) · 791 Bytes
/
main.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
package main
import (
"errors"
"fmt"
"net/http"
"os"
)
func main() {
err := os.Mkdir("./hosted", 0755)
if err != nil && !errors.Is(err, os.ErrExist) {
panic(err)
}
files := http.StripPrefix("/hosted/", http.FileServer(http.Dir("./hosted")))
http.HandleFunc("/upload", upload)
http.HandleFunc("/shorten", redirectRegister)
http.HandleFunc("/hosted/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/hosted/" {
http.NotFound(w, r)
return
}
files.ServeHTTP(w, r)
})
http.Handle("/r/", http.StripPrefix("/r/", http.HandlerFunc(redirect)))
http.Handle("/", http.FileServer(http.Dir("./static")))
go cleanFolder()
fmt.Println("Server initialized.")
if err := http.ListenAndServe(host, nil); err != nil {
fmt.Println(err)
os.Exit(1)
}
}