File tree 5 files changed +70
-7
lines changed
5 files changed +70
-7
lines changed Original file line number Diff line number Diff line change 6
6
7
7
"github.com/onurmenal/go-rest-api/internal/comment"
8
8
"github.com/onurmenal/go-rest-api/internal/db"
9
+ transportHttp "github.com/onurmenal/go-rest-api/internal/transport/http"
9
10
)
10
11
11
12
func Run () error {
@@ -28,13 +29,10 @@ func Run() error {
28
29
29
30
cmtService := comment .NewService (db )
30
31
31
- fmt .Println (cmtService .PostComment (context .Background (), comment.Comment {
32
- Slug : "testslug" ,
33
- Body : "testbody" ,
34
- Author : "onur menal" ,
35
- }))
36
-
37
- fmt .Println ("Successfully connected and pinged database" )
32
+ httpHandler := transportHttp .NewHandler (cmtService )
33
+ if err := httpHandler .Serve (); err != nil {
34
+ return err
35
+ }
38
36
39
37
return nil
40
38
}
File renamed without changes.
Original file line number Diff line number Diff line change 5
5
require (
6
6
github.com/golang-migrate/migrate/v4 v4.15.2 // indirect
7
7
github.com/google/go-github/v35 v35.2.0 // indirect
8
+ github.com/gorilla/mux v1.8.0 // indirect
8
9
github.com/jmoiron/sqlx v1.3.5 // indirect
9
10
github.com/lib/pq v1.10.6 // indirect
10
11
github.com/satori/go.uuid v1.2.0 // indirect
Original file line number Diff line number Diff line change @@ -587,6 +587,8 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
587
587
github.com/gorilla/mux v1.7.2 /go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs =
588
588
github.com/gorilla/mux v1.7.3 /go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs =
589
589
github.com/gorilla/mux v1.7.4 /go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So =
590
+ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI =
591
+ github.com/gorilla/mux v1.8.0 /go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So =
590
592
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c /go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ =
591
593
github.com/gorilla/websocket v1.4.0 /go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ =
592
594
github.com/gorilla/websocket v1.4.2 /go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE =
Original file line number Diff line number Diff line change
1
+ package http
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "log"
7
+ "net/http"
8
+ "os"
9
+ "os/signal"
10
+ "time"
11
+
12
+ "github.com/gorilla/mux"
13
+ )
14
+
15
+ type CommentService interface {}
16
+
17
+ type Handler struct {
18
+ Router * mux.Router
19
+ Service CommentService
20
+ Server * http.Server
21
+ }
22
+
23
+ func NewHandler (service CommentService ) * Handler {
24
+ h := & Handler {
25
+ Service : service ,
26
+ }
27
+ h .Router = mux .NewRouter ()
28
+ h .mapRoutes ()
29
+
30
+ h .Server = & http.Server {
31
+ Addr : "0.0.0.0:8080" ,
32
+ Handler : h .Router ,
33
+ }
34
+
35
+ return h
36
+ }
37
+
38
+ func (h * Handler ) mapRoutes () {
39
+ h .Router .HandleFunc ("/hello" , func (w http.ResponseWriter , r * http.Request ) {
40
+ fmt .Fprintf (w , "Hello World" )
41
+ })
42
+ }
43
+
44
+ func (h * Handler ) Serve () error {
45
+ go func () {
46
+ if err := h .Server .ListenAndServe (); err != nil {
47
+ log .Println (err .Error ())
48
+ }
49
+ }()
50
+
51
+ c := make (chan os.Signal , 1 )
52
+ signal .Notify (c , os .Interrupt )
53
+ <- c
54
+
55
+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
56
+ defer cancel ()
57
+ h .Server .Shutdown (ctx )
58
+
59
+ log .Println ("shutting down" )
60
+
61
+ return nil
62
+ }
You can’t perform that action at this time.
0 commit comments