-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from minazukie/feature/hot-restart
Add hot restart
- Loading branch information
Showing
11 changed files
with
143 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/GoSome/fileUpdater/pkg/core" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/gin-gonic/gin" | ||
"gopkg.in/yaml.v3" | ||
"log" | ||
"os" | ||
"path" | ||
) | ||
|
||
var Path string | ||
var Config core.ServerConfigs | ||
var DaemonZ bool | ||
var PidPath string | ||
var LogFile string | ||
|
||
func Parse(init bool) { | ||
logFunc := log.Printf | ||
if init { | ||
logFunc = log.Fatalf | ||
} | ||
if _, err := os.Stat(Path); os.IsNotExist(err) { | ||
logFunc("config file \"%s\" not exist", Path) | ||
return | ||
} | ||
|
||
configFile, err := os.Open(Path) | ||
if err != nil { | ||
logFunc("open config file \"%s\" failed", Path) | ||
return | ||
} | ||
defer configFile.Close() | ||
|
||
switch path.Ext(Path) { | ||
case ".json": | ||
err = json.NewDecoder(configFile).Decode(&Config) | ||
if err != nil { | ||
logFunc("%s", err) | ||
} | ||
case ".yml": | ||
fallthrough | ||
case ".yaml": | ||
err := yaml.NewDecoder(configFile).Decode(&Config) | ||
if err != nil { | ||
logFunc("%s", err) | ||
} | ||
default: | ||
logFunc("config file path must end with .json or .yaml") | ||
} | ||
} | ||
|
||
// Inject injects config into gin's context. | ||
func Inject(c *gin.Context) { | ||
c.Set("cfg", Config) | ||
c.Next() | ||
} | ||
|
||
// Watch watches config file, configs will be reloaded when config file is changed. | ||
func Watch() { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer watcher.Close() | ||
|
||
watcher.Add(Path) | ||
|
||
for { | ||
select { | ||
case ev := <-watcher.Events: | ||
if ev.Op == fsnotify.Write { | ||
log.Println("config file has been changed, attempt to reload...") | ||
Parse(false) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package listeners | ||
|
||
import ( | ||
"github.com/GoSome/fileUpdater/pkg/config" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
) | ||
|
||
func ListenSIGUSR2() { | ||
s := make(chan os.Signal, 1) | ||
signal.Notify(s, syscall.SIGUSR2) | ||
|
||
go func() { | ||
for { | ||
<-s | ||
log.Println("config file has been changed, attempt to reload...") | ||
config.Parse(false) | ||
} | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters