-
Notifications
You must be signed in to change notification settings - Fork 15
/
wpdir.go
169 lines (130 loc) · 3.3 KB
/
wpdir.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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"time"
"github.com/wpdirectory/wpdir/internal/config"
"github.com/wpdirectory/wpdir/internal/db"
"github.com/wpdirectory/wpdir/internal/log"
"github.com/wpdirectory/wpdir/internal/metrics"
"github.com/wpdirectory/wpdir/internal/server"
"github.com/wpdirectory/wpdir/internal/tasks"
)
//go:generate go run -tags=dev embed_files.go
var (
version string
commit string
date string
)
func main() {
// Set and Parse flags
flagHelp := flag.Bool("help", false, "Display help information")
flagFresh := flag.Bool("fresh", false, "Begin with fresh data load")
flagDev := flag.Bool("dev", false, "Enable Dev mode")
flag.Parse()
if *flagHelp {
fmt.Println(helpText)
os.Exit(1)
}
fmt.Println("Starting WPDirectory")
// Create Logger
l := log.New()
// Create Config
c := config.Setup(version, commit, date, *flagDev)
l.Printf("Hostname: %s\n", c.Host)
// Ensure Directory Structure Exists
mkdirs(c.WD)
// Set Temp Dir
// TODO: Check error- what would we do?
setTempDir(c.WD)
// Setup Metrics
metrics.Setup()
// Setup BoltDB
db.Setup(c.WD)
defer db.Close()
// Setup server struct to hold all App data
s := server.New(l, c, flagFresh)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// Setup HTTP server.
s.Setup()
// Clean up temp dir
tasks.Add("0 */15 * * * *", emptyTempDir)
// Start Task Runner
tasks.Start()
<-stop
l.Printf("Shutting down WPdir...\n")
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Shutdown
tasks.Stop()
s.Shutdown(ctx)
}
const (
helpText = `WPDirectory is a web service for lightning fast code searching of the WordPress Plugin & Theme Directories.
Usage:
wpdir [flags]
Flags:
-help Help outputs help text and exits.
-fresh Begins a fresh load, all extensions are queued for updating.
Config:
WPDirectory requires a config file, located at /etc/wpdir/ or in the working directory, to successfully run. See the example-config.yml.`
)
func mkdirs(wd string) {
tmp := filepath.Join(wd, "tmp")
os.MkdirAll(tmp, 0766)
db := filepath.Join(wd, "data", "db")
os.MkdirAll(db, 0766)
ssl := filepath.Join(wd, "data", "ssl")
os.MkdirAll(ssl, 0760)
plugins := filepath.Join(wd, "data", "index", "plugins")
os.MkdirAll(plugins, 0766)
themes := filepath.Join(wd, "data", "index", "themes")
os.MkdirAll(themes, 0766)
}
// setTempDir sets the temp dir
// WPdir creates a lot of temp files which need
// to be cleaned up by the program itself
func setTempDir(wd string) error {
path := filepath.Join(wd, "tmp")
switch opsys := runtime.GOOS; opsys {
case "windows":
err := os.Setenv("TMP", path)
return err
case "darwin":
err := os.Setenv("TMPDIR", path)
return err
case "linux":
err := os.Setenv("TMPDIR", path)
return err
default:
return nil
}
}
// emptyTempDir deletes the contents of the temp dir
// Clears out temp files created during indexing
func emptyTempDir() {
wd, _ := os.Getwd()
dir := filepath.Join(wd, "tmp")
d, err := os.Open(dir)
if err != nil {
return
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return
}
}
return
}