1
1
package common
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"io"
7
6
"log"
@@ -15,25 +14,30 @@ func Setup(
15
14
part1 Solution ,
16
15
part2 Solution ,
17
16
) {
18
- http .HandleFunc ("/1" , createHandler (day , 1 , part1 ))
19
- http .HandleFunc ("/2" , createHandler (day , 2 , part2 ))
17
+ http .HandleFunc ("/1" , createSolutionHandler (day , 1 , part1 ))
18
+ http .HandleFunc ("/2" , createSolutionHandler (day , 2 , part2 ))
19
+ http .HandleFunc ("/health" , healthCheckHandler )
20
+ http .HandleFunc ("/health/live" , healthCheckHandler )
21
+ http .HandleFunc ("/health/ready" , healthCheckHandler )
22
+
20
23
fmt .Printf ("Starting Day #%d service on port 3000\n " , day )
21
24
if err := http .ListenAndServe (":3000" , nil ); err != nil {
22
25
log .Fatal (err )
26
+
23
27
}
24
28
}
25
29
26
- func createHandler (
30
+ func createSolutionHandler (
27
31
day int ,
28
32
part int ,
29
33
solution func (string ) string ,
30
34
) func (http.ResponseWriter , * http.Request ) {
31
35
if solution == nil {
32
- solution = defaultHandler (day , part )
36
+ solution = defaultSolutionHandler (day , part )
33
37
}
34
38
35
39
return func (w http.ResponseWriter , r * http.Request ) {
36
- if err := methodMustBePost ( w , r ); err != nil {
40
+ if err := whitelistMethods ([] string { "POST" }, w , r ); err != nil {
37
41
fmt .Print (err .Error ())
38
42
return
39
43
}
@@ -45,15 +49,29 @@ func createHandler(
45
49
}
46
50
47
51
result := solution (input )
48
- _ , err = w .Write ([]byte (result ))
49
- if err != nil {
52
+ if _ , err = w .Write ([]byte (result )); err != nil {
50
53
http .Error (w , "Error" , http .StatusInternalServerError )
51
54
return
52
55
}
53
56
}
54
57
}
55
58
56
- func defaultHandler (
59
+ func healthCheckHandler (
60
+ w http.ResponseWriter ,
61
+ r * http.Request ,
62
+ ) {
63
+ if err := whitelistMethods ([]string {"GET" , "POST" }, w , r ); err != nil {
64
+ fmt .Print (err .Error ())
65
+ return
66
+ }
67
+
68
+ if _ , err := w .Write ([]byte ("{ \" status\" : \" UP\" }" )); err != nil {
69
+ http .Error (w , "Error" , http .StatusInternalServerError )
70
+ return
71
+ }
72
+ }
73
+
74
+ func defaultSolutionHandler (
57
75
day int ,
58
76
part int ,
59
77
) Solution {
@@ -62,14 +80,23 @@ func defaultHandler(
62
80
}
63
81
}
64
82
65
- func methodMustBePost (w http.ResponseWriter , r * http.Request ) error {
66
- if r .Method != http .MethodPost {
67
- http .Error (w , "Only POST method is allowed" , http .StatusMethodNotAllowed )
68
- msg := fmt .Sprintf ("Expected method to be POST, but was %s\n " , r .Method )
69
- return errors .New (msg )
83
+ func whitelistMethods (
84
+ methods []string ,
85
+ w http.ResponseWriter ,
86
+ r * http.Request ,
87
+ ) error {
88
+ for _ , method := range methods {
89
+ if r .Method == method {
90
+ return nil
91
+ }
70
92
}
71
93
72
- return nil
94
+ http .Error (
95
+ w ,
96
+ fmt .Sprintf ("%s is not in allowed methods: %q" , r .Method , methods ),
97
+ http .StatusMethodNotAllowed ,
98
+ )
99
+ return fmt .Errorf ("expected method to be one of %q, but was %s" , methods , r .Method )
73
100
}
74
101
75
102
func readInput (r * http.Request ) (string , error ) {
0 commit comments