11package server
22
33import (
4- "context"
54 "encoding/json"
65 "net/http"
76
@@ -15,8 +14,8 @@ import (
1514
1615// server all server necessary dependencies
1716type server struct {
18- serverName string
19- httpAddr string
17+ serverID string
18+ httpAddr string
2019
2120 router http.Handler
2221 fetching fetching.Service
@@ -37,33 +36,27 @@ type Server interface {
3736
3837// New initialize the server
3938func New (
40- serverName string ,
41- httpAddr string ,
39+ serverID string ,
4240 fS fetching.Service ,
4341 aS adding.Service ,
4442 mS modifying.Service ,
4543 rS removing.Service ) Server {
4644 a := & server {
47- serverName : serverName ,
48- httpAddr : httpAddr ,
49- fetching : fS ,
50- adding : aS ,
51- modifying : mS ,
52- removing : rS }
45+ serverID : serverID ,
46+ fetching : fS ,
47+ adding : aS ,
48+ modifying : mS ,
49+ removing : rS }
5350 router (a )
5451
5552 return a
5653}
5754
58- func (s * server ) createContext (ctx context.Context ) context.Context {
59- ctx = context .WithValue (ctx , contextKeyServerName , s .serverName )
60- ctx = context .WithValue (ctx , contextKeyHttpAddr , s .httpAddr )
61-
62- return ctx
63- }
64-
6555func router (s * server ) {
6656 r := mux .NewRouter ()
57+
58+ r .Use (newServerMiddleware (s .serverID ))
59+
6760 r .HandleFunc ("/gophers" , s .FetchGophers ).Methods (http .MethodGet )
6861 r .HandleFunc ("/gophers/{ID:[a-zA-Z0-9_]+}" , s .FetchGopher ).Methods (http .MethodGet )
6962 r .HandleFunc ("/gophers" , s .AddGopher ).Methods (http .MethodPost )
@@ -79,9 +72,7 @@ func (s *server) Router() http.Handler {
7972
8073// FetchGophers return a list of all gophers
8174func (s * server ) FetchGophers (w http.ResponseWriter , r * http.Request ) {
82- ctx := s .createContext (r .Context ())
83-
84- gophers , _ := s .fetching .FetchGophers (ctx )
75+ gophers , _ := s .fetching .FetchGophers (r .Context ())
8576
8677 w .Header ().Set ("Content-Type" , "application/json" )
8778 _ = json .NewEncoder (w ).Encode (gophers )
@@ -90,10 +81,8 @@ func (s *server) FetchGophers(w http.ResponseWriter, r *http.Request) {
9081
9182// FetchGopher return a gopher by ID
9283func (s * server ) FetchGopher (w http.ResponseWriter , r * http.Request ) {
93- ctx := s .createContext (r .Context ())
94-
9584 vars := mux .Vars (r )
96- gopher := s .fetching .FetchGopherByID (ctx , vars ["ID" ])
85+ gopher := s .fetching .FetchGopherByID (r . Context () , vars ["ID" ])
9786 w .Header ().Set ("Content-Type" , "application/json" )
9887 if gopher == nil {
9988 w .WriteHeader (http .StatusNotFound )
@@ -125,8 +114,7 @@ func (s *server) AddGopher(w http.ResponseWriter, r *http.Request) {
125114 _ = json .NewEncoder (w ).Encode ("Error unmarshalling request body" )
126115 return
127116 }
128- ctx := s .createContext (r .Context ())
129- if err := s .adding .AddGopher (ctx , g .ID , g .Name , g .Image , g .Age ); err != nil {
117+ if err := s .adding .AddGopher (r .Context (), g .ID , g .Name , g .Image , g .Age ); err != nil {
130118 w .WriteHeader (http .StatusInternalServerError )
131119 _ = json .NewEncoder (w ).Encode ("Can't create a gopher" )
132120 return
@@ -155,9 +143,7 @@ func (s *server) ModifyGopher(w http.ResponseWriter, r *http.Request) {
155143 return
156144 }
157145 vars := mux .Vars (r )
158- ctx := s .createContext (r .Context ())
159-
160- if err := s .modifying .ModifyGopher (ctx , vars ["ID" ], g .Name , g .Image , g .Age ); err != nil {
146+ if err := s .modifying .ModifyGopher (r .Context (), vars ["ID" ], g .Name , g .Image , g .Age ); err != nil {
161147 w .WriteHeader (http .StatusInternalServerError )
162148 _ = json .NewEncoder (w ).Encode ("Can't modify a gopher" )
163149 return
@@ -168,10 +154,9 @@ func (s *server) ModifyGopher(w http.ResponseWriter, r *http.Request) {
168154
169155// RemoveGopher remove a gopher
170156func (s * server ) RemoveGopher (w http.ResponseWriter , r * http.Request ) {
171- ctx := s .createContext (r .Context ())
172157 vars := mux .Vars (r )
173158
174- _ = s .removing .RemoveGopher (ctx , vars ["ID" ])
159+ _ = s .removing .RemoveGopher (r . Context () , vars ["ID" ])
175160 w .Header ().Set ("Content-Type" , "application/json" )
176161 w .WriteHeader (http .StatusNoContent )
177162
0 commit comments