-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
112 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package chi | ||
|
||
import ( | ||
"github.com/diffuse/gloss" | ||
"github.com/go-chi/chi" | ||
"github.com/go-chi/chi/middleware" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type Router struct { | ||
*chi.Mux | ||
db gloss.Database | ||
} | ||
|
||
// NewRouter creates a router, associates a database | ||
// with it, and mounts API routes | ||
func NewRouter(db gloss.Database) *Router { | ||
r := &Router{db: db} | ||
r.setupRoutes() | ||
|
||
return r | ||
} | ||
|
||
// setupRoutes | ||
func (rt *Router) setupRoutes() { | ||
// set routes | ||
routes := chi.NewRouter() | ||
routes.Post("/counter/{counterId}", rt.IncrementCounterById) | ||
routes.Get("/counter/{counterId}", rt.GetCounterById) | ||
|
||
// setup router | ||
rt.Mux = chi.NewRouter() | ||
|
||
// set middleware | ||
rt.Mux.Use( | ||
middleware.Logger, | ||
middleware.Recoverer) | ||
|
||
// mount routes on versioned path | ||
rt.Mux.Mount("/v1", routes) | ||
} | ||
|
||
// Increment the value of the counter with ID counterId | ||
func (rt *Router) IncrementCounterById(w http.ResponseWriter, r *http.Request) { | ||
// get the counter ID | ||
counterId, err := strconv.ParseUint(chi.URLParam(r, "counterId"), 10, 32) | ||
if err != nil { | ||
http.Error(w, "invalid counter ID", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// increment in db | ||
if err := rt.db.IncrementCounter(int(counterId)); err != nil { | ||
http.Error(w, "failed to increment counter value", http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
// Get the value of the counter with ID counterId | ||
func (rt *Router) GetCounterById(w http.ResponseWriter, r *http.Request) { | ||
// get the counter ID | ||
counterId, err := strconv.ParseInt(chi.URLParam(r, "counterId"), 10, 32) | ||
if err != nil { | ||
http.Error(w, "invalid counter ID", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// get value and return to requester | ||
val, err := rt.db.GetCounterVal(int(counterId)) | ||
if err != nil { | ||
http.Error(w, "failed to get counter value", http.StatusNotFound) | ||
return | ||
} | ||
|
||
if _, err := w.Write([]byte(strconv.Itoa(val))); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import ( | |
) | ||
|
||
type Database struct { | ||
db *pgx.Conn | ||
db *pgx.Conn | ||
ctx context.Context | ||
} | ||
|
||
|