-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
59 lines (48 loc) · 1.57 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
package main
import (
"fmt"
"github.com/go-chi/chi"
"github.com/luqmansen/gosty/pkg/apiserver/util"
"github.com/luqmansen/gosty/pkg/fileserver"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"strings"
)
var gitCommit string
func main() {
workDir, _ := os.Getwd()
folder := util.GetEnv("STORAGE", "storage")
pathToServe := workDir + "/" + folder
port := util.GetEnv("PORT", "8001")
address := fmt.Sprintf("%s:%s", "0.0.0.0", port)
peerHost := util.GetEnv("FILESERVER_PEER_HOST", "")
peers := strings.Split(peerHost, ",")
selfHost := os.Getenv("HOSTNAME")
var peerLists []string
for _, peer := range peers {
if peer != "" && !strings.Contains(peer, selfHost) {
peerLists = append(peerLists, fmt.Sprintf("%s:%s", peer, port))
}
}
log.Infof("Peer list: %s", peerLists)
fileServerHandler := fileserver.NewFileServerHandler(pathToServe, peerLists, address)
router := fileserver.NewRouter(fileServerHandler)
server := fileserver.NewServer(address, router)
util.GetVersionEndpoint(router, gitCommit)
go server.Serve()
// TODO: fix peer discovery
//go fileServerHandler.PeerDiscovery()
//quick and dirty to make fileserver-0 sync to other replica
if strings.Contains(selfHost, "0") {
// only run lsycnd on primary because it doesn't support the other way
// https://stackoverflow.com/questions/36457567/use-lsyncd-to-update-a-local-folder-with-a-remote-source
go fileServerHandler.StartSync()
}
select {}
}
func getVersion(router *chi.Mux) {
router.Get("/version", func(writer http.ResponseWriter, request *http.Request) {
writer.Write([]byte(gitCommit))
})
}