-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
126 lines (110 loc) · 2.79 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/robrotheram/gogallery/config"
"github.com/robrotheram/gogallery/datastore"
"github.com/robrotheram/gogallery/web"
"github.com/robrotheram/gogallery/worker"
"github.com/spf13/viper"
"golang.org/x/crypto/bcrypt"
)
func generatePassword() {
args := os.Args[1:]
if len(args) == 2 {
if args[0] == "generate" {
hash, err := bcrypt.GenerateFromPassword([]byte(args[1]), bcrypt.MinCost)
if err != nil {
log.Println(err)
}
fmt.Printf("Your Admin Password is: %s \nPlease Copy it into the config \n", string(hash))
os.Exit(0)
} else {
log.Fatalf("Unsure what the argument [ %s ] did you mean generate", args[0])
}
} else if len(args) == 0 {
return
} else {
log.Fatal("Invalid Arguments")
}
}
var Config *config.Configuration
func main() {
generatePassword()
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetEnvPrefix("GLLRY")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
err := viper.Unmarshal(&Config)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
if _, err := os.Stat(Config.Gallery.Basepath); os.IsNotExist(err) {
panic("GALLERY DIRECTORY NOT FOUND EXITING!")
}
worker.StartWorkers()
go setUpWatchers(Config.Gallery.Basepath)
datastore.Cache = datastore.NewDataStore(&Config.Database)
go func() {
datastore.ScanPath(Config.Gallery.Basepath, &Config.Gallery)
}()
go func() {
if Config.IG.SyncRate == 0 {
fmt.Println("Instagram Sync Cancled")
return
}
d := time.Duration(Config.IG.SyncRate) * time.Minute
for range time.Tick(d) {
if Config.IG.Enable {
fmt.Println("Running Task: Instagram Sync")
datastore.IG.SyncFrom()
}
}
}()
web.Serve(Config)
}
var watcher *fsnotify.Watcher
// main
func setUpWatchers(path string) {
// creates a new file watcher
watcher, _ = fsnotify.NewWatcher()
defer watcher.Close()
// starting at the root of the project, walk each file/directory searching for
// directories
if err := filepath.Walk(path, watchDir); err != nil {
fmt.Println("ERROR", err)
}
//
done := make(chan bool)
//
go func() {
for {
select {
// watch for events
case _ = <-watcher.Events:
fmt.Println("File chnage detected. Scanning files")
datastore.ScanPath(Config.Gallery.Basepath, &Config.Gallery)
// watch for errors
case err := <-watcher.Errors:
fmt.Println("ERROR", err)
}
}
}()
<-done
}
// watchDir gets run as a walk func, searching for directories to add watchers to
func watchDir(path string, fi os.FileInfo, err error) error {
if fi.Mode().IsDir() {
return watcher.Add(path)
}
return nil
}