Handling failure when starting server and fetching port from command line arguments.
parent
b8eaa40100
commit
b1bf2e6a1a
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StandardResponse struct {
|
type StandardResponse struct {
|
||||||
|
@ -95,7 +96,19 @@ func handleDefault(writer http.ResponseWriter, _ *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Starting new server...")
|
args := os.Args
|
||||||
|
|
||||||
|
var port = 8080
|
||||||
|
if len(args) >= 2 {
|
||||||
|
p, err := strconv.Atoi(args[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("there was a conversion error for port number %v", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
port = p
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Starting new server on port %v...", port)
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
|
||||||
r.HandleFunc("/", handleIndex)
|
r.HandleFunc("/", handleIndex)
|
||||||
|
@ -103,9 +116,11 @@ func main() {
|
||||||
r.NotFoundHandler = http.HandlerFunc(handleDefault)
|
r.NotFoundHandler = http.HandlerFunc(handleDefault)
|
||||||
|
|
||||||
server := http.Server{
|
server := http.Server{
|
||||||
Addr: ":8080",
|
Addr: fmt.Sprintf(":%d", port),
|
||||||
Handler: r,
|
Handler: r,
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = server.ListenAndServe()
|
if err := server.ListenAndServe(); err != nil {
|
||||||
|
panic(fmt.Errorf("got error while starting server %v", err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue