Damnit again

Former-commit-id: b394f26caf0df7d113ac4cc7dacc9c544af6897f
This commit is contained in:
bel
2019-06-21 18:12:31 -06:00
commit 90a31495c9
32 changed files with 3464 additions and 0 deletions

BIN
server/.routes_test.go.swp Normal file

Binary file not shown.

21
server/new.go Normal file
View File

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

13
server/new_test.go Normal file
View File

@@ -0,0 +1,13 @@
package server
import (
"local/rssmon3/config"
"os"
"testing"
)
func TestNew(t *testing.T) {
os.Setenv("DB", "MAP")
config.New()
New()
}

165
server/routes.go Normal file
View File

@@ -0,0 +1,165 @@
package server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"local/router"
"local/rssmon3/config"
"local/sandbox/selenium/copart/copart/auction"
"local/storage"
"log"
"net/http"
"regexp"
"strings"
)
func (s *Server) Routes() error {
handles := []struct {
path string
handler http.HandlerFunc
}{
{path: fmt.Sprintf("/api/tag/%s", router.Wildcard), handler: s.tag},
}
for _, handle := range handles {
if err := s.router.Add(handle.path, handle.handler); err != nil {
return err
}
}
return nil
}
func (s *Server) notFound(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
func (s *Server) userError(w http.ResponseWriter, r *http.Request, err error) {
status := http.StatusBadRequest
log.Printf("%d: %v", status, err)
w.WriteHeader(status)
}
func (s *Server) error(w http.ResponseWriter, r *http.Request, err error) {
status := http.StatusInternalServerError
log.Printf("%d: %v", status, err)
w.WriteHeader(status)
}
func (s *Server) tag(w http.ResponseWriter, r *http.Request) {
foo := s.notFound
switch r.Method {
case "GET":
foo = s.getFeedByTag
}
foo(w, r)
}
func (s *Server) getFeedByTag(w http.ResponseWriter, r *http.Request) {
tag := regexp.MustCompile("^.*\\/").ReplaceAllString(r.URL.Path, "")
log.Println(tag)
s.error(w, r, errors.New("not impl"))
}
func (s *Server) auctions(w http.ResponseWriter, r *http.Request) {
foo := http.NotFound
r.ParseForm()
switch strings.ToLower(r.Method) {
case "get":
if len(r.Form) > 0 {
foo = s.query
} else {
foo = s.get
}
}
foo(w, r)
}
func (s *Server) get(w http.ResponseWriter, r *http.Request) {
db := config.Values().DB
if len(strings.Split(r.URL.Path, "/")) < 3 {
s.notFound(w, r)
return
}
key := strings.Split(r.URL.Path, "/")[2]
log.Println("GET", key)
value, err := db.Get(key)
if err == storage.ErrNotFound {
s.notFound(w, r)
return
}
if err != nil {
s.error(w, r, err)
return
}
io.Copy(w, bytes.NewBuffer(value))
}
func (s *Server) query(w http.ResponseWriter, r *http.Request) {
log.Println("QUERY", r.URL.Path)
list, err := s.list()
if err != nil {
s.error(w, r, err)
return
}
form := r.Form
for _, value := range form {
for _, chunk := range value {
iteration := []string{}
for _, name := range list {
if strings.Contains(strings.ToLower(name), strings.ToLower(chunk)) {
iteration = append(iteration, name)
}
}
list = iteration
}
}
if len(list) < 1 {
s.notFound(w, r)
return
}
carsJSON, err := s.gatherCarsTruncatedJSON(list)
if err != nil {
s.error(w, r, fmt.Errorf("gather cars(%v): %v", list, err))
return
}
w.Write(carsJSON)
}
func (s *Server) list() ([]string, error) {
db := config.Values().DB
list := []string{}
if b, err := db.Get("LIST"); err == storage.ErrNotFound {
list = []string{}
db.Set("LIST", []byte("[]"))
} else if err != nil {
return nil, fmt.Errorf("bad list in storage: %v", err)
} else if err := json.Unmarshal(b, &list); err != nil {
return nil, fmt.Errorf("bad list in storage: %v", err)
}
return list, nil
}
func (s *Server) gatherCarsTruncatedJSON(keys []string) ([]byte, error) {
cars := []*auction.Car{}
k := 0
for _, key := range keys {
k += 1
if k > 50 {
cars = append(cars, &auction.Car{Title: "Max results reached, please search more specifically"})
break
}
b, err := config.Values().DB.Get(key)
if err != nil {
return nil, err
}
c := auction.NewCar()
if err := c.Decode(b); err != nil {
return nil, err
}
cars = append(cars, c)
}
return json.Marshal(cars)
}

104
server/routes_test.go Normal file
View File

@@ -0,0 +1,104 @@
package server
import (
"local/rssmon3/config"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
var validNS = "ns"
var validKey = "key"
var validValue = "value"
func TestRoutes(t *testing.T) {
os.Setenv("DB", "MAP")
os.Args = []string{"nothing"}
if err := config.New(); err != nil {
t.Fatal(err)
}
s := New()
if err := s.Routes(); err != nil {
t.Fatalf("cannot routes(): %v", err)
}
cases := []struct {
path string
method string
status int
}{
{
path: "/api/tag",
method: "GET",
status: http.StatusNotFound,
},
{
path: "/api/tag/",
method: "GET",
status: http.StatusNotFound,
},
{
path: "/api/tag/key",
method: "GET",
status: http.StatusOK,
},
{
path: "/api/tag/key",
method: "POST",
status: http.StatusNotFound,
},
}
for i, c := range cases {
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("[%d] wrong status for %q on %q: %v, want %v", i, c.method, c.path, w.Code, c.status)
}
}
}
func TestServerHandlersNotFound(t *testing.T) {
os.Args = []string{"a"}
config.New()
s := New()
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
s.notFound(w, r)
if w.Code != http.StatusNotFound {
t.Error(w.Code)
}
}
func TestServerHandlers(t *testing.T) {
os.Args = []string{"a"}
config.New()
s := New()
cases := []struct {
foo func(http.ResponseWriter, *http.Request, error)
code int
}{
{
foo: s.userError,
code: http.StatusBadRequest,
},
{
foo: s.error,
code: http.StatusInternalServerError,
},
}
for _, c := range cases {
w := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "/", nil)
c.foo(w, r, nil)
if w.Code != c.code {
t.Errorf("unexpected status: want %v, got %v", c.code, w.Code)
}
}
}

13
server/server.go Normal file
View File

@@ -0,0 +1,13 @@
package server
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)
}

14
server/server_test.go Normal file
View File

@@ -0,0 +1,14 @@
package server
import (
"local/rssmon3/config"
"testing"
"time"
)
func TestServerRun(t *testing.T) {
config.New()
s := New()
go s.Run()
time.Sleep(time.Millisecond * 100)
}