Add initial project files
This commit is contained in:
173
api/router.go
Normal file
173
api/router.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/eslider/superherohub/pkg/deesee"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
// Router is a custom router that encapsulates the mux.Router
|
||||
type Router struct {
|
||||
Heros []*deesee.Superhero // The Heros where superheroes are stored
|
||||
router *mux.Router // Encapsulated mux router
|
||||
logger *log.Logger // The logger to use for this router
|
||||
key int // The key to use for DeeSee encryption
|
||||
}
|
||||
|
||||
// NewRouter creates a new encapsulated router.
|
||||
//
|
||||
// The pattern I use is to define a custom router structure
|
||||
// that has a `mux.Router` as the field and also encapsulates things
|
||||
// like the JSON Heros to load, encryption-keys, database connection,
|
||||
// application configuration, and so on.
|
||||
//
|
||||
// This makes it easy to update routes as they
|
||||
// require different resources and development progresses.
|
||||
//
|
||||
// And handlers shouldn't have prefix like "handle", course they are methods of the Router
|
||||
func NewRouter(heros []*deesee.Superhero, key int, logger *log.Logger) *Router {
|
||||
// Set the logger
|
||||
if logger == nil {
|
||||
logger = log.Default()
|
||||
}
|
||||
|
||||
r := &Router{
|
||||
Heros: heros,
|
||||
key: key,
|
||||
logger: logger,
|
||||
router: mux.NewRouter(),
|
||||
}
|
||||
|
||||
r.router.Use(r.loggingMiddleware)
|
||||
|
||||
// Set a custom 404 handler
|
||||
r.router.NotFoundHandler = http.HandlerFunc(r.HandleNotFound)
|
||||
|
||||
// Set `getSuperHeroes` as the handler for the route
|
||||
r.router.HandleFunc("/superheroes", r.GetSuperHeroes).Methods(http.MethodGet)
|
||||
|
||||
// Set `putSuperHero` as the handler for the route
|
||||
r.router.HandleFunc("/superheroes", r.StoreSuperHero).Methods(http.MethodPut)
|
||||
|
||||
// Enable CORS by uncommenting the following line
|
||||
// router.Use(mux.CORSMethodMiddleware(router))
|
||||
|
||||
// Maybe better to use /api prefix for all routes?
|
||||
// subrouter := router.PathPrefix("/api").Subrouter()
|
||||
return r
|
||||
}
|
||||
|
||||
// GetHandler returns the encapsulated router
|
||||
func (r *Router) GetHandler() http.Handler {
|
||||
return r.router
|
||||
}
|
||||
|
||||
// GetSuperHeroes returns a list of superheroes
|
||||
// with optional filtering and encryption of identities.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// - Retrieve all superheroes
|
||||
//
|
||||
// GET /superheroes
|
||||
//
|
||||
// - Retrieve all superheroes with encrypted identities:
|
||||
//
|
||||
// GET /superheroes?encode=deesee
|
||||
//
|
||||
// - Retrieve superheroes that match given superpower(s)
|
||||
//
|
||||
// GET /superheroes?powers=flight,super-strength
|
||||
//
|
||||
// - Retrieve superheroes that match given superpower(s) with encrypted identities
|
||||
//
|
||||
// GET /superheroes?powers=flight,super-strength&encode=deesee
|
||||
func (r *Router) GetSuperHeroes(w http.ResponseWriter, req *http.Request) {
|
||||
var (
|
||||
// Resulting
|
||||
heroes = r.Heros
|
||||
// Get query parameter values
|
||||
params = req.URL.Query()
|
||||
err error
|
||||
)
|
||||
|
||||
// Filter superheroes by superpowers?
|
||||
// The name of the power should be long enough to search for, at least 3 chars.
|
||||
if powerFilter := params.Get("superpowers"); len(powerFilter) > 2 {
|
||||
|
||||
// Here we use the `strings` package to split comma-separated power names.
|
||||
// Maybe it's better to prevent send special chars by remove with regular expression.
|
||||
powers := strings.Split(powerFilter, ",")
|
||||
heroes = deesee.SearchByPowers(heroes, powers)
|
||||
}
|
||||
|
||||
// Encrypt identities with DeeSee encryption algorithm?
|
||||
if strings.EqualFold(params.Get("encrypted"), "true") {
|
||||
// Encrypt identities
|
||||
heroes = deesee.EncryptHerosIdentities(heroes, r.key)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
// Return JSON
|
||||
if err = json.NewEncoder(w).Encode(heroes); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// StoreSuperHero saves one superhero
|
||||
func (r *Router) StoreSuperHero(w http.ResponseWriter, req *http.Request) {
|
||||
var err error
|
||||
hero := deesee.NewSuperhero()
|
||||
if err = json.NewDecoder(req.Body).Decode(hero); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if superhero superpower is acceptable
|
||||
if !hero.IsAcceptable() {
|
||||
http.Error(w, fmt.Sprintf("Hero power is not acceptable: %s", hero.Name), http.StatusExpectationFailed)
|
||||
return
|
||||
}
|
||||
|
||||
// Prevent duplicate superheroes
|
||||
if deesee.FindByName(r.Heros, strings.TrimSpace(hero.Name)) != nil {
|
||||
http.Error(w, fmt.Sprintf("Hero is already exists: %s", hero.Name), http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
r.Heros = append(r.Heros, hero)
|
||||
|
||||
// Return JSON
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_, err = w.Write([]byte(`{"status": "ok"}`))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// HandleNotFound is a custom 404 handler
|
||||
func (r *Router) HandleNotFound(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, err := w.Write([]byte(`{"error": "not found"}`))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// loggingMiddleware is a middleware that logs all requests
|
||||
func (r *Router) loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||
r.logger.Printf("Request: %s %s", req.Method, req.RequestURI)
|
||||
next.ServeHTTP(w, req)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user