Browse Source

fast web recipe

Aaron Torres 2 years ago
parent
commit
a1cd521474

+ 26 - 0
chapter13/fastweb/handlers.go

@@ -0,0 +1,26 @@
+package main
+
+import (
+	"encoding/json"
+
+	"github.com/valyala/fasthttp"
+)
+
+// GetItems will return our items object
+func GetItems(ctx *fasthttp.RequestCtx) {
+	enc := json.NewEncoder(ctx)
+	items := ReadItems()
+	enc.Encode(&items)
+	ctx.SetStatusCode(fasthttp.StatusOK)
+}
+
+// AddItems modifies our array
+func AddItems(ctx *fasthttp.RequestCtx) {
+	item, ok := ctx.UserValue("item").(string)
+	if !ok {
+		ctx.SetStatusCode(fasthttp.StatusBadRequest)
+	}
+
+	AddItem(item)
+	ctx.SetStatusCode(fasthttp.StatusOK)
+}

+ 41 - 0
chapter13/fastweb/handlers_test.go

@@ -0,0 +1,41 @@
+package main
+
+import (
+	"testing"
+
+	"github.com/valyala/fasthttp"
+)
+
+func TestGetItems(t *testing.T) {
+	type args struct {
+		ctx *fasthttp.RequestCtx
+	}
+	tests := []struct {
+		name string
+		args args
+	}{
+		{"base-case", args{&fasthttp.RequestCtx{}}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			GetItems(tt.args.ctx)
+		})
+	}
+}
+
+func TestAddItems(t *testing.T) {
+	type args struct {
+		ctx *fasthttp.RequestCtx
+	}
+	tests := []struct {
+		name string
+		args args
+	}{
+		{"base-case", args{&fasthttp.RequestCtx{}}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			AddItems(tt.args.ctx)
+		})
+	}
+}

+ 28 - 0
chapter13/fastweb/items.go

@@ -0,0 +1,28 @@
+package main
+
+import (
+	"sync"
+)
+
+var items []string
+var mu *sync.RWMutex
+
+func init() {
+	mu = &sync.RWMutex{}
+}
+
+// AddItem adds an item to our list
+// in a thread-safe way
+func AddItem(item string) {
+	mu.Lock()
+	items = append(items, item)
+	mu.Unlock()
+}
+
+// ReadItems returns our list of items
+// in a thread-safe way
+func ReadItems() []string {
+	mu.RLock()
+	defer mu.RUnlock()
+	return items
+}

+ 39 - 0
chapter13/fastweb/items_test.go

@@ -0,0 +1,39 @@
+package main
+
+import (
+	"reflect"
+	"testing"
+)
+
+func TestAddItem(t *testing.T) {
+	type args struct {
+		item string
+	}
+	tests := []struct {
+		name string
+		args args
+	}{
+		{"base-case", args{"test"}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			AddItem(tt.args.item)
+		})
+	}
+}
+
+func TestReadItems(t *testing.T) {
+	tests := []struct {
+		name string
+		want []string
+	}{
+		{"base-case", []string{"", "test"}},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := ReadItems(); !reflect.DeepEqual(got, tt.want) {
+				t.Errorf("ReadItems() = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}

+ 18 - 0
chapter13/fastweb/main.go

@@ -0,0 +1,18 @@
+package main
+
+import (
+	"fmt"
+	"log"
+
+	"github.com/buaazp/fasthttprouter"
+	"github.com/valyala/fasthttp"
+)
+
+func main() {
+	router := fasthttprouter.New()
+	router.GET("/item", GetItems)
+	router.POST("/item/:item", AddItems)
+
+	fmt.Println("server starting on localhost:8080")
+	log.Fatal(fasthttp.ListenAndServe("localhost:8080", router.Handler))
+}