func (this FakeProvider) Middleware() rack.Middleware {
	if this.middleware == nil {
		hostcept := interceptor.New()
		hostcept.Intercept("/auth", rack.Func(func(vars map[string]interface{}, next func()) {
			values := url.Values{}
			values.Set("state", parser.V(vars).FormValue("state"))
			values.Set("code", "c0D3")
			redirecter.V(vars).Redirect(parser.V(vars).FormValue("redirect_uri") + "?" + values.Encode())
		}))
		hostcept.Intercept("/token", rack.Func(func(vars map[string]interface{}, next func()) {
			if parser.V(vars).FormValue("code") == "c0D3" {
				httper.V(vars).GetRequest().Header.Set("content-type", "application/json")
				httper.V(vars).SetMessageString("{\"access_token\":\"tokendata\",\"refresh_token\":\"refreshtoken1\",\"expires_in\":3600}")
			}
		}))
		hostcept.Intercept("/data", rack.Func(func(vars map[string]interface{}, next func()) {
			if auth := httper.V(vars).GetRequest().Header.Get("Authorization"); auth != "Bearer tokendata" {
				httper.V(vars).SetMessageString("Invalid authorization: " + auth)
			}
			httper.V(vars).SetMessageString("payload")
		}))
		hostrackup := rack.New()
		hostrackup.Add(sessioner.Middleware)
		hostrackup.Add(hostcept)

		this.middleware = hostrackup
	}
	return this.middleware
}
func TokenHandlerFunc(o Oauther, tok *oauth.Token) rack.Middleware {
	return rack.Func(func(vars map[string]interface{}, next func()) {
		if tok == nil {
			(httper.V)(vars).SetMessageString("User declined app")
		} else {
			(httper.V)(vars).SetMessageString(getPayload(o, tok))
		}
	})
}
func Example_NoError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
	}))

	conn := httper.HttpConnection(":3002")
	go conn.Go(rackup)

	GetFrom("http://localhost:3002/")
	//output: Just Fine!
}
Example #4
0
// Get Templates will return a Middleware that will load a specified folder of templates into your rack environment
func GetTemplates(loc string) rack.Middleware {
	t, errs := templater.New(loc)

	return rack.Func(func(vars map[string]interface{}, next func()) {
		if len(errs) > 0 {
			for _, err := range errs {
				(logger.V)(vars).Println("Template Loading - Warning - " + err.Error())
			}
		}
		vars[template_index] = Group{t}
		next()
	})
}
Example #5
0
func (this dispatchAction) Run(vars map[string]interface{}, next func()) {
	actions := rack.New()
	actions.Add(this.action)
	switch (httper.V)(vars).GetRequest().Method {
	case "GET":
		//if it was a get, the default action should be to render the template corresponding with the action
		actions.Add(renderer.Renderer{this.routeName + "/" + this.name})
	case "POST", "PUT":
		//if it was a put or a post, we the default action should be to redirect to the affected item
		actions.Add(rack.Func(func(vars map[string]interface{}, next func()) {
			(redirecter.V)(vars).Redirect(this.descriptor.router.Route(vars))
		}))
	case "DELETE":
		actions.Add(rack.Func(func(vars map[string]interface{}, next func()) {
			delete(vars, this.varName)
			(redirecter.V)(vars).Redirect(this.descriptor.router.Route(vars))
		}))
	default:
		panic("Unknown method")
	}
	actions.Run(vars, next)
}
Example #6
0
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(V{"world": "World!"})
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		world := vars["world"].(string)
		(httper.V)(vars).SetMessageString("Hello " + world)
	}))

	conn := httper.HttpConnection(":4015")
	go conn.Go(rackup)

	GetFrom("http://localhost:4015/")
	//output: Hello World!
}
func Example_BasicError() {
	rackup := rack.New()
	rackup.Add(ErrorHandler)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		httper.V(vars).SetMessageString("Just Fine!")
		array := make([]byte, 0)
		array[1] = 0 //this action results in a runtime error; we are indexing past the range of the slice
	}))

	conn := httper.HttpConnection(":4001")
	go conn.Go(rackup)

	GetFrom("http://localhost:4001/")
	//output: runtime error: index out of range
}
func Example_NoLayout() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(AddLayout)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Title"] = "Hello World"
		(httper.V)(vars).AppendMessageString("Hello World!")
	}))

	conn := httper.HttpConnection(":3001")
	go conn.Go(rackup)

	GetFrom("http://localhost:3001/")
	//output: Hello World!
}
func Example_Render() {
	rackup := rack.New()
	rackup.Add(logger.New(os.Stdout, "", 0))
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Object"] = "World"
		next()
	}))
	rackup.Add(Renderer{"test"})

	conn := httper.HttpConnection(":4010")
	go conn.Go(rackup)

	GetFrom("http://localhost:4010/")
	//output: Hello World
}
func Example_Basic() {
	rackup := rack.New()
	rackup.Add(templater.GetTemplates("./test_templates"))
	rackup.Add(AddLayout)
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		vars["Layout"] = "test"
		vars["Title"] = "Hello World"
		(httper.V)(vars).AppendMessageString("Hello World!")
	}))

	conn := httper.HttpConnection(":4000")
	go conn.Go(rackup)

	GetFrom("http://localhost:4000/")
	//output: <html><head><title>Hello World</title></head><body>Hello World!</body></html>
}
Example #11
0
func (this descriptor) addDispatchAction(funcs map[string]rack.Middleware, name string, method reflect.Method) {
	name = strings.ToLower(name)
	d := new(dispatchAction)
	d.descriptor = this
	d.name = name
	d.action = rack.Func(func(vars map[string]interface{}, next func()) {
		copy := reflect.New(this.t)
		mapper := copy.Interface().(ResourceController)
		mapper.SetRackVars(this, vars, next)
		method.Func.Call([]reflect.Value{reflect.Indirect(copy)})
		if !mapper.IsFinished() {
			next()
		}
	})

	funcs[name] = d
}
func Example_Redirect() {
	rackup := rack.New()
	rackup.Add(rack.Func(func(vars map[string]interface{}, next func()) {
		h := (httper.V)(vars)
		path := h.GetRequest().URL.Path
		if path != "/" {
			h.SetMessageString(path)
		} else {
			next()
		}
	}))
	rackup.Add(Redirecter{"/redirected/"})

	conn := httper.HttpConnection(":4008")
	go conn.Go(rackup)

	GetFrom("http://localhost:4008/")
	//output: /redirected/
}
Example #13
0
func init() {
	var MemberRoute *Router = New()

	MemberRoute.Routing = SignalFunc(func(vars map[string]interface{}) bool {
		coinName := (V)(vars).CurrentSection()
		coinInfo, exists := coins[coinName]
		if !exists {
			return false
		}
		vars["Name"] = coinName
		vars["Info"] = coinInfo
		return true
	})

	MemberRoute.Action = rack.Func(func(vars map[string]interface{}, next func()) {
		name := vars["Name"].(string)
		info := vars["Info"].(string)
		(httper.V)(vars).SetMessageString(name + " - " + info + " - " + MemberRoute.Route(vars))
	})

	MemberRoute.Name = NamerFunc(func(vars map[string]interface{}) string {
		name := "(coin)"
		if coin, ok := vars["Name"].(string); ok {
			name = coin
		}
		return name
	})

	CollectionRoute := BasicRoute("coins", CoinCollectionWare)
	CollectionRoute.AddRoute(MemberRoute)

	Root := BasicRoute("", RootWare)
	Root.AddRoute(CollectionRoute)

	conn := httper.HttpConnection(":4011")
	go conn.Go(Root)
}
func TestBinary(t *testing.T) {
	//setup test variables
	var isOpened, isClosed bool
	message := []byte{}

	//create websocket handler
	ws := New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		isOpened = true
		next()
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		message = V(vars).GetMessage().([]byte)
		V(vars).SetResponse([]byte("Who's there?"))
		next()
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		isClosed = true
		next()
	}))
	ws.ReceiveBinaryMessages()

	//start a web server
	server := httper.HttpConnection(":4017")
	go server.Go(ws)

	//connect via websocket
	conn, err := websocket.Dial("ws://localhost:4017", "", "http://localhost")
	if err != nil {
		t.Fatal("Fatal - Can't open")
	}

	if !isOpened {
		t.Error("Should be open")
	}

	//send a message
	if _, err := conn.Write([]byte("Knock knock!")); err != nil {
		t.Fatal("Fatal - Can't write")
	}

	//get a response
	response := make([]byte, 512)
	n, err := conn.Read(response)
	if err != nil {
		t.Fatal("Fatal - Can't read")
	}

	if !bytes.Equal(message, []byte("Knock knock!")) {
		t.Error("Message not passed through")
	}
	if !bytes.Equal(response[:n], []byte("Who's there?")) {
		t.Error("Response not passed through")
	}

	//and close
	if err := conn.Close(); err != nil {
		t.Fatal("Fatal - Can't close")
	}

	<-time.After(time.Second / 10)

	if !isClosed {
		t.Error("Should be closed")
	}
}
func TestJSON(t *testing.T) {
	//setup test variables
	var isOpened, isClosed bool
	message := TestJSONMessage{}

	//create websocket handler
	ws := New()
	ws.OnOpen(rack.Func(func(vars map[string]interface{}, next func()) {
		isOpened = true
		next()
	}))
	ws.OnMessage(rack.Func(func(vars map[string]interface{}, next func()) {
		message = V(vars).GetMessage().(TestJSONMessage)
		fmt.Println(message)
		V(vars).SetResponse("Who's there?")
		next()
	}))
	ws.OnClose(rack.Func(func(vars map[string]interface{}, next func()) {
		isClosed = true
		next()
	}))

	defaultObj := TestJSONMessage{Type: "Blank", Data: map[string]interface{}{"test": "test"}}
	ws.ReceiveJSONObjects(defaultObj)

	//start a web server
	server := httper.HttpConnection(":4018")
	go server.Go(ws)

	//connect via websocket
	conn, err := websocket.Dial("ws://localhost:4018", "", "http://localhost")
	if err != nil {
		t.Fatal("Fatal - Can't open")
	}

	if !isOpened {
		t.Error("Should be open")
	}

	//send a message
	sendme := TestJSONMessage{Type: "text", Data: map[string]interface{}{"message": "Knock knock!"}}
	data, err := json.Marshal(sendme)
	if err != nil {
		t.Fatal("Fatal - Can't json")
	}

	if _, err := conn.Write(data); err != nil {
		t.Fatal("Fatal - Can't write")
	}

	//get a response
	response := make([]byte, 512)
	n, err := conn.Read(response)
	if err != nil {
		t.Fatal("Fatal - Can't read")
	}

	//response is in JSON, convert back to regular
	var response_object string
	json.Unmarshal(response[:n], &response_object)

	//check for message & response errors
	if message.Type != "text" {
		t.Error("Message Type Wrong - ", message.Type)
	}
	if message.Data["message"] != "Knock knock!" {
		t.Error("Message Wrong - ", message.Data["message"])
	}
	if response_object != "Who's there?" {
		t.Error("Response not passed through - ", response_object)
	}

	//and close
	if err := conn.Close(); err != nil {
		t.Fatal("Fatal - Can't close")
	}

	<-time.After(time.Second / 10)

	if !isClosed {
		t.Error("Should be closed")
	}
}