From 99287b7f82c109155c2af10e2b93794a644279d8 Mon Sep 17 00:00:00 2001 From: aceberg <1502200+aceberg@users.noreply.github.com> Date: Sun, 25 Dec 2022 22:52:16 +0700 Subject: [PATCH] refactor --- .github/workflows/docker-readme.yml | 8 ++-- .github/workflows/main-docker-all.yml | 10 ++--- Makefile | 3 ++ cmd/WatchYourLAN/main.go | 30 +++---------- {pkg => internal}/conf/getconfig.go | 12 +++--- {pkg => internal}/db/db-connect.go | 0 {pkg => internal}/db/db-edit.go | 0 {pkg => internal}/models/models.go | 0 internal/web/index.go | 18 ++++++++ {pkg => internal}/web/templates/footer.html | 0 {pkg => internal}/web/templates/header.html | 0 {pkg => internal}/web/templates/index.html | 0 {pkg => internal}/web/templates/offline.html | 0 {pkg => internal}/web/webgui.go | 19 ++++++--- pkg/web/index.go | 45 -------------------- 15 files changed, 54 insertions(+), 91 deletions(-) rename {pkg => internal}/conf/getconfig.go (79%) rename {pkg => internal}/db/db-connect.go (100%) rename {pkg => internal}/db/db-edit.go (100%) rename {pkg => internal}/models/models.go (100%) create mode 100644 internal/web/index.go rename {pkg => internal}/web/templates/footer.html (100%) rename {pkg => internal}/web/templates/header.html (100%) rename {pkg => internal}/web/templates/index.html (100%) rename {pkg => internal}/web/templates/offline.html (100%) rename {pkg => internal}/web/webgui.go (73%) delete mode 100644 pkg/web/index.go diff --git a/.github/workflows/docker-readme.yml b/.github/workflows/docker-readme.yml index 8205277..314718a 100644 --- a/.github/workflows/docker-readme.yml +++ b/.github/workflows/docker-readme.yml @@ -2,10 +2,10 @@ name: DockerHub-README on: workflow_dispatch: - push: - branches: [ "main" ] - paths: - - 'README.md' + # push: + # branches: [ "main" ] + # paths: + # - 'README.md' env: IMAGE_NAME: watchyourlan diff --git a/.github/workflows/main-docker-all.yml b/.github/workflows/main-docker-all.yml index 55e2d7b..38a41cb 100644 --- a/.github/workflows/main-docker-all.yml +++ b/.github/workflows/main-docker-all.yml @@ -2,11 +2,11 @@ name: Docker on: workflow_dispatch: - push: - branches: [ "main" ] - paths: - - 'Dockerfile' - - 'src/**' + # push: + # branches: [ "main" ] + # paths: + # - 'Dockerfile' + # - 'src/**' env: IMAGE_NAME: watchyourlan diff --git a/Makefile b/Makefile index bf1173a..1cb6259 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ fmt: lint: golangci-lint run + golint ./... + +check: fmt lint go-build: cd cmd/WatchYourLAN/ && \ diff --git a/cmd/WatchYourLAN/main.go b/cmd/WatchYourLAN/main.go index e608f0a..4110a9f 100644 --- a/cmd/WatchYourLAN/main.go +++ b/cmd/WatchYourLAN/main.go @@ -2,38 +2,20 @@ package main import ( "fmt" - // "time" - "github.com/aceberg/WatchYourLAN/pkg/conf" - "github.com/aceberg/WatchYourLAN/pkg/db" - "github.com/aceberg/WatchYourLAN/pkg/web" - // . "github.com/aceberg/WatchYourLAN/pkg/models" + + "github.com/aceberg/WatchYourLAN/internal/conf" + "github.com/aceberg/WatchYourLAN/internal/db" + "github.com/aceberg/WatchYourLAN/internal/web" ) -// var AppConfig Conf -// var AllHosts []Host - -// func scan_and_compare() { -// var foundHosts []Host -// var dbHosts []Host -// for { // Endless -// foundHosts = arp_scan() // Scan interfaces -// dbHosts = db_select() // Select everything from DB -// db_setnow() // Mark hosts in DB as offline -// hosts_compare(foundHosts, dbHosts) // Compare hosts online and in DB -// // and add them to DB -// AllHosts = db_select() -// time.Sleep(time.Duration(AppConfig.Timeout) * time.Second) // Timeout -// } -// } +const configPath = "/data/config" func main() { - appConfig := conf.GetConfig() // Get config from Defaults, Config file, Env + appConfig := conf.GetConfig(configPath) // Get config from Defaults, Config file, Env fmt.Println("CONF =", appConfig) db.CreateDB(appConfig.DbPath) // Check if DB exists. Create if not - // go scan_and_compare() - web.Webgui(appConfig) // Start web GUI } diff --git a/pkg/conf/getconfig.go b/internal/conf/getconfig.go similarity index 79% rename from pkg/conf/getconfig.go rename to internal/conf/getconfig.go index f637966..08bbb04 100644 --- a/pkg/conf/getconfig.go +++ b/internal/conf/getconfig.go @@ -1,13 +1,11 @@ package conf import ( - . "github.com/aceberg/WatchYourLAN/pkg/models" + . "github.com/aceberg/WatchYourLAN/internal/models" "github.com/spf13/viper" ) -const configPath = "/data/config" - -func GetConfig() (config Conf) { +func GetConfig(path string) (config Conf) { viper.SetDefault("IFACE", "enp1s0") viper.SetDefault("DBPATH", "/data/db.sqlite") viper.SetDefault("GUIIP", "localhost") @@ -16,7 +14,7 @@ func GetConfig() (config Conf) { viper.SetDefault("SHOUTRRR_URL", "") viper.SetDefault("THEME", "solar") - viper.SetConfigFile(configPath) + viper.SetConfigFile(path) viper.SetConfigType("env") viper.ReadInConfig() @@ -33,8 +31,8 @@ func GetConfig() (config Conf) { return config } -func WriteConfig(theme string) { - viper.SetConfigFile(configPath) +func WriteConfig(path, theme string) { + viper.SetConfigFile(path) viper.SetConfigType("env") viper.Set("THEME", theme) viper.WriteConfig() diff --git a/pkg/db/db-connect.go b/internal/db/db-connect.go similarity index 100% rename from pkg/db/db-connect.go rename to internal/db/db-connect.go diff --git a/pkg/db/db-edit.go b/internal/db/db-edit.go similarity index 100% rename from pkg/db/db-edit.go rename to internal/db/db-edit.go diff --git a/pkg/models/models.go b/internal/models/models.go similarity index 100% rename from pkg/models/models.go rename to internal/models/models.go diff --git a/internal/web/index.go b/internal/web/index.go new file mode 100644 index 0000000..6c4d02d --- /dev/null +++ b/internal/web/index.go @@ -0,0 +1,18 @@ +package web + +import ( + // "fmt" + "html/template" + "net/http" + + "github.com/aceberg/WatchYourLAN/internal/models" +) + +func index(w http.ResponseWriter, r *http.Request) { + var guiData models.GuiData + guiData.Config = AppConfig + guiData.Hosts = []models.Host{} + + tmpl, _ := template.ParseFiles(TemplPath + "index.html", TemplPath + "header.html", TemplPath + "footer.html") + tmpl.ExecuteTemplate(w, "index", guiData) +} diff --git a/pkg/web/templates/footer.html b/internal/web/templates/footer.html similarity index 100% rename from pkg/web/templates/footer.html rename to internal/web/templates/footer.html diff --git a/pkg/web/templates/header.html b/internal/web/templates/header.html similarity index 100% rename from pkg/web/templates/header.html rename to internal/web/templates/header.html diff --git a/pkg/web/templates/index.html b/internal/web/templates/index.html similarity index 100% rename from pkg/web/templates/index.html rename to internal/web/templates/index.html diff --git a/pkg/web/templates/offline.html b/internal/web/templates/offline.html similarity index 100% rename from pkg/web/templates/offline.html rename to internal/web/templates/offline.html diff --git a/pkg/web/webgui.go b/internal/web/webgui.go similarity index 73% rename from pkg/web/webgui.go rename to internal/web/webgui.go index b4ed391..a8777b3 100644 --- a/pkg/web/webgui.go +++ b/internal/web/webgui.go @@ -1,21 +1,28 @@ package web import ( - "embed" + // "embed" "fmt" - . "github.com/aceberg/WatchYourLAN/pkg/models" "log" "net/http" + + "github.com/aceberg/WatchYourLAN/internal/models" ) -var AppConfig Conf +var ( + AppConfig models.Conf + + TemplPath string +) // var AllHosts []Host -//go:embed templates/* -var TemplHTML embed.FS +////go:embed templates/* +// var TemplHTML embed.FS -func Webgui(appConfig Conf) { +func Webgui(appConfig models.Conf) { + + TemplPath = "../../internal/web/templates/" AppConfig = appConfig address := AppConfig.GuiIP + ":" + AppConfig.GuiPort diff --git a/pkg/web/index.go b/pkg/web/index.go deleted file mode 100644 index 1e4b8b2..0000000 --- a/pkg/web/index.go +++ /dev/null @@ -1,45 +0,0 @@ -package web - -import ( - // "fmt" - "html/template" - "net/http" - // "strconv" - . "github.com/aceberg/WatchYourLAN/pkg/models" -) - -func index(w http.ResponseWriter, r *http.Request) { - var guiData GuiData - guiData.Config = AppConfig - guiData.Hosts = []Host{} - - tmpl, _ := template.ParseFS(TemplHTML, "templates/index.html", "templates/header.html", "templates/footer.html") - tmpl.ExecuteTemplate(w, "index", guiData) -} - -// func update_host(w http.ResponseWriter, r *http.Request) { -// idStr := r.FormValue("id") -// name := r.FormValue("name") -// knownStr := r.FormValue("known") - -// if idStr == "" { -// fmt.Fprintf(w, "No data!") -// } else { -// var known uint16 -// id, _ := strconv.Atoi(idStr) -// known = 0 -// if knownStr == "on" { -// known = 1 -// } - -// for i, oneHost := range AllHosts { -// if oneHost.Id == uint16(id) { -// AllHosts[i].Name = name -// AllHosts[i].Known = known -// db_update(AllHosts[i]) -// } -// } -// } - -// http.Redirect(w, r, r.Header.Get("Referer"), 302) -// }