像Django一样,在文件更改时重新启动Go的net/http服务器

huangapple go评论84阅读模式
英文:

Restart Go's net/http server on file-change like Django

问题

我正在尝试使用Martini,它在Go的基本net/http包上添加了一些很好的功能。

我想知道,像Django一样,我如何让服务器在源文件更改时自动重启?我知道如何监视文件,但我不确定如何在同一进程中重新触发服务器。

我猜想要再次触发http.ListenAndServe,但我觉得这可能与已经运行的实例不兼容。

我需要生成一个子进程/守护进程来使其工作吗?

英文:

I'm trying out Martini, which adds some nice functionality upon Go's basic net/http package.

I was wondering tho. How can I, like Django does too, let the server restart itself on source-file changes? I know how to monitor a file, but I'm not sure how to retrigger the Server within the same process.

I'm guessing to trigger http.ListenAndServe again, but I have a feeling it doesn't go well with instance already running.

Do I need to spawn a subprocess/daemon to get this working?

答案1

得分: 14

也许你需要 gin

由 Martini 的创建者制作。

英文:

Maybe you need gin ?

Made by the creator of Martini.

答案2

得分: 13

你可以试试看

Fresh 是一个命令行工具,每当你保存一个 Go 或模板文件时,它会构建并重新启动你的 Web 应用程序。

英文:

You may give it a try

Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file.

答案3

得分: 11

你需要使用一个可以监视目录/文件并运行命令的外部工具。我建议使用reflex,它本身就是用Go编写的。

(修改README中的示例):

# 每当.go文件发生更改时重新运行make命令
reflex -r '\.go$' ./mymartiniapp
英文:

You'll need to use an external tool that can watch a directory/files and run a command. I'd recommend reflex which is written in Go itself:

(modifying the example in the README):

# Rerun make whenever a .go file changes
reflex -r '\.go$' ./mymartiniapp

huangapple
  • 本文由 发表于 2014年1月29日 08:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/21419771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定