This commit is contained in:
stryan 2023-07-30 20:05:22 -04:00
parent 65882afcff
commit 98542afd24

29
main.go
View File

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@ -20,9 +19,9 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
func load_config(filename string) (*Config, error) { func loadConfig(filename string) (*Config, error) {
u := &Config{} u := &Config{}
yamlFile, err := ioutil.ReadFile(filename) yamlFile, err := os.ReadFile(filename)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,7 +52,7 @@ func main() {
} }
debug := os.Getenv("WATCH_DEBUG") debug := os.Getenv("WATCH_DEBUG")
watchusers, err := load_config(file) watchusers, err := loadConfig(file)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -89,27 +88,35 @@ func main() {
<-c <-c
log.Info("trying to shutdown cleanly") log.Info("trying to shutdown cleanly")
endFn() endFn()
server.Shutdown(ctx) err := server.Shutdown(ctx)
if err != nil {
log.Warn("error shutting down tilde server", "error", err)
}
}() }()
wg.Add(1) wg.Add(1)
go func() { go func() {
server.ListenAndServe() err := server.ListenAndServe()
if err != nil {
log.Warn("error shutting down web server", "error", err)
}
wg.Done() wg.Done()
log.Info("watch web server shutdown") log.Info("watch web server shutdown")
}() }()
wg.Add(1) wg.Add(1)
go func() { go func() {
a.run(ctx) err := a.run(ctx)
if err != nil {
log.Warn("error shutting down update server", "error", err)
}
wg.Done() wg.Done()
log.Info("watch updater shutdown") log.Info("watch updater shutdown")
}() }()
log.Info("watch running") log.Info("watch running")
wg.Wait() wg.Wait()
log.Info("shut down") log.Info("shut down")
return
} }
func (watch *Watch) server() http.Handler { func (watch *Watch) server() http.Handler {
@ -150,7 +157,7 @@ func (watch *Watch) run(ctx context.Context) error {
case <-ticker.C: case <-ticker.C:
for i, v := range watch.Tildes { for i, v := range watch.Tildes {
if v.Update { if v.Update {
err := watch.run_update(i, v) err := watch.runUpdate(i, v)
if err != nil { if err != nil {
log.Warn("error running update", "error", err) log.Warn("error running update", "error", err)
} }
@ -164,7 +171,7 @@ func (watch *Watch) run(ctx context.Context) error {
} }
} }
func (watch *Watch) run_update(token string, reg Registration) error { func (watch *Watch) runUpdate(token string, reg Registration) error {
if reg.Domain == "" || reg.IPAddr == "" { if reg.Domain == "" || reg.IPAddr == "" {
return fmt.Errorf("invalid registration for update %v: %v", reg.Domain, reg.IPAddr) return fmt.Errorf("invalid registration for update %v: %v", reg.Domain, reg.IPAddr)
} }