forked from jeroenpardon/sui
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
113 lines (89 loc) · 2.28 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
package main
import (
"fmt"
"net/http"
"text/template"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
"github.com/willfantom/sui/config"
"github.com/willfantom/sui/providers"
)
var (
indexData *IndexData
)
func main() {
log.SetLevel(log.InfoLevel)
log.Infof("SUI - home server dashboard")
err := config.LoadConfig()
if err != nil {
log.Fatalf("problem loading config")
}
if config.IsDebug() {
log.SetLevel(log.DebugLevel)
}
log.Debugf("parsed Config\n")
indexData = NewIndexData()
indexData.AppTitle = config.GetAppTitle()
addAppProviders()
addSearchEngines()
addBookmarks()
go refreshApps()
r := mux.NewRouter()
serveAssets(r)
r.HandleFunc("/", serveIndex)
r.HandleFunc("/js/search.js", serveSearchJS)
http.ListenAndServe(":6999", r)
}
func addAppProviders() {
var err error
for _, provider := range config.GetAppProviderConfigs() {
log.Debugf("adding provider | %s", provider.Name)
indexData.AppProviders[provider.Name], err = providers.NewAppProvider(provider.Name, provider.PType)
if err != nil {
panic(err)
}
}
}
func addSearchEngines() {
log.Debugf("adding search engines\n")
indexData.SearchEngines = config.GetSearchEngines()
}
func addBookmarks() {
log.Debugf("adding bookmarks\n")
indexData.Bookmarks = config.GetBookmarks()
for name, bmks := range indexData.Bookmarks {
log.Debugf("added bookmarks | category: %s | count: %d", name, len(*bmks))
}
}
func refreshApps() {
for true {
for name, prov := range indexData.AppProviders {
err := prov.RefreshApps()
if err != nil {
panic(err)
}
log.Debugf("found apps | provider: %s | app count: %d", name, len(prov.Apps))
}
time.Sleep(time.Duration(config.GetAppRefresh())*time.Second)
}
}
func serveIndex(w http.ResponseWriter, r *http.Request) {
fmt.Println("Serving Index")
var t = template.Must(template.ParseFiles("./templates/index.html"))
err := t.Execute(w, indexData)
if err != nil {
panic(err)
}
}
func serveSearchJS(w http.ResponseWriter, r *http.Request) {
var t = template.Must(template.ParseFiles("./templates/search.js"))
err := t.Execute(w, indexData)
if err != nil {
panic(err)
}
}
func serveAssets(r *mux.Router) {
fs := http.FileServer(http.Dir("./assets/"))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
}