История изменений
Исправление beastie, (текущая версия) :
Как-то так:
package main
import (
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
)
type App struct {
confFile string
whatever []byte
}
func (a *App) Load() error {
p, err := ioutil.ReadFile(a.confFile)
if err != nil {
return err
}
a.whatever = p
return nil
}
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(a.whatever)
}
type Loader interface {
Load() error
}
func Reload(l Loader, sig ...os.Signal) error {
if err := l.Load(); err != nil {
return err
}
c := make(chan os.Signal, 1)
signal.Notify(c, sig...)
go func() {
for range c {
if err := l.Load(); err != nil {
panic(err)
}
}
}()
}
func main() {
app := &App{confFile: "config.txt"}
Reload(app, syscall.SIGHUP) // reload configuration on SIGHUP
http.Handle("/", app)
http.ListenAndServe("localhost:8080", nil)
}
Исходная версия beastie, :
Как-то так:
package main
import (
"io/ioutil"
"net/http"
"os"
"os/signal"
"syscall"
)
type App struct {
confFile string
whatever []byte
}
func (a *App) Load() error {
p, err := ioutil.ReadFile(a.confFile)
if err != nil {
return err
}
a.whatever = p
return nil
}
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(a.whatever)
}
type Loader interface {
Load() error
}
func Reload(l Loader, sig ...os.Signal) {
l.Load()
c := make(chan os.Signal, 1)
signal.Notify(c, sig...)
go func() {
for range c {
if err := l.Load(); err != nil {
panic(err)
}
}
}()
}
func main() {
app := &App{confFile: "config.txt"}
Reload(app, syscall.SIGHUP) // reload configuration on SIGHUP
http.Handle("/", app)
http.ListenAndServe("localhost:8080", nil)
}