-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
72 lines (60 loc) · 2.05 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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/bleenco/sharebox/filemanager"
"github.com/bleenco/sharebox/vex"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
"github.com/urfave/negroni"
)
var (
listenAddr = flag.String("addr", "0.0.0.0:4505", "server listen address")
rootDir = flag.String("dir", "./", "root serve directory")
enableVex = flag.Bool("vex", false, "enable vex tunnel connection")
remoteServer = flag.String("s", "bleenco.space", "vex server hostname")
remotePort = flag.Int("p", 2200, "vex server SSH port")
localServer = flag.String("ls", "localhost", "local server hostname")
localPort = flag.Int("lp", 4505, "local server port")
)
func main() {
flag.Parse()
filemanager.Init(*rootDir)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
router := httprouter.New()
c := cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:4200"},
AllowCredentials: true,
AllowedHeaders: []string{"Authorization", "Content-Type"},
})
router.GET("/api/files/browse/*filepath", filemanager.BrowseHandler)
router.GET("/api/files/download/*filepath", filemanager.DownloadHandler)
router.GET("/api/files/download-zip", filemanager.DownloadZipHandler)
router.POST("/api/files/upload", filemanager.UploadHandler)
router.POST("/api/files/delete", filemanager.DeleteHandler)
router.POST("/api/files/folder/create", filemanager.CreateFolderHandler)
router.POST("/api/files/copy", filemanager.CopyHandler)
router.NotFound = http.FileServer(&AssetFS{
Asset: Asset,
AssetDir: AssetDir,
AssetInfo: AssetInfo,
Prefix: "./dist",
Fallback: "index.html",
}).ServeHTTP
n := negroni.New()
n.Use(c)
n.Use(negroni.NewRecovery())
n.Use(negroni.NewLogger())
n.UseHandler(router)
fmt.Printf("Serving files from %s at %s ...\n", *rootDir, *listenAddr)
go http.ListenAndServe(*listenAddr, n)
if *enableVex {
go vexclient.InitClient(*localServer, *remoteServer, *localPort, *remotePort)
}
<-ch
}