-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
152 lines (125 loc) · 3.14 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strings"
"github.com/gophergala2016/togepi/meta"
"github.com/gophergala2016/togepi/redis"
"github.com/gophergala2016/togepi/server"
"github.com/gophergala2016/togepi/tcp"
"github.com/gophergala2016/togepi/util"
)
var (
httpServerAddress = flag.String("http-host", "185.73.39.98:8011", "togepi server's host (without protocol)")
tcpServerAddress = flag.String("tcp-host", "185.73.39.98:8012", "togepi server's host")
socketPort = flag.Int("socket-port", 8013, "a port to be used for local inter-process communication")
httpPort = flag.Int("http-port", 8011, "HTTP server's port")
tcpPort = flag.Int("tcp-port", 8012, "TCP server's port")
providerPort = flag.Int("provider-port", 8014, "provider port")
redisHost = flag.String("redis-host", "127.0.0.1:6379", "Redis host address")
redisDB = flag.Int("redis-db", 0, "Redis DB")
serverMode = flag.Bool("server", false, "run in server mode")
daemonMode = flag.Bool("start", false, "run in daemon mode")
showShared = flag.Bool("a", false, "List all shared files")
removeHash = flag.String("rm", "", "A shared file to remove")
)
var (
srv *server.Server
p *server.Provider
r *redis.Redis
md *meta.Data
l *tcp.Listener
cl *tcp.Client
)
func init() {
flag.Parse()
}
func shutdown() {
if srv != nil {
srv.Stop()
}
if r != nil {
r.Close()
}
if l != nil {
l.Stop()
}
if cl != nil {
cl.Close()
}
if p != nil {
p.Stop()
}
os.Exit(0)
}
func main() {
if *serverMode {
startServer()
} else if *showShared {
md = meta.NewData()
err := readConfig()
if err != nil {
util.CheckError(err, shutdown)
}
for k, v := range md.Files {
fmt.Println(md.UserID+k, v.Path)
}
shutdown()
} else {
md = meta.NewData()
if *removeHash != "" {
rmErr := removeSharedFile(*removeHash)
util.CheckError(rmErr, shutdown)
shutdown()
}
if len(os.Args) > 1 {
if *daemonMode {
startDaemon()
} else {
filePath := os.Args[1]
err := readConfig()
if err != nil {
util.CheckError(err, shutdown)
}
*tcpServerAddress = md.TCPServer
*httpServerAddress = md.HTTPServer
if len(filePath) == 96 && !strings.Contains(filePath, "/") {
requestFile(filePath)
shutdown()
} else {
fileStat, fileStatErr := os.Stat(filePath)
util.CheckError(fileStatErr, shutdown)
if fileStat.IsDir() {
util.CheckError(errors.New(filePath+" is a directory"), shutdown)
}
currentDir, currentDirErr := os.Getwd()
util.CheckError(currentDirErr, shutdown)
if string(filePath[0]) != "/" {
filePath = currentDir + "/" + filePath
}
shareErr := shareFile(filePath)
util.CheckError(shareErr, shutdown)
shutdown()
}
}
} else {
util.CheckError(errors.New("please provide required arguments"), shutdown)
}
}
// Shutting down on SIGINT.
go func() {
intChan := make(chan os.Signal)
signal.Notify(intChan, os.Interrupt)
<-intChan
log.Println("Shutting down gracefully")
go shutdown()
fmt.Println("send SIGINT again to kill")
<-intChan
os.Exit(1)
}()
select {}
}