config watcher, restart server on change

This commit is contained in:
0ceanSlim 2024-08-09 16:22:27 -04:00
parent d81d78e25f
commit 22729612ff
4 changed files with 118 additions and 28 deletions

2
go.mod
View File

@ -12,6 +12,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/kr/pretty v0.3.1 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
@ -31,4 +32,5 @@ require (
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0
gopkg.in/fsnotify.v1 v1.4.7
)

4
go.sum
View File

@ -21,6 +21,7 @@ github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
@ -82,6 +83,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -99,6 +102,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

40
main.go
View File

@ -11,6 +11,10 @@ import (
"grain/server/utils"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"golang.org/x/net/websocket"
@ -20,6 +24,15 @@ func main() {
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml")
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
restartChan := make(chan struct{})
go utils.WatchConfigFile("config.yml", restartChan)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
for {
wg.Add(1)
cfg, err := config.LoadConfig("config.yml")
if err != nil {
log.Fatal("Error loading config: ", err)
@ -29,7 +42,6 @@ func main() {
if err != nil {
log.Fatal("Error initializing database: ", err)
}
defer db.DisconnectDB(client)
config.SetupRateLimiter(cfg)
config.SetupSizeLimiter(cfg)
@ -40,8 +52,23 @@ func main() {
}
mux := setupRoutes()
server := startServer(cfg, mux, &wg)
startServer(cfg, mux)
select {
case <-restartChan:
log.Println("Restarting server...")
server.Close()
db.DisconnectDB(client)
wg.Wait() // Wait for the server to fully shut down before restarting
time.Sleep(3 * time.Second) // Add a delay before restarting
case <-signalChan:
log.Println("Shutting down server...")
server.Close()
db.DisconnectDB(client)
wg.Wait() // Wait for the server to fully shut down before exiting
return
}
}
}
func setupRoutes() *http.ServeMux {
@ -56,7 +83,7 @@ func setupRoutes() *http.ServeMux {
return mux
}
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux, wg *sync.WaitGroup) *http.Server {
server := &http.Server{
Addr: config.Server.Port,
Handler: mux,
@ -64,11 +91,16 @@ func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second,
IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second,
}
go func() {
defer wg.Done() // Notify that the server is done shutting down
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
err := server.ListenAndServe()
if err != nil {
if err != nil && err != http.ErrServerClosed {
fmt.Println("Error starting server:", err)
}
}()
return server
}
var wsServer = &websocket.Server{

View File

@ -0,0 +1,52 @@
package utils
import (
"fmt"
"log"
"time"
"gopkg.in/fsnotify.v1"
)
func WatchConfigFile(filePath string, restartChan chan<- struct{}) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("Error creating file watcher:", err)
}
defer watcher.Close()
err = watcher.Add(filePath)
if err != nil {
log.Fatal("Error adding file to watcher:", err)
}
var debounceTimer *time.Timer
debounceDuration := 1 * time.Second // Adjust this duration as needed
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
if debounceTimer != nil {
debounceTimer.Stop() // Cancel the previous timer
}
debounceTimer = time.AfterFunc(debounceDuration, func() {
fmt.Println("Config file modified, restarting server...")
select {
case restartChan <- struct{}{}:
default:
// Skip sending restart signal if there's already one in the channel
}
})
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error watching file:", err)
}
}
}