Skip to content

Commit 4d0bcee

Browse files
committed
Propagate request context
1 parent c1cc09b commit 4d0bcee

File tree

17 files changed

+209
-135
lines changed

17 files changed

+209
-135
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GOPHERAPI_NAME=GOPERAPI
2+
GOPHERAPI_SERVER_HOST=localhost
3+
GOPHERAPI_SERVER_PORT=3000

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin
2+
.vscode
3+
.idea

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Go parameters
2+
MAIN_PATH=cmd/gopherapi/main.go
3+
BINARY_NAME=$(BINARY_PATH)/server
4+
BINARY_PATH=bin
5+
6+
run:
7+
go build -o $(BINARY_NAME) -race $(MAIN_PATH)
8+
./$(BINARY_NAME)
9+
10+
test:
11+
go test -race -v -timeout=10s ./...
12+
13+
clean:
14+
go clean $(MAIN_PATH)
15+
rm -f $(BINARY_PATH)/*

cmd/gopherapi/main.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,58 @@ import (
55
"fmt"
66
"log"
77
"net/http"
8+
"os"
9+
"strconv"
810

9-
sample "github.com/friendsofgo/gopherapi/cmd/sample-data"
10-
"github.com/friendsofgo/gopherapi/internal/container"
11+
"github.com/friendsofgo/gopherapi/cmd/sample-data"
1112
gopher "github.com/friendsofgo/gopherapi/pkg"
13+
"github.com/friendsofgo/gopherapi/pkg/adding"
14+
"github.com/friendsofgo/gopherapi/pkg/fetching"
15+
"github.com/friendsofgo/gopherapi/pkg/modifying"
16+
"github.com/friendsofgo/gopherapi/pkg/removing"
17+
"github.com/friendsofgo/gopherapi/pkg/server"
18+
"github.com/friendsofgo/gopherapi/pkg/storage/inmem"
19+
20+
_ "github.com/joho/godotenv/autoload"
1221
)
1322

1423
func main() {
24+
25+
var (
26+
hostName, _ = os.Hostname()
27+
defaultServerName = fmt.Sprintf("%s-%s", os.Getenv("GOPHERAPI_NAME"), hostName)
28+
defaultHost = os.Getenv("GOPHERAPI_SERVER_HOST")
29+
defaultPort, _ = strconv.Atoi(os.Getenv("GOPHERAPI_SERVER_PORT"))
30+
)
31+
32+
host := flag.String("host", defaultHost, "define host of the server")
33+
port := flag.Int("port", defaultPort, "define port of the server")
34+
serverName := flag.String("server-name", defaultServerName, "define name of the server")
1535
withData := flag.Bool("withData", false, "initialize the api with some gophers")
1636
flag.Parse()
1737

1838
var gophers map[string]gopher.Gopher
1939
if *withData {
2040
gophers = sample.Gophers
2141
}
22-
s := container.InitializeServer(gophers)
2342

24-
fmt.Println("The gopher server is on tap now: http://localhost:8080")
25-
log.Fatal(http.ListenAndServe(":8080", s.Router()))
43+
repo := inmem.NewRepository(gophers)
44+
fetchingService := fetching.NewService(repo)
45+
addingService := adding.NewService(repo)
46+
modifyingService := modifying.NewService(repo)
47+
removingService := removing.NewService(repo)
48+
49+
httpAddr := fmt.Sprintf("%s:%d", *host, *port)
50+
51+
s := server.New(
52+
*serverName,
53+
httpAddr,
54+
fetchingService,
55+
addingService,
56+
modifyingService,
57+
removingService,
58+
)
59+
60+
fmt.Println("The gopher server is on tap now:", httpAddr)
61+
log.Fatal(http.ListenAndServe(httpAddr, s.Router()))
2662
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/friendsofgo/gopherapi
22

33
require (
4-
github.com/google/wire v0.2.2
54
github.com/gorilla/mux v1.7.0
5+
github.com/joho/godotenv v1.3.0
66
)

go.sum

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
2-
github.com/google/wire v0.2.2 h1:fSIRzE/K12IaNgV6X0173X/oLrTwHKRiMcFZhiDrN3s=
3-
github.com/google/wire v0.2.2/go.mod h1:7FHVg6mFpFQrjeUZrm+BaD50N5jnDKm50uVPTpyYOmU=
41
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
52
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
6-
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8-
golang.org/x/crypto v0.0.0-20190422183909-d864b10871cd/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
9-
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
10-
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
11-
golang.org/x/net v0.0.0-20190420063019-afa5a82059c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
12-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
13-
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
16-
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
3+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
4+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=

internal/container/wire.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

internal/container/wire_gen.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

pkg/adding/service.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package adding
22

33
import (
4+
"context"
5+
46
gopher "github.com/friendsofgo/gopherapi/pkg"
57
)
68

79
// Service provides adding operations.
810
type Service interface {
9-
AddGopher(ID, name, image string, age int) error
11+
AddGopher(ctx context.Context, ID, name, image string, age int) error
1012
}
1113

1214
type service struct {
@@ -19,7 +21,7 @@ func NewService(repository gopher.Repository) Service {
1921
}
2022

2123
// AddGopher adds the given gopher to storage
22-
func (s *service) AddGopher(ID, name, image string, age int) error {
24+
func (s *service) AddGopher(ctx context.Context, ID, name, image string, age int) error {
2325
g := gopher.New(ID, name, image, age)
24-
return s.repository.CreateGopher(g)
26+
return s.repository.CreateGopher(ctx, g)
2527
}

pkg/fetching/service.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package fetching
22

3-
import gopher "github.com/friendsofgo/gopherapi/pkg"
3+
import (
4+
"context"
5+
6+
gopher "github.com/friendsofgo/gopherapi/pkg"
7+
)
48

59
// Service provides fetching operations.
610
type Service interface {
7-
FetchGophers() ([]gopher.Gopher, error)
8-
FetchGopherByID(ID string) (*gopher.Gopher, error)
11+
FetchGophers(ctx context.Context) ([]gopher.Gopher, error)
12+
FetchGopherByID(ctx context.Context, ID string) (*gopher.Gopher, error)
913
}
1014

1115
type service struct {
@@ -18,11 +22,11 @@ func NewService(repository gopher.Repository) Service {
1822
}
1923

2024
// FetchGophers returns all gophers
21-
func (s *service) FetchGophers() ([]gopher.Gopher, error) {
22-
return s.repository.FetchGophers()
25+
func (s *service) FetchGophers(ctx context.Context) ([]gopher.Gopher, error) {
26+
return s.repository.FetchGophers(ctx)
2327
}
2428

2529
// FetchGopherByID returns a gopher
26-
func (s *service) FetchGopherByID(ID string) (*gopher.Gopher, error) {
27-
return s.repository.FetchGopherByID(ID)
30+
func (s *service) FetchGopherByID(ctx context.Context, ID string) (*gopher.Gopher, error) {
31+
return s.repository.FetchGopherByID(ctx, ID)
2832
}

0 commit comments

Comments
 (0)