-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
131 lines (117 loc) · 2.53 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
package main
import (
"fmt"
"os"
"path"
"strings"
"time"
"net/http"
_ "net/http/pprof"
l "log"
"github.com/G1itchZero/ZeroGo/server"
"github.com/G1itchZero/ZeroGo/site_manager"
"github.com/G1itchZero/ZeroGo/utils"
log "github.com/Sirupsen/logrus"
"github.com/pkg/browser"
"github.com/urfave/cli"
)
const VERSION string = "0.1.0"
func main() {
os.MkdirAll(utils.GetDataPath(), 0777)
utils.CreateCerts()
log.SetLevel(log.ErrorLevel)
// l.SetOutput(ioutil.Discard)
log.WithFields(log.Fields{
"id": utils.GetPeerID(),
}).Info("Your Peer ID")
app := cli.NewApp()
app.Name = "ZeroGo"
app.Usage = "ZeroNet gate"
app.Version = VERSION
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "enable debug mode",
},
cli.BoolFlag{
Name: "no-tab",
Usage: "dont open new tab",
},
cli.IntFlag{
Name: "port",
Value: 43210,
Usage: "serving port",
},
cli.StringFlag{
Name: "homepage",
Value: utils.ZN_HOMEPAGE,
Usage: "homepage",
},
}
sm := site_manager.NewSiteManager()
app.Commands = []cli.Command{
{
Name: "download",
Aliases: []string{"d"},
Usage: "download site",
Action: func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
address := c.Args().First()
sm.Remove(address)
site := sm.Get(address)
site.Wait()
return nil
},
},
}
app.Action = func(c *cli.Context) error {
utils.SetDebug(c.Bool("debug"))
utils.SetHomepage(c.String("homepage"))
hasMedia, _ := utils.Exists(path.Join(utils.GetDataPath(), utils.ZN_UPDATE))
sync := make(chan int)
if !hasMedia {
go func() {
site := sm.GetFiles(utils.ZN_UPDATE, func(filename string) bool {
return strings.HasPrefix(filename, utils.ZN_MEDIA)
})
site.Wait()
sync <- 0
}()
} else {
go func() {
sync <- 0
}()
}
names := sm.GetFiles(utils.ZN_NAMES, func(filename string) bool {
return strings.HasPrefix(filename, "data/names.json")
})
names.Wait()
sm.Get(utils.ZN_ID)
// ids.Wait()
<-sync
sm.LoadNames()
s := server.NewServer(c.Int("port"), sm)
if c.Bool("debug") {
l.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
}
if !c.Bool("no-tab") {
go func() {
time.Sleep(time.Second)
addr := utils.ZN_HOMEPAGE
if c.NArg() > 0 {
addr = c.Args().First()
}
browser.OpenURL(fmt.Sprintf("http://127.0.0.1:%d/%s", c.Int("port"), addr))
}()
}
s.Serve()
return nil
}
app.Run(os.Args)
}