Initial dumb servers

This commit is contained in:
Bel LaPointe
2019-03-14 14:43:02 -06:00
commit 1f679f4c06
10 changed files with 381 additions and 0 deletions

20
server/serve/new.go Normal file
View File

@@ -0,0 +1,20 @@
package serve
import (
"local/dynamodb/server/config"
"local/s2sa/s2sa/server/router"
"strings"
)
type Server struct {
port string
router *router.Router
}
func New() *Server {
config := config.Values()
return &Server{
port: ":" + strings.TrimPrefix(config.Port, ":"),
router: router.New(),
}
}

11
server/serve/new_test.go Normal file
View File

@@ -0,0 +1,11 @@
package serve
import (
"os"
"testing"
)
func TestNew(t *testing.T) {
os.Setenv("DB", "MAP")
New()
}

62
server/serve/routes.go Normal file
View File

@@ -0,0 +1,62 @@
package serve
import (
"io/ioutil"
"local/dynamodb/server/config"
"local/s2sa/s2sa/server/router"
"local/storage"
"net/http"
"strings"
)
func (s *Server) Routes() error {
if err := s.router.Add("/"+router.Wildcard, s.CatchAll); err != nil {
return err
}
return nil
}
func (s *Server) CatchAll(w http.ResponseWriter, r *http.Request) {
foo := http.NotFound
switch strings.ToLower(r.Method) {
case "get":
foo = s.get
case "put":
foo = s.put
case "delete":
foo = s.put
}
foo(w, r)
}
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 {
w.WriteHeader(http.StatusNotFound)
return
} else if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.Write(v)
}
}
func (s *Server) put(w http.ResponseWriter, r *http.Request) {
config := config.Values()
key := strings.Split(r.URL.Path, "/")[1]
if r == nil || r.Body == nil {
w.WriteHeader(http.StatusBadRequest)
return
}
value, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if err := config.DB.Set(key, value); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}

137
server/serve/routes_test.go Normal file
View File

@@ -0,0 +1,137 @@
package serve
import (
"io/ioutil"
"local/dynamodb/server/config"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
var validKey = "key"
var validValue = "value"
func TestRoutes(t *testing.T) {
os.Setenv("DB", "MAP")
config.New()
s := New()
if err := s.Routes(); err != nil {
t.Fatalf("cannot routes(): %v", err)
}
config.Values().DB.Set(validKey, []byte("anything"))
cases := []struct {
path string
method string
status int
}{
{
path: "/" + validKey,
method: "GET",
status: http.StatusOK,
},
{
path: "/" + validKey,
method: "PUT",
status: http.StatusOK,
},
{
path: "/" + validKey,
method: "DELETE",
status: http.StatusOK,
},
{
path: "/" + validKey,
method: "POST",
status: http.StatusNotFound,
},
{
path: "/",
method: "GET",
status: http.StatusNotFound,
},
{
path: "/",
method: "PUT",
status: http.StatusNotFound,
},
{
path: "/",
method: "DELETE",
status: http.StatusNotFound,
},
{
path: "/",
method: "POST",
status: http.StatusNotFound,
},
{
path: "",
method: "GET",
status: http.StatusNotFound,
},
{
path: "",
method: "PUT",
status: http.StatusNotFound,
},
{
path: "",
method: "DELETE",
status: http.StatusNotFound,
},
{
path: "",
method: "POST",
status: http.StatusNotFound,
},
}
for _, c := range cases {
//t.Logf("CASE %v", c)
r, err := http.NewRequest(c.method, c.path, strings.NewReader(validValue))
if err != nil {
t.Fatalf("err making request: %v", err)
}
w := httptest.NewRecorder()
s.ServeHTTP(w, r)
if w.Code != c.status {
t.Errorf("wrong status for %q on %q: %v, want %v", c.method, c.path, w.Code, c.status)
}
}
}
func TestPutGet(t *testing.T) {
os.Setenv("DB", "MAP")
config.New()
s := New()
if err := s.Routes(); err != nil {
t.Errorf("cannot routes(): %v", err)
}
w := httptest.NewRecorder()
r, err := http.NewRequest("PUT", "/"+validKey, strings.NewReader(validValue))
if err != nil {
t.Fatalf("err making put request: %v", err)
}
s.put(w, r)
if w.Code != http.StatusOK {
t.Fatalf("err status on put: %v", w.Code)
}
w = httptest.NewRecorder()
r, err = http.NewRequest("GET", "/"+validKey, nil)
if err != nil {
t.Fatalf("err making get request: %v", err)
}
s.get(w, r)
if w.Code != http.StatusOK {
t.Fatalf("err status on get: %v", w.Code)
} else if v, err := ioutil.ReadAll(w.Body); err != nil {
t.Fatalf("err reading response body on get: %v", err)
} else if string(v) != validValue {
t.Fatalf("wrong response body on get: %v", string(v))
}
}

13
server/serve/server.go Normal file
View File

@@ -0,0 +1,13 @@
package serve
import (
"net/http"
)
func (s *Server) Run() error {
return http.ListenAndServe(s.port, s)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}