-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (46 loc) · 964 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"flag"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"github.com/gin-gonic/gin"
)
var port string
func init() {
gin.SetMode(gin.ReleaseMode)
if runtime.GOOS == "windows" {
gin.DisableConsoleColor()
}
gracefulExit()
flag.StringVar(&port, "port", "27281", "port to listen on")
log.SetPrefix("[SRV] ")
log.SetFlags(log.LstdFlags)
}
// checkError
func ce(err error, msg string) {
if err != nil {
log.Panicln(msg, err)
}
}
func gracefulExit() {
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
os.Exit(0)
}()
}
func main() {
flag.Parse()
r := gin.Default()
r.Use(gin.Recovery())
r.StaticFS("/", http.Dir("./public"))
hostname, err := os.Hostname()
ce(err, "os.Hostname")
log.Printf("Listening on http://%[1]s:%[2]s/ , http://localhost:%[2]s/\n", hostname, port)
err = r.Run(":" + port)
ce(err, "http.ListenAndServe")
}