This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
207 lines (168 loc) · 5.06 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"os/signal"
"os/user"
"time"
rice "github.com/GeertJohan/go.rice"
badger "github.com/dgraph-io/badger"
"github.com/gorilla/mux"
"github.com/logrusorgru/aurora"
)
func main() {
// License
fmt.Println("")
fmt.Println(aurora.Bold("GIPNS :"), "Throws a Git repository to IPNS.")
fmt.Println("Copyright © 2019 Nato Boram")
fmt.Println("This program is free software : you can redistribute it and/or modify it under the terms of the " + aurora.Underline("GNU General Public License").String() + " as published by the " + aurora.Underline("Free Software Foundation").String() + ", either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but " + aurora.Bold("without any warranty").String() + " ; without even the implied warranty of " + aurora.Italic("merchantability").String() + " or " + aurora.Italic("fitness for a particular purpose").String() + ". See the " + aurora.Underline("GNU General Public License").String() + " for more details. You should have received a copy of the " + aurora.Underline("GNU General Public License").String() + " along with this program. If not, see " + aurora.Blue("http://www.gnu.org/licenses/").String() + ".")
fmt.Println(aurora.Bold("Contact :"), aurora.Blue("https://gitlab.com/NatoBoram/git-to-ipns"))
fmt.Println("")
// User
path, err := initUser()
if err != nil {
return
}
dirHome = path
// IPFS
err = initIPFS()
if err != nil {
return
}
// Git
err = initGit()
if err != nil {
return
}
// Badger
db, err := initBager()
if err != nil {
return
}
defer db.Close()
// Refresh repos
go func() {
for {
onAllRepos(db)
time.Sleep(24 * time.Hour)
}
}()
// Router
go initMux(db)
// Listen to CTRL+C
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for range c {
// Close database
err = db.Close()
if err != nil {
fmt.Println("Couldn't save the database.")
log.Fatalln(err.Error())
}
os.Exit(0)
}
}()
// Wait
<-make(chan struct{})
}
func initGit() (err error) {
// Git Directory
err = os.MkdirAll(dirHome+dirGit, permPrivateDirectory)
if err != nil {
fmt.Println("Couldn't create the git directory.")
fmt.Println(err.Error())
return
}
// Check for Git
path, err := exec.LookPath("git")
if err != nil {
fmt.Println("Git is not installed.")
fmt.Println(err.Error())
return
}
fmt.Println(aurora.Bold("Git :"), aurora.Blue(path))
fmt.Println("")
return
}
func initIPFS() (err error) {
// Check for IPFS
path, err := exec.LookPath("ipfs")
if err != nil {
fmt.Println("IPFS is not installed.")
fmt.Println(err.Error())
return
}
fmt.Println(aurora.Bold("IPFS :"), aurora.Blue(path))
// Enable sharding
exec.Command("ipfs", "config", "--json", "Experimental.ShardingEnabled", "true").Run()
// Check for IPFS Cluster Service
path, err = exec.LookPath("ipfs-cluster-service")
if err != nil {
fmt.Println("IPFS Cluster Service is not installed.")
fmt.Println(err.Error())
return
}
fmt.Println(aurora.Bold("IPFS Cluster Service :"), aurora.Blue(path))
// Check for IPFS Cluster Control
path, err = exec.LookPath("ipfs-cluster-ctl")
if err != nil {
fmt.Println("IPFS Cluster Control is not installed.")
fmt.Println(err.Error())
return
}
fmt.Println(aurora.Bold("IPFS Cluster Control :"), aurora.Blue(path))
// Connect to Swarm
initSwarm()
fmt.Println("")
return
}
func initSwarm() {
for _, pg := range PublicGateways {
ipfsSwarmConnect(pg)
}
}
func initBager() (db *badger.DB, err error) {
// Badger Directory
err = os.MkdirAll(dirHome+dirBadger, permPrivateDirectory)
if err != nil {
fmt.Println("Couldn't create the badger directory.")
fmt.Println(err.Error())
return
}
// Options
options := badger.DefaultOptions(dirHome + dirBadger)
db, err = badger.Open(options)
if err != nil {
fmt.Println("Couldn't open a Badger Database.")
fmt.Println(err.Error())
}
fmt.Println()
return db, err
}
func initUser() (path string, err error) {
usr, err := user.Current()
if err != nil {
fmt.Println("Couldn't get the current user.")
fmt.Println(err.Error())
}
return usr.HomeDir, err
}
func initMux(db *badger.DB) {
r := mux.NewRouter()
// API
api := r.StrictSlash(true).PathPrefix("/api").Subrouter()
// Repos
repos := api.PathPrefix("/repos").Subrouter()
repos.Methods("GET").Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reposGetHandler(db, w, r) })
repos.Methods("POST").Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { reposPostHandler(db, w, r) })
repos.Methods("GET").Path("/{link:.*}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { repoGetHandler(db, w, r) })
repos.Methods("DELETE").Path("/{link:.*}").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { repoDeleteHandler(db, w, r) })
// Web Server
r.PathPrefix("/").Handler(http.FileServer(rice.MustFindBox("web").HTTPBox()))
fmt.Println("Web server started at", aurora.Blue("http://localhost:62458/"))
log.Fatal(http.ListenAndServe(":62458", r))
}