Skip to content

Commit fab21f0

Browse files
author
Onur Menal
committed
Transport layer
1 parent db6e5a6 commit fab21f0

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

cmd/server/main.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/onurmenal/go-rest-api/internal/comment"
88
"github.com/onurmenal/go-rest-api/internal/db"
9+
transportHttp "github.com/onurmenal/go-rest-api/internal/transport/http"
910
)
1011

1112
func Run() error {
@@ -28,13 +29,10 @@ func Run() error {
2829

2930
cmtService := comment.NewService(db)
3031

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+
}
3836

3937
return nil
4038
}
File renamed without changes.

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/golang-migrate/migrate/v4 v4.15.2 // indirect
77
github.com/google/go-github/v35 v35.2.0 // indirect
8+
github.com/gorilla/mux v1.8.0 // indirect
89
github.com/jmoiron/sqlx v1.3.5 // indirect
910
github.com/lib/pq v1.10.6 // indirect
1011
github.com/satori/go.uuid v1.2.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,8 @@ github.com/gorilla/handlers v1.4.2/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/
587587
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
588588
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
589589
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=
590592
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
591593
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
592594
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=

internal/transport/http/handler.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

0 commit comments

Comments
 (0)