Skip to content

Commit 80dfe1a

Browse files
committed
uses gin to auto restart application in development
1 parent 9077c64 commit 80dfe1a

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gin-bin

Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ WORKDIR /go/src/github.com/maxcnunes/monitor-api
44

55
ADD . /go/src/github.com/maxcnunes/monitor-api
66

7-
RUN go get -d ./...
7+
RUN go get -d ./...
8+
9+
RUN go get github.com/codegangsta/gin

fig.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mongo:
33

44
local:
55
build: .
6-
command: go run main.go
6+
command: gin
77
ports:
88
- 3000
99
volumes:
@@ -13,5 +13,6 @@ local:
1313
MONGODB_DATABASE: monitor-api
1414
VIRTUAL_PORT: 3000
1515
VIRTUAL_HOST: monitor-service.local.bravi.com.br
16+
PORT_BEHIND_PROXY: 3001
1617
links:
1718
- mongo:mongo

main.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
package main
22

33
import (
4-
"flag"
54
"github.com/maxcnunes/monitor-api/monitor"
65
"github.com/maxcnunes/monitor-api/server"
76
"log"
87
"net/http"
8+
"os"
99
)
1010

1111
var (
1212
db = monitor.DB{}
1313
data = monitor.DataMonitor{}
1414
router = server.Router{}
1515
websocket = server.Websocket{}
16-
addr = flag.String("addr", ":3000", "http service address")
1716
)
1817

1918
func main() {
20-
flag.Parse()
19+
var addr string
20+
if env := os.Getenv("PORT_BEHIND_PROXY"); env != "" {
21+
addr = ":" + env
22+
} else if env := os.Getenv("VIRTUAL_PORT"); env != "" {
23+
addr = ":" + env
24+
} else {
25+
addr = ":3000"
26+
}
2127

2228
db.Start()
2329
defer db.Close()
@@ -27,8 +33,8 @@ func main() {
2733
http.Handle("/", router.Start(&data))
2834
http.HandleFunc("/ws", websocket.Start(&data))
2935

30-
log.Printf("Server running on http://0.0.0.0:%d", *addr)
31-
if err := http.ListenAndServe(*addr, nil); err != nil {
36+
log.Printf("Server running on http://%s", addr)
37+
if err := http.ListenAndServe(addr, nil); err != nil {
3238
log.Fatal("ListenAndServe: ", err)
3339
}
3440
}

0 commit comments

Comments
 (0)