Skip to content

Commit

Permalink
feat(gnodev): add /reload and /reset endpoints (gnolang#2448)
Browse files Browse the repository at this point in the history
  • Loading branch information
n0izn0iz authored and gfanton committed Jul 23, 2024
1 parent 7f063a0 commit 09d5f52
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type devCfg struct {
maxGas int64
chainId string
serverMode bool
unsafeAPI bool
}

var defaultDevOptions = &devCfg{
Expand Down Expand Up @@ -209,6 +210,13 @@ func (c *devCfg) RegisterFlags(fs *flag.FlagSet) {
defaultDevOptions.maxGas,
"set the maximum gas per block",
)

fs.BoolVar(
&c.unsafeAPI,
"unsafe-api",
defaultDevOptions.unsafeAPI,
"enable /reset and /reload endpoints which are not safe to expose publicly",
)
}

func execDev(cfg *devCfg, args []string, io commands.IO) (err error) {
Expand Down Expand Up @@ -274,6 +282,23 @@ func execDev(cfg *devCfg, args []string, io commands.IO) (err error) {
// Setup gnoweb
webhandler := setupGnoWebServer(logger.WithGroup(WebLogName), cfg, devNode)

// Setup unsafe APIs if enabled
if cfg.unsafeAPI {
mux.HandleFunc("/reset", func(res http.ResponseWriter, req *http.Request) {
if err := devNode.Reset(req.Context()); err != nil {
logger.Error("failed to reset", slog.Any("err", err))
res.WriteHeader(http.StatusInternalServerError)
}
})

mux.HandleFunc("/reload", func(res http.ResponseWriter, req *http.Request) {
if err := devNode.Reload(req.Context()); err != nil {
logger.Error("failed to reload", slog.Any("err", err))
res.WriteHeader(http.StatusInternalServerError)
}
})
}

// Setup HotReload if needed
if !cfg.noWatch {
evtstarget := fmt.Sprintf("%s/_events", server.Addr)
Expand Down

0 comments on commit 09d5f52

Please sign in to comment.