Implement simple client

This commit is contained in:
Bel LaPointe
2019-03-14 15:20:23 -06:00
parent 1f679f4c06
commit c5b6ad08e4
5 changed files with 152 additions and 5 deletions

View File

@@ -7,14 +7,14 @@ import (
)
type Server struct {
port string
Port string
router *router.Router
}
func New() *Server {
config := config.Values()
return &Server{
port: ":" + strings.TrimPrefix(config.Port, ":"),
Port: ":" + strings.TrimPrefix(config.Port, ":"),
router: router.New(),
}
}

View File

@@ -32,14 +32,14 @@ func (s *Server) CatchAll(w http.ResponseWriter, r *http.Request) {
func (s *Server) get(w http.ResponseWriter, r *http.Request) {
config := config.Values()
key := strings.Split(r.URL.Path, "/")[1]
if v, err := config.DB.Get(key); err == storage.ErrNotFound {
if value, err := config.DB.Get(key); err == storage.ErrNotFound {
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.Write(v)
w.Write(value)
}
}

View File

@@ -5,7 +5,7 @@ import (
)
func (s *Server) Run() error {
return http.ListenAndServe(s.port, s)
return http.ListenAndServe(s.Port, s)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {