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