-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlr.go
33 lines (25 loc) · 739 Bytes
/
handlr.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
package handlr
import (
"fmt"
"net/http"
)
// Allows end user to create an instance of Handlr
func New() *Handlr {
return &Handlr{http.NewServeMux(), Router{path: "/"}}
}
// It houses the main Router as well as the mux instances.
type Handlr struct {
mux *http.ServeMux
router Router
}
// Registers Routers and ListenAndServer over Handlr.mux
func (h *Handlr) Start(portNumber int) error {
h.mux.Handle("/", &h.router) // Binds mux to routes
return h.listenAndServe(portNumber)
}
// ListenAndServer over Handlr.mux
func (h *Handlr) listenAndServe(portNumber int) error {
portString := fmt.Sprintf(":%d", portNumber)
fmt.Printf("> Server started on port %s", portString)
return http.ListenAndServe(portString, h.mux)
}