-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
110 lines (94 loc) · 3.19 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
// Copyright (C) 2020 David Vogel
//
// This file is part of Galago.
//
// Galago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Galago is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Galago. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"time"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/bmp"
"github.com/Dadido3/configdb"
"github.com/gorilla/mux"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
"github.com/snowzach/rotatefilehook"
)
var log = logrus.New()
var conf = configdb.NewOrPanic([]configdb.Storage{
configdb.UseYAMLFile(filepath.Join(".", "config", "config.yaml")),
})
var router = mux.NewRouter()
var validExtensions = map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".bmp": true}
func main() {
var verbosity logrus.Level
if err := conf.Get(".Logging.Verbosity", &verbosity); err != nil {
log.Fatalf("Can't load verbosity from config files: %v", err)
}
// Logging
os.MkdirAll(filepath.Join(".", "log"), os.ModePerm)
rotateFileHook, err := rotatefilehook.NewRotateFileHook(rotatefilehook.RotateFileConfig{
Filename: filepath.Join(".", "log", "newest.log"),
MaxSize: 50, // megabytes
MaxBackups: 3,
MaxAge: 365, //days
Level: verbosity,
Formatter: &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
//return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line)
return fmt.Sprintf("%s():%d", f.Function, f.Line), ""
},
},
})
if err != nil {
log.Fatalf("Failed to initialize file rotate hook: %v", err)
}
log.SetReportCaller(true)
log.AddHook(rotateFileHook)
log.SetLevel(verbosity)
log.SetOutput(colorable.NewColorableStdout())
log.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC3339,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
//return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line)
return fmt.Sprintf("%s():%d", f.Function, f.Line), ""
},
})
var cachePath string
if err := conf.Get(".Cache.Path", &cachePath); err != nil {
log.Fatalf("Can't load cache path from config files: %v", err)
}
os.MkdirAll(cachePath, os.ModePerm)
cache = NewCache(cachePath)
// Add routes to the webserver
serverUIInit()
loadSources()
log.Infof("Galago %v started", version)
var addr string
if err := conf.Get(".Server.ListenAddress", &addr); err != nil {
log.Fatalf("Can't load server listen address from config files: %v", err)
}
log.Infof("Listening at %v", addr)
log.Fatalf("Webserver returned error: %v", http.ListenAndServe(addr, router))
}