Skip to content

Commit

Permalink
Add static apps
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdkleijn committed Jan 22, 2024
1 parent f9a515f commit cac3444
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 157 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ go 1.21
require (
github.com/gorilla/mux v1.8.1
github.com/rs/cors v1.10.1
github.com/rs/zerolog v1.31.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.18.2
)

require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand All @@ -6,6 +7,7 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
Expand All @@ -21,17 +23,26 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
Expand Down Expand Up @@ -63,6 +74,9 @@ go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN8
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
89 changes: 56 additions & 33 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
package config

import (
"fmt"
"os"
"strings"

log "github.com/sirupsen/logrus"
m "github.com/mvdkleijn/homedash/internal/models"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

Expand Down Expand Up @@ -43,16 +47,16 @@ type IconConfiguration struct {
}

type StaticConfiguration struct {
Apps []AppInfo
Apps []m.ContainerInfo
}

type AppInfo struct {
Name string `json:"name"`
Url string `json:"url"`
Icon string `json:"icon"`
IconFile string `json:"iconFile"`
Comment string `json:"comment"`
}
// type AppInfo struct {
// Name string `json:"name"`
// Url string `json:"url"`
// Icon string `json:"icon"`
// IconFile string `json:"iconFile"`
// Comment string `json:"comment"`
// }

type CorsConfiguration struct {
AllowedOrigins []string
Expand All @@ -64,7 +68,7 @@ type CorsConfiguration struct {

var (
Config Configuration
Logger *log.Logger
Logger *zerolog.Logger
Index IconIndex = IconIndex{}
)

Expand All @@ -77,7 +81,7 @@ func initViper() {
viper.SetDefault("debug", false)
viper.SetDefault("maxAge", "20")
if isRunningInContainer() {
Logger.Debugln("detected that we're runnning in a container, using /homedash as default data directory")
Logger.Debug().Msg("detected that we're runnning in a container, using /homedash as default data directory")
viper.SetDefault("icons.tmpDir", "/homedash/tmp")
viper.SetDefault("icons.cacheDir", "/homedash/cache")
} else {
Expand All @@ -92,7 +96,7 @@ func initViper() {
viper.SetDefault("cors.debug", false)

// Set default values for the static configuration
viper.SetDefault("apps", []AppInfo{})
viper.SetDefault("apps", []m.ContainerInfo{})

viper.SetEnvPrefix("homedash")

Expand All @@ -105,9 +109,9 @@ func initViper() {

err := viper.ReadInConfig()
if err != nil {
Logger.Infof("tried to load configuration file but found none")
Logger.Info().Msg("tried to load configuration file but found none")
} else {
Logger.Infof("loaded configuration file: %v", viper.ConfigFileUsed())
Logger.Info().Str("configfile", viper.ConfigFileUsed()).Msg("loaded configuration file")
}

viper.AutomaticEnv()
Expand All @@ -125,13 +129,13 @@ func initViper() {
Config.Cors.AllowedMethods = viper.GetStringSlice("cors.allowedMethods")
Config.Cors.Debug = viper.GetBool("cors.debug")

var appInfos []AppInfo
var appInfos []m.ContainerInfo
if apps := viper.Get("static.apps"); apps != nil {
if appSlice, ok := apps.([]interface{}); ok {
appInfos = make([]AppInfo, len(appSlice))
appInfos = make([]m.ContainerInfo, len(appSlice))
for i, v := range appSlice {
if appMap, ok := v.(map[string]interface{}); ok {
appInfo := AppInfo{
appInfo := m.ContainerInfo{
Name: appMap["name"].(string),
}
if url, ok := appMap["url"].(string); ok {
Expand All @@ -140,45 +144,46 @@ func initViper() {
if icon, ok := appMap["icon"].(string); ok {
appInfo.Icon = icon
}
if iconFile, ok := appMap["iconFile"].(string); ok {
appInfo.IconFile = iconFile
}
if comment, ok := appMap["comment"].(string); ok {
appInfo.Comment = comment
}
appInfo.IconFile = GetIconPath(appInfo.Icon)
appInfos[i] = appInfo
} else {
continue
}
}
Logger.Infof("Loaded static configuration: %v", appInfos)
Logger.Info().Str("appInfos", fmt.Sprintf("%v", appInfos)).Msg("Loaded static configuration")
} else {
Logger.Infof("Skipping invalid static configuration: %v", apps)
Logger.Info().Str("apps", fmt.Sprintf("%v", apps)).Msg("Skipping invalid static configuration")
}
}

Config.Static.Apps = appInfos
}

func init() {
Logger = log.New()
Logger.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})

Logger.Info("initializing system")
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().Timestamp().Logger()
Logger = &log
// Logger.SetFormatter(&log.TextFormatter{
// DisableColors: false,
// FullTimestamp: true,
// })

log.Info().Msg("initializing system")
initViper()

zerolog.SetGlobalLevel(zerolog.InfoLevel)
if Config.Global.Debug {
Logger.SetLevel(log.DebugLevel)
Logger.Debug("enabled DEBUG logging level")
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("enabled DEBUG logging level")
}

UpdateIcons(false)
UpdateIconPaths()

Logger.Info("initialization completed")
Logger.Debugf("dumping active configuration: %v", Config)
log.Info().Msg("initialization completed")
Logger.Debug().Interface("config", Config).Msg("dumping active configuration")
}

func isRunningInContainer() bool {
Expand All @@ -187,3 +192,21 @@ func isRunningInContainer() bool {
}
return true
}

func UpdateIconPaths() {
for i := range Config.Static.Apps {
Config.Static.Apps[i].IconFile = GetIconPath(Config.Static.Apps[i].Icon)
}
}

func GetIconPath(icon string) string {
log.Info().Str("icon", icon).Msg("getting icon path")
value, exists := Index[icon]
log.Debug().Interface("index", Index).Msg("icon index")

if !exists {
return "/static/default-icon.svg"
}

return fmt.Sprintf("/icons/%s", value)
}
4 changes: 2 additions & 2 deletions internal/config/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
)

func downloadFile(url string, filepath string) error {
Logger.Debugf("Attempting to download update from %s", url)
Logger.Debug().Str("url", url).Msg("attempting to download update from url")

response, err := http.Get(url)
if err != nil {
Expand Down Expand Up @@ -100,7 +100,7 @@ func UpdateIcons(refresh bool) {
// Check if the directory exists
_, err := os.Stat(filepath.Join(Config.Icons.CacheDir, "applications_index.json"))
if err == nil && !refresh {
Logger.Infof("already have icons and not asked to refresh")
Logger.Info().Msg("already have icons and not asked to refresh")
createIndexFromCache()
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ type ContainerInfo struct {
IconFile string `json:"iconFile"`
Comment string `json:"comment"`
}

type ContainerUpdate struct {
Uuid string `json:"uuid"`
Containers []ContainerInfo `json:"containers"`
}
14 changes: 14 additions & 0 deletions internal/repositories/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package repositories

import (
c "github.com/mvdkleijn/homedash/internal/config"
m "github.com/mvdkleijn/homedash/internal/models"
)

func GetAppList() map[string][]m.ContainerInfo {
appList := make(map[string][]m.ContainerInfo)

appList["static"] = c.Config.Static.Apps

return appList
}
Loading

0 comments on commit cac3444

Please sign in to comment.