I serve HTTP over gin's https://github.com/fvbock/endless. I would like to see the differences from the basic HTTP server.
I've sent syscall.SIGUSR1
signal with:
syscall.Kill(getPid(), syscall.SIGUSR1)
The app doesn't exit, but I cannot detect the restart.
What I have to do is initialise new configurations to the app when the toml config file changes.
My code is as follows:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func init() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
router := gin.New()
router.GET("/restart", restart)
if err := endless.ListenAndServe("localhost:7777", router); err != nil {
panic(err)
}
}
When I hit the restart endpoint, I want the toml config printed out.
Updating answer based on the changes to your question. The endless library can allow you to handle that signal by default. You will need to register a hook. I've expanded on your example code below:
package main
import (
"os"
"fmt"
"syscall"
"github.com/gin-gonic/gin"
"github.com/fvbock/endless"
"github.com/BurntSushi/toml"
)
type Config struct {
Age int
Cats []string
}
var cfg Config
func restart(c *gin.Context) {
syscall.Kill(os.Getpid(), syscall.SIGUSR1)
}
func readConfig() {
toml.DecodeFile("config.toml", &cfg)
fmt.Println("Testing", cfg)
}
func main() {
readConfig()
router := gin.New()
router.GET("/restart", restart)
srv := endless.NewServer("localhost:7777", router)
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1] = append(
srv.SignalHooks[endless.PRE_SIGNAL][syscall.SIGUSR1],
readConfig)
if err := srv.ListenAndServe(); err != nil {
panic(err)
}
}
Now when you call the restart endpoint, you should see the changes to config file refelcted in stdout. However in order to watch the file for changes you would need to use something like fsnotify