Skip to content

Commit

Permalink
Minor UI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ordspilleren committed May 23, 2021
1 parent 564ecda commit c486bb7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 22 deletions.
2 changes: 1 addition & 1 deletion html/monitorlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<form method="POST">
<input type="hidden" name="monitorid" value="{{$index}}"></input>
<div class="buttons">
{{if .Started}}
{{if .IsRunning}}
<input type="submit" class="button is-small is-danger" name="stop" value="Stop"></input>
{{else}}
<input type="submit" class="button is-small is-primary" name="start" value="Start"></input>
Expand Down
22 changes: 18 additions & 4 deletions html/monitornew.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,42 @@
<div class="field">
<label class="label">Name</label>
<div class="control">
<input name="name" class="input" type="text" placeholder="Text input">
<input name="name" class="input" type="text">
</div>
</div>

<div class="field">
<label class="label">URL</label>
<div class="control">
<input name="url" class="input" type="text" placeholder="Text input">
<input name="url" class="input" type="text">
</div>
</div>

<div class="field">
<label class="label">Interval</label>
<div class="control">
<input name="interval" class="input" type="number" placeholder="Text input">
<input name="interval" class="input" type="number">
</div>
</div>

<div class="field">
<label class="label">CSS Selectors</label>
<div class="control">
<textarea name="cssselectors" class="textarea"></textarea>
</div>
</div>

<div class="field">
<label class="label">JSON Selectors</label>
<div class="control">
<textarea name="jsonselectors" class="textarea"></textarea>
</div>
</div>

<div class="field">
<label class="label">Notifiers</label>
<div class="control">
<input name="notifier" class="input" type="text" placeholder="Text input">
<input name="notifier" class="input" type="text">
</div>
</div>

Expand Down
64 changes: 50 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"

"github.com/Ordspilleren/ChangeMonitor/html"
Expand All @@ -16,12 +18,13 @@ import (

var wg = &sync.WaitGroup{}

var ConfigFile string
var StorageDirectory string
var EnableWebUI bool

type Config struct {
ConfigFile string
StorageDirectory string
EnableWebUI bool
Monitors monitor.Monitors `json:"monitors"`
Notifiers notify.Notifiers `json:"notifiers"`
Monitors monitor.Monitors `json:"monitors"`
Notifiers notify.Notifiers `json:"notifiers"`
}

var config Config
Expand All @@ -35,13 +38,13 @@ func getEnv(key, fallback string) string {
}

func init() {
config.ConfigFile = getEnv("CONFIG_FILE", "config.json")
config.StorageDirectory = getEnv("STORAGE_DIRECTORY", "data")
config.EnableWebUI, _ = strconv.ParseBool(getEnv("ENABLE_WEBUI", "true"))
log.Printf("Config File: %s", config.ConfigFile)
log.Printf("Storage Directory: %s", config.StorageDirectory)
ConfigFile = getEnv("CONFIG_FILE", "config.json")
StorageDirectory = getEnv("STORAGE_DIRECTORY", "data")
EnableWebUI, _ = strconv.ParseBool(getEnv("ENABLE_WEBUI", "true"))
log.Printf("Config File: %s", ConfigFile)
log.Printf("Storage Directory: %s", StorageDirectory)

b, err := ioutil.ReadFile(config.ConfigFile)
b, err := ioutil.ReadFile(ConfigFile)
if err != nil {
log.Print(err)
return
Expand All @@ -56,15 +59,24 @@ func init() {
}

func main() {
config.Monitors.StartMonitoring(wg, notifierMap, config.StorageDirectory)
config.Monitors.StartMonitoring(wg, notifierMap, StorageDirectory)

if config.EnableWebUI {
if EnableWebUI {
startHTTPServer()
} else {
wg.Wait()
}
}

func (t *Config) JSON() ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", "\t")
err := encoder.Encode(t)
return buffer.Bytes(), err
}

func startHTTPServer() {
http.Handle("/assets/", http.FileServer(html.GetAssetFS()))
http.HandleFunc("/", monitorList)
Expand Down Expand Up @@ -97,6 +109,12 @@ func monitorList(w http.ResponseWriter, r *http.Request) {
func monitorNew(w http.ResponseWriter, r *http.Request) {
p := html.MonitorNewParams{}

monitorID := r.URL.Query().Get("id")
if monitorID != "" {
id, _ := strconv.ParseInt(monitorID, 10, 64)
p.Monitor = &config.Monitors[id]
}

if r.Method != http.MethodPost {
html.MonitorNew(w, p)
return
Expand All @@ -108,14 +126,32 @@ func monitorNew(w http.ResponseWriter, r *http.Request) {
if err != nil {
log.Print(err)
}
cssSelectors := r.FormValue("cssselectors")
jsonSelectors := r.FormValue("jsonselectors")
notifiers := r.Form["notifier"]

monitor := monitor.NewMonitor(name, url, interval, notifiers)
monitor.Init(notifierMap, config.StorageDirectory)

if cssSelectors != "" {
cssSelectorSlice := strings.Split(cssSelectors, "\n")
monitor.AddCSSSelectors(cssSelectorSlice...)
}
if jsonSelectors != "" {
jsonSelectorSlice := strings.Split(jsonSelectors, "\n")
monitor.AddCSSSelectors(jsonSelectorSlice...)
}

monitor.Init(notifierMap, StorageDirectory)

p.Success = true

config.Monitors = append(config.Monitors, *monitor)

newConfig, _ := config.JSON()
err = ioutil.WriteFile(ConfigFile, newConfig, 0644)
if err != nil {
log.Print(err)
}

html.MonitorNew(w, p)
}
19 changes: 16 additions & 3 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Monitor struct {
UseChrome bool `json:"useChrome"`
Selectors Selectors `json:"selectors,omitempty"`
Interval time.Duration `json:"interval"`
Notifiers []string `json:"notifiers"`
Started bool
Notifiers []string `json:"notifiers,omitempty"`
started bool
doneChannel chan bool
ticker *time.Ticker
id string
Expand Down Expand Up @@ -64,6 +64,14 @@ func NewMonitor(name string, url string, interval int64, notifiers []string) *Mo
return monitor
}

func (m *Monitor) AddCSSSelectors(selectors ...string) {
m.Selectors.CSS = &selectors
}

func (m *Monitor) AddJSONSelectors(selectors ...string) {
m.Selectors.JSON = &selectors
}

func (m *Monitor) Init(notifierMap notify.NotifierMap, storageDirectory string) {
m.id = generateSHA1String(m.URL)
m.doneChannel = make(chan bool)
Expand All @@ -83,13 +91,14 @@ func (m *Monitor) Init(notifierMap notify.NotifierMap, storageDirectory string)

func (m *Monitor) Start(wg *sync.WaitGroup) {
wg.Add(1)
m.Started = true
m.started = true
go func(monitor *Monitor) {
monitor.check()
for {
select {
case <-monitor.doneChannel:
wg.Done()
m.started = false
return
case <-monitor.ticker.C:
monitor.check()
Expand All @@ -102,6 +111,10 @@ func (m *Monitor) Stop() {
m.doneChannel <- true
}

func (m *Monitor) IsRunning() bool {
return m.started
}

func (m Monitors) StartMonitoring(wg *sync.WaitGroup, notifierMap notify.NotifierMap, storageDirectory string) {
for idx := range m {
m[idx].Init(notifierMap, storageDirectory)
Expand Down

0 comments on commit c486bb7

Please sign in to comment.