1
1
package main
2
2
3
3
import (
4
- "encoding/json "
4
+ "errors "
5
5
"fmt"
6
+ "math"
6
7
"net/http"
7
8
"time"
8
9
@@ -17,34 +18,38 @@ type Server struct {
17
18
}
18
19
19
20
//NewServer is to create httpserver
20
- func NewServer (config * ServerConfig ) * Server {
21
+ func NewServer (config * ServerConfig ) ( * Server , error ) {
21
22
// todo: Create HttppServer
22
23
23
24
router := mux .NewRouter ()
24
25
registerRouters (router )
25
- router .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
26
- // an example API handler
27
- json .NewEncoder (w ).Encode (map [string ]bool {"ok" : true })
28
- })
26
+
27
+ //Check for Int64 Overflow
28
+ if math .MaxInt64 / time .Second < config .WriteTimeout {
29
+ return nil , errors .New ("WriteTimeout overflows int64" )
30
+ }
31
+
32
+ if math .MaxInt64 / time .Second < config .ReadTimeout {
33
+ return nil , errors .New ("ReadTimeout overflows int64" )
34
+ }
29
35
30
36
server := & Server {
31
37
config : config ,
32
38
router : router ,
39
+ httpServer : & http.Server {
40
+ Handler : router ,
41
+ Addr : config .Address ,
42
+ WriteTimeout : config .WriteTimeout * time .Second ,
43
+ ReadTimeout : config .ReadTimeout * time .Second ,
44
+ },
33
45
}
34
46
35
- server .httpServer = & http.Server {
36
- Handler : router ,
37
- Addr : config .Address ,
38
- WriteTimeout : config .WriteTimeout * time .Second ,
39
- ReadTimeout : config .ReadTimeout * time .Second ,
40
- }
41
-
42
- return server
47
+ return server , nil
43
48
}
44
49
45
50
func (s * Server ) start () error {
46
51
// todo: Start and Listen HttpServer
47
- fmt .Printf ("Listening on %s \n " , s .httpServer .Addr )
52
+ fmt .Printf ("Listening on http:// %s \n " , s .httpServer .Addr )
48
53
return s .httpServer .ListenAndServe ()
49
54
}
50
55
0 commit comments