-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (63 loc) · 1.82 KB
/
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
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/mfmayer/darchivist/internal/arc"
"github.com/mfmayer/darchivist/internal/log"
"github.com/mfmayer/darchivist/internal/vfs/vfswebui"
)
var listenUiAddr = flag.String("listen", ":9055", "Listen address and port")
var archivePath = flag.String("path", os.Getenv("DARCHIVE_PATH"), "Path to the documents archive (can also be set by environment varaible DARCHIVE_PATH)")
func exists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func init() {
}
func main() {
flag.Parse()
if !exists(*archivePath) {
log.Error.Print("Invalid archive path set")
flag.PrintDefaults()
os.Exit(-1)
}
archive := arc.NewArchive(*archivePath)
router := chi.NewRouter()
router.Route("/api/", archive.InstallAPI)
router.Get("/", http.RedirectHandler("/ui/", http.StatusMovedPermanently).ServeHTTP)
if err := installFileServer(router, "/ui", vfswebui.FileSystem); err != nil {
panic(err)
}
log.Info.Printf("Web UI listening on: %v", *listenUiAddr)
if err := http.ListenAndServe(*listenUiAddr, router); err != nil {
panic(err)
}
}
func installFileServer(router chi.Router, path string, root http.FileSystem) error {
if strings.ContainsAny(path, "{}*") {
return fmt.Errorf("FileServer does not permit URL parameters")
}
if path != "/" && path[len(path)-1] != '/' {
router.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
fs := http.StripPrefix(path, http.FileServer(root))
router.Route(path, func(r chi.Router) {
r.Use(middleware.Compress(5, "gzip"))
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
})
return nil
}