File tree 2 files changed +37
-12
lines changed
2 files changed +37
-12
lines changed Original file line number Diff line number Diff line change
1
+ package handlers
2
+
3
+ import (
4
+ "fmt"
5
+ "io/ioutil"
6
+ "log"
7
+ "net/http"
8
+ )
9
+
10
+ // Hello handler
11
+ type Hello struct {
12
+ l * log.Logger
13
+ }
14
+
15
+ // NewHello Function which gives reference to Hello handler
16
+ func NewHello (l * log.Logger ) * Hello {
17
+ return & Hello {l }
18
+ }
19
+
20
+ func (h * Hello ) ServeHTTP (rw http.ResponseWriter , r * http.Request ) {
21
+ h .l .Println ("Hello world" )
22
+ d , err := ioutil .ReadAll (r .Body )
23
+
24
+ if err != nil {
25
+ http .Error (rw , "Oops" , http .StatusBadRequest )
26
+ return
27
+ }
28
+ fmt .Fprintf (rw , "Hello %s" , d )
29
+ }
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "fmt"
5
- "io/ioutil"
6
4
"log"
7
5
"net/http"
6
+ "os"
7
+
8
+ "github.com/nandangrover/go-microservices/handlers"
8
9
)
9
10
10
11
func main () {
11
- http .HandleFunc ("/" , func (rw http.ResponseWriter , r * http.Request ) {
12
- log .Println ("Hello world" )
13
- d , err := ioutil .ReadAll (r .Body )
12
+ l := log .New (os .Stdout , "product-api" , log .LstdFlags )
13
+ hh := handlers .NewHello (l )
14
14
15
- if err != nil {
16
- http .Error (rw , "Oops" , http .StatusBadRequest )
17
- return
18
- }
19
- fmt .Fprintf (rw , "Hello %s" , d )
20
- })
15
+ sm := http .NewServeMux ()
16
+ sm .Handle ("/" , hh )
21
17
22
- http .ListenAndServe (":8080 " , nil )
18
+ http .ListenAndServe (":9090 " , sm )
23
19
}
You can’t perform that action at this time.
0 commit comments