Skip to content

Commit

Permalink
Split into packages + basic web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Ordspilleren committed May 22, 2021
1 parent c869eab commit e30e567
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 260 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@

# Dependency directories (remove the comment below to include it)
# vendor/

data/

config.json
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ go 1.16

require (
github.com/PuerkitoBio/goquery v1.6.1
github.com/nikoksr/notify v0.16.1
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/pkg/errors v0.9.1
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
github.com/tidwall/gjson v1.7.5
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
179 changes: 0 additions & 179 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions html/assets/bulma.min.css

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions html/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package html

import (
"embed"
"html/template"
"io"
"net/http"

"github.com/Ordspilleren/ChangeMonitor/monitor"
)

//go:embed *.html
var htmlTemplates embed.FS

//go:embed assets
var assets embed.FS

var (
monitorList = parse("monitorlist.html")
)

type MonitorListParams struct {
Monitors *monitor.Monitors
}

func MonitorList(w io.Writer, p MonitorListParams) error {
return monitorList.Execute(w, p)
}

func parse(file string) *template.Template {
return template.Must(
template.New("layout.html").ParseFS(htmlTemplates, "layout.html", file))
}

func GetAssetFS() http.FileSystem {
return http.FS(assets)
}
27 changes: 27 additions & 0 deletions html/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Change Monitor</title>
<link rel="stylesheet" href="assets/bulma.min.css">
</head>

<body>
<section class="section">
<div class="container">
<div class="columns is-centered">
<div class="column is-half">
<h1 class="title">
Change Monitor
</h1>

{{block "content" .}}{{end}}
</div>
</div>
</div>
</section>
</body>

</html>
27 changes: 27 additions & 0 deletions html/monitorlist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{{define "content"}}
<table class="table is-striped">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{range .Monitors}}
<tr>
<td>{{.Name}}</td>
<td><a
href="{{.URL}}">{{.URL}}</a>
</td>
<td>
<div class="buttons">
<button class="button is-small is-primary">Primary</button>
<button class="button is-small is-danger">Link</button>
</div>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
30 changes: 25 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,26 @@ import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"sync"

"github.com/Ordspilleren/ChangeMonitor/html"
"github.com/Ordspilleren/ChangeMonitor/monitor"
"github.com/Ordspilleren/ChangeMonitor/notify"
)

var wg = &sync.WaitGroup{}

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

var config Config
var notifiers NotifierMap
var notifiers notify.NotifierMap

func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
Expand Down Expand Up @@ -48,7 +53,22 @@ func init() {
}

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

//wg.Wait()

startHTTPServer()
}

wg.Wait()
func startHTTPServer() {
http.Handle("/assets/", http.FileServer(html.GetAssetFS()))
http.HandleFunc("/", monitorList)
http.ListenAndServe(":8080", nil)
}

func monitorList(w http.ResponseWriter, r *http.Request) {
p := html.MonitorListParams{
Monitors: &config.Monitors,
}
html.MonitorList(w, p)
}
23 changes: 11 additions & 12 deletions monitor.go → monitor/monitor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package monitor

import (
"context"
Expand All @@ -12,8 +12,9 @@ import (
"sync"
"time"

"github.com/Ordspilleren/ChangeMonitor/notify"
"github.com/Ordspilleren/ChangeMonitor/storage"
"github.com/PuerkitoBio/goquery"
"github.com/nikoksr/notify"
"github.com/tidwall/gjson"
)

Expand All @@ -38,9 +39,9 @@ type Monitor struct {
doneChannel chan bool
ticker *time.Ticker
id string
notifier *notify.Notify
notifiers notify.NotifierList
client MonitorClient
storage *Storage
storage *storage.Storage
}

type Selectors struct {
Expand All @@ -50,16 +51,14 @@ type Selectors struct {

type Monitors []Monitor

func (m *Monitor) Init(notifierMap NotifierMap) {
func (m *Monitor) Init(notifierMap notify.NotifierMap, storageDirectory string) {
m.id = generateSHA1String(m.URL)
m.doneChannel = make(chan bool)
m.ticker = time.NewTicker(m.Interval * time.Minute)
m.storage = InitStorage(m.id)

m.notifier = notify.New()
m.storage = storage.InitStorage(m.id, storageDirectory)

for _, notifier := range m.Notifiers {
m.notifier.UseServices(notifierMap[notifier])
m.notifiers = append(m.notifiers, notifierMap[notifier])
}

if m.UseChrome {
Expand Down Expand Up @@ -89,9 +88,9 @@ func (m *Monitor) Stop() {
m.doneChannel <- true
}

func (m Monitors) StartMonitoring(wg *sync.WaitGroup, notifierMap NotifierMap) {
func (m Monitors) StartMonitoring(wg *sync.WaitGroup, notifierMap notify.NotifierMap, storageDirectory string) {
for idx := range m {
m[idx].Init(notifierMap)
m[idx].Init(notifierMap, storageDirectory)
m[idx].Start(wg)
}
}
Expand All @@ -112,7 +111,7 @@ func (m *Monitor) check() {
if m.compareContent(storageContent, selectorContent) {
m.storage.WriteContent(selectorContent)
log.Print("Content has changed!")
_ = m.notifier.Send(
_ = m.notifiers.Send(
context.Background(),
fmt.Sprintf("<b><u>%s has changed!</u></b>", m.Name),
fmt.Sprintf("<b>New content:</b>\n%.200s\n\n<b>Old content:</b>\n%.200s\n\n<b>URL:</b> %s", selectorContent, storageContent, m.URL),
Expand Down
56 changes: 0 additions & 56 deletions notify.go

This file was deleted.

61 changes: 61 additions & 0 deletions notify/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package notify

import (
"context"

"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

type Notifier interface {
Init()
Send(context.Context, string, string) error
}

type NotifierList []Notifier

type NotifierMap map[string]Notifier

type Notifiers struct {
Mailgun *Mailgun `json:"mailgun"`
Telegram *Telegram `json:"telegram"`
}

type Mailgun struct {
Domain string `json:"domain"`
ApiKey string `json:"apiKey"`
SenderAddress string `json:"senderAddress"`
WithEurope bool `json:"withEurope"`
Receivers []string `json:"receivers"`
}

func (n *Notifiers) InitNotifiers() NotifierMap {
notifiers := make(NotifierMap)

if n.Telegram != nil {
n.Telegram.Init()
notifiers["telegram"] = n.Telegram
}

return notifiers
}

func (n NotifierList) Send(ctx context.Context, subject, message string) error {
var eg errgroup.Group

for _, service := range n {
if service != nil {
s := service
eg.Go(func() error {
return s.Send(ctx, subject, message)
})
}
}

err := eg.Wait()
if err != nil {
err = errors.Wrap(err, err.Error())
}

return err
}
Loading

0 comments on commit e30e567

Please sign in to comment.