-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
48 lines (41 loc) · 802 Bytes
/
api.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
package main
import (
"fmt"
"net/http"
"time"
)
type Api struct {
Host string
Port string
server *http.Server
multiplexer *http.ServeMux
middleware MiddlewareChain
}
func (api *Api) Init() error {
var err error
// init middleware
err = api.InitMiddleware()
if err != nil {
return err
}
// init handlers
err = api.InitMultiplexer()
if err != nil {
return err
}
// init handlers
api.server = &http.Server{
Addr: api.Host + ":" + api.Port,
Handler: api.multiplexer,
}
return nil
}
// starts the server with listenAndServe
func (api *Api) Start() error {
return api.server.ListenAndServe()
}
func (api *Api) Log(log string) {
timestamp := time.Now().Local().Format("2006-01-02 15:04:05")
prefix := timestamp + ": "
fmt.Println(prefix + log)
}