Skip to content

Commit df17abe

Browse files
authored
Merge pull request #5 from friendsofgo/playing_with_context
Playing with context
2 parents c1cc09b + ab5b55c commit df17abe

File tree

22 files changed

+376
-139
lines changed

22 files changed

+376
-139
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)/*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The Gopher API, is a simple CRUD API for formative purpose, we're building it wh
88
**Install**
99

1010
```sh
11-
$ GO111MODULE=off go get -u github.com/friendsofgo/gopherapi/cmd/gopherapi
11+
$ go get -u github.com/friendsofgo/gopherapi/cmd/gopherapi
1212
```
1313

1414
**Usage**

cmd/gopherapi/main.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,60 @@ 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/log/logrus"
16+
"github.com/friendsofgo/gopherapi/pkg/modifying"
17+
"github.com/friendsofgo/gopherapi/pkg/removing"
18+
"github.com/friendsofgo/gopherapi/pkg/server"
19+
"github.com/friendsofgo/gopherapi/pkg/storage/inmem"
20+
21+
_ "github.com/joho/godotenv/autoload"
1222
)
1323

1424
func main() {
25+
26+
var (
27+
hostName, _ = os.Hostname()
28+
defaultServerID = fmt.Sprintf("%s-%s", os.Getenv("GOPHERAPI_NAME"), hostName)
29+
defaultHost = os.Getenv("GOPHERAPI_SERVER_HOST")
30+
defaultPort, _ = strconv.Atoi(os.Getenv("GOPHERAPI_SERVER_PORT"))
31+
)
32+
33+
host := flag.String("host", defaultHost, "define host of the server")
34+
port := flag.Int("port", defaultPort, "define port of the server")
35+
serverID := flag.String("server-id", defaultServerID, "define server identifier")
1536
withData := flag.Bool("withData", false, "initialize the api with some gophers")
1637
flag.Parse()
1738

1839
var gophers map[string]gopher.Gopher
1940
if *withData {
2041
gophers = sample.Gophers
2142
}
22-
s := container.InitializeServer(gophers)
2343

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

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
6+
github.com/sirupsen/logrus v1.4.2
67
)

go.sum

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
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=
1+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
42
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
53
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
4+
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
5+
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
6+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
67
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=
8+
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
9+
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
10+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
12+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
1413
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=

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
}

0 commit comments

Comments
 (0)