-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (44 loc) · 1.04 KB
/
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
54
package main
import (
"fmt"
"net/http"
"os"
"strings"
app "github.com/rgrs-x/service/api/app"
)
// Color message for port binding
const (
printColor = "\033[38;5;%dm%s\033[39;49m\n"
errColor = 9
warningColor = 3
developColor = 10
)
func main() {
router := app.SetupRoutes()
port := os.Getenv("PORT")
if port == "" {
port = "8000" //localhost
}
fmt.Printf(printColor, warningColor, port)
//@ Launch the app, visit localhost:8000/api
err := http.ListenAndServe(":"+port, router)
// Handle to another port if binding error
if result := catchErrorBinding(err); result {
port = "8084"
fmt.Printf(printColor, developColor, "Changed to port: "+port)
err = http.ListenAndServe(":"+port, router)
if err != nil {
fmt.Printf(printColor, errColor, err)
}
} else {
fmt.Printf(printColor, warningColor, err)
}
}
// For handling second port
func catchErrorBinding(err error) bool {
if strings.Contains(err.Error(), "bind: address already in use") {
fmt.Printf(printColor, errColor, err)
return true
}
return false
}