router7/cmd/backupd/backupd.go

71 lines
1.7 KiB
Go
Raw Permalink Normal View History

2018-06-28 13:39:48 +02:00
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-06-26 18:01:01 +02:00
// Binary backupd provides tarballs of /perm.
package main
import (
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/gokrazy/gokrazy"
2018-07-09 08:54:04 +02:00
"github.com/rtr7/router7/internal/backup"
"github.com/rtr7/router7/internal/multilisten"
"github.com/rtr7/router7/internal/teelogger"
2018-06-26 18:01:01 +02:00
)
var log = teelogger.NewConsole()
var httpListeners = multilisten.NewPool()
func updateListeners() error {
hosts, err := gokrazy.PrivateInterfaceAddrs()
if err != nil {
return err
}
httpListeners.ListenAndServe(hosts, func(host string) multilisten.Listener {
return &http.Server{Addr: net.JoinHostPort(host, "8077")}
})
return nil
}
func logic() error {
http.HandleFunc("/backup.tar.gz", func(w http.ResponseWriter, r *http.Request) {
if err := backup.Archive(w, "/perm"); err != nil {
log.Printf("backup.tar.gz: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
updateListeners()
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGUSR1)
for range ch {
if err := updateListeners(); err != nil {
log.Printf("updateListeners: %v", err)
}
}
return nil
}
func main() {
if err := logic(); err != nil {
log.Fatal(err)
}
}