/
main.go
231 lines (185 loc) · 5.34 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strconv"
DM "github.com/kycklingar/PBooru/DataManager"
"github.com/kycklingar/PBooru/DataManager/image"
apiv1 "github.com/kycklingar/PBooru/api/rest"
h "github.com/kycklingar/PBooru/handlers"
)
var gConf config
//var gManager *DM.Manager = DM.GManager
func faviconHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/favicon.ico")
}
func serveHiddenFiles(dir string) {
files, err := ioutil.ReadDir("." + dir)
if err != nil {
panic(err)
}
for _, f := range files {
//fmt.Println(dir + f.Name())
serveHiddenFile(dir + f.Name())
}
}
func serveHiddenFile(path string) {
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "."+path)
})
}
func redirectHandler(w http.ResponseWriter, r *http.Request) {
//fmt.Println(r.Host, r.RequestURI)
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusSeeOther)
}
func main() {
migrateStore := flag.Int("migrate-store", -1, "Migrate all files and thumbnails to the configured store")
initDir := flag.Bool("init-dir", false, "Initialize an ipfs directory of all files")
initConfig := flag.Bool("init-cfg", false, "Initialize the configfile and exit.")
configFilePath := flag.String("cfg", "config.cfg", "Load config file.")
generateThumbnails := flag.Int("gen-thumbs", 0, "Generate (missing) thumbnails for this size")
generateThumbnail := flag.Int("gen-thumb", 0, "Generate thumbnails for this post id")
checkThumbSupport := flag.Bool("thumb-support", false, "Check for installed thumbnailing software")
calcChecksums := flag.Bool("calc-checksums", false, "Calculate the checksums of all posts")
generateFileSize := flag.Bool("gen-filesize", false, "Generate file sizes on posts with 0 filesize")
generateFileDim := flag.Bool("gen-dimensions", false, "Generate file dimensions")
upgradePostCids := flag.Bool("upgrade-post-cids", false, "Upgrade post cids to base32")
genPears := flag.Bool("gen-pears", false, "Harvest apple tree")
recalcApple := flag.Bool("recalc-appletree", false, "Recalculate appletree")
genPhash := flag.Bool("gen-phash", false, "Generate phashes")
tombstoneFile := flag.String("read-tombstone", "", "Read tombstones from a tombstone file")
verifyFiles := flag.Int("verify-files", -1, "Verify the integrity of the files in ipfs")
updateUserFlags := flag.Bool("update-user-flags", false, "Update user flags <old> <new>")
flag.Parse()
if *checkThumbSupport {
image.ThumbnailerInstalled()
return
}
log.SetFlags(log.Llongfile)
gConf = exeConf(*configFilePath)
h.CFG = &gConf.HCfg
DM.CFG = &gConf.DBCfg
if *initConfig {
return
}
DM.Setup(gConf.IPFSAPI)
go catchSignals()
if *verifyFiles >= 0 {
log.Println(DM.VerifyFileIntegrity(*verifyFiles))
return
}
if *tombstoneFile != "" {
log.Println(DM.UpdateTombstone(*tombstoneFile))
return
}
if *genPears {
if err := DM.GeneratePears(); err != nil {
log.Fatal(err)
}
return
}
if *recalcApple {
if err := DM.RecalculateAppletree(); err != nil {
log.Fatal(err)
}
return
}
if *genPhash {
log.Print(DM.GenPhash())
return
}
if *upgradePostCids {
DM.UpgradePostCids()
return
}
if *updateUserFlags {
oldStr := flag.Arg(0)
newStr := flag.Arg(1)
old, err := strconv.Atoi(oldStr)
if err != nil {
fmt.Println("<Old> expected a number, got:", oldStr)
return
}
new, err := strconv.Atoi(newStr)
if err != nil {
fmt.Println("<New> expected a number, got:", newStr)
return
}
if err := DM.UpdateUserFlags(new, old); err != nil {
log.Fatal(err)
}
return
}
if *generateFileDim {
DM.GenerateFileDimensions()
return
}
if *calcChecksums {
if err := DM.CalculateChecksums(); err != nil {
log.Fatal(err)
}
return
}
if *generateFileSize {
if err := DM.GenerateFileSizes(); err != nil {
log.Fatal(err)
}
return
}
if *initDir {
DM.InitDir()
return
}
if *migrateStore >= 0 {
DM.MigrateStore(*migrateStore)
return
}
if *generateThumbnail > 0 {
DM.GenerateThumbnail(*generateThumbnail)
return
}
if *generateThumbnails > 0 {
DM.GenerateThumbnails(*generateThumbnails)
return
}
fs := http.FileServer(http.Dir("./static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/favicon.ico", faviconHandler)
//serveHiddenFiles("/hidden/")
for hk, hv := range h.Handlers {
http.HandleFunc(hk, hv)
}
http.Handle("/api/v1/", http.StripPrefix("/api/v1", apiv1.Mux))
if gConf.HTTPRedirect == "true" && gConf.HTTPSAddress != "" {
if gConf.HTTPAddress == "" {
gConf.HTTPAddress = ":80"
}
go log.Print(http.ListenAndServeTLS(gConf.HTTPSAddress, gConf.SSLCertPath, gConf.SSLKeyPath, nil))
log.Print(http.ListenAndServe(gConf.HTTPAddress, http.HandlerFunc(redirectHandler)))
} else {
if gConf.HTTPAddress == "" && gConf.HTTPSAddress != "" {
log.Print(http.ListenAndServeTLS(gConf.HTTPSAddress, gConf.SSLCertPath, gConf.SSLKeyPath, nil))
} else {
if gConf.HTTPSAddress != "" {
go log.Print(http.ListenAndServeTLS(gConf.HTTPSAddress, gConf.SSLCertPath, gConf.SSLKeyPath, nil))
}
log.Print(http.ListenAndServe(gConf.HTTPAddress, nil))
}
}
//http.HandleFunc("/static/", http.NotFound)
}
func catchSignals() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
err := DM.Close()
if err != nil {
log.Println(err)
}
os.Exit(1)
}