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

12.8k views Asked by At

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?

3

There are 3 answers

4
Adrian On BEST ANSWER

Maybe you need gin ?

Made by the creator of Martini.

0
elithrar On

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
3
Brandon Chen On

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.