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

3

22

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?

Phenocryst answered 29/1, 2014 at 0:27 Comment(1)
Are you talking source file (compiled) changes or resources? For the former you'll need an external process to watch your source since a recompile is needed. For the latter, Martini seems to serve the latest, at least in development mode.Woolley
T
15

Maybe you need gin ?

Made by the creator of Martini.

Talya answered 30/1, 2014 at 11:36 Comment(4)
Wonderfully simple. Thank you :)Phenocryst
Gin starts the server in one port, but I'm using the mux router. What is conflicting ports.Rotator
Gin is not maintained anymore, as you can read in various issues on Github, like here: github.com/codegangsta/gin/issues/90 "This project is abandoned."Scheldt
Gin is by no means abandoned and its development is ongoing. The have regularly commitsPunk
C
14

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.

Careerist answered 29/1, 2014 at 8:46 Comment(3)
By far the most user-friendly package I've tried to date. Great recommendation!Valenevalenka
This will make go development fun!Hime
Fresh is now unmaintainedActinopod
H
12

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
Heed answered 29/1, 2014 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.