Skip to content

Commit c1cc09b

Browse files
committed
Add wire
1 parent 881b266 commit c1cc09b

File tree

6 files changed

+73
-20
lines changed

6 files changed

+73
-20
lines changed

cmd/gopherapi/main.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,9 @@ import (
66
"log"
77
"net/http"
88

9-
"github.com/friendsofgo/gopherapi/pkg/removing"
10-
11-
"github.com/friendsofgo/gopherapi/pkg/modifying"
12-
13-
"github.com/friendsofgo/gopherapi/pkg/adding"
14-
"github.com/friendsofgo/gopherapi/pkg/fetching"
15-
16-
"github.com/friendsofgo/gopherapi/pkg/storage/inmem"
17-
189
sample "github.com/friendsofgo/gopherapi/cmd/sample-data"
10+
"github.com/friendsofgo/gopherapi/internal/container"
1911
gopher "github.com/friendsofgo/gopherapi/pkg"
20-
21-
"github.com/friendsofgo/gopherapi/pkg/server"
2212
)
2313

2414
func main() {
@@ -29,14 +19,7 @@ func main() {
2919
if *withData {
3020
gophers = sample.Gophers
3121
}
32-
33-
repo := inmem.NewRepository(gophers)
34-
fS := fetching.NewService(repo)
35-
aS := adding.NewService(repo)
36-
mS := modifying.NewService(repo)
37-
rS := removing.NewService(repo)
38-
39-
s := server.New(fS, aS, mS, rS)
22+
s := container.InitializeServer(gophers)
4023

4124
fmt.Println("The gopher server is on tap now: http://localhost:8080")
4225
log.Fatal(http.ListenAndServe(":8080", s.Router()))

go.mod

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

3-
require github.com/gorilla/mux v1.7.0
3+
require (
4+
github.com/google/wire v0.2.2
5+
github.com/gorilla/mux v1.7.0
6+
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
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=
14
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
25
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=

internal/container/wire.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//+build wireinject
2+
3+
package container
4+
5+
import (
6+
gopher "github.com/friendsofgo/gopherapi/pkg"
7+
"github.com/friendsofgo/gopherapi/pkg/adding"
8+
"github.com/friendsofgo/gopherapi/pkg/fetching"
9+
"github.com/friendsofgo/gopherapi/pkg/modifying"
10+
"github.com/friendsofgo/gopherapi/pkg/removing"
11+
"github.com/friendsofgo/gopherapi/pkg/server"
12+
"github.com/friendsofgo/gopherapi/pkg/storage/inmem"
13+
"github.com/google/wire"
14+
)
15+
16+
func InitializeServer(gophers map[string]gopher.Gopher) server.Server {
17+
wire.Build(server.New, inmem.NewRepository, fetching.NewService, adding.NewService, modifying.NewService, removing.NewService)
18+
return server.NewWire()
19+
}

internal/container/wire_gen.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/server/api.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/gorilla/mux"
1313
)
1414

15+
// api all server necessary dependencies
1516
type api struct {
1617
router http.Handler
1718
fetching fetching.Service
@@ -45,6 +46,11 @@ func New(fS fetching.Service, aS adding.Service, mS modifying.Service, rS removi
4546
return a
4647
}
4748

49+
// NewWire empty initialization for using with wire
50+
func NewWire() Server {
51+
return &api{}
52+
}
53+
4854
func (a *api) Router() http.Handler {
4955
return a.router
5056
}

0 commit comments

Comments
 (0)