Example #1
0
// NewHandler returns an http.Handler that supports the ori command-line utility.
// Mount it as follows:
// 	http.Handle("/path/to/api/_ori/", admin.NewHandler("/path/to/api/_ori/")) // Note the trailing slashes
//  http.Handle("/path/to/api", kami.Handler())
// You can attach it to a different path if you like; just make sure to use
// the --mount flag (or set ORI_ADMIN_MOUNT_POINT) in the CLI.
func NewHandler(route string) *kami.Mux {

	ori := kami.New()

	ori.Use("/", config.Middleware)
	ori.Use("/", auth.Middleware)
	ori.Use("/", rest.Middleware)

	ori.Get(route+"config", auth.Check(auth.Super).Then(getConfig))
	ori.Patch(route+"config", auth.Check(auth.Super).Then(changeConfig))

	ori.Post(route+"accounts", auth.Check(auth.Super).Then(newAccount))
	ori.Get(route+"accounts/:id", auth.Check(auth.Super).Then(getAccount))
	ori.Delete(route+"accounts/:id", auth.Check(auth.Super).Then(deleteAccount))
	ori.Patch(route+"accounts/:id", auth.Check(auth.Super).Then(changeAccount))
	ori.Post(route+"accounts/:id/password", auth.Check(auth.Super).Then(changeAccountPassword))
	ori.Get(route+"accounts/:id/jwt", auth.Check(auth.Super).Then(getJwt))
	ori.Post(route+"load", auth.Check(auth.Super).Then(loadEntities))

	return ori

}
Example #2
0
// TODO: this mostly a copy/paste of kami_test.go, rewrite it!
func TestKamiMux(t *testing.T) {
	mux := kami.New()

	// normal stuff
	mux.Use("/mux/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		return context.WithValue(ctx, "test1", "1")
	})
	mux.Use("/mux/v2/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		return context.WithValue(ctx, "test2", "2")
	})
	mux.Get("/mux/v2/papers/:page", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		page := kami.Param(ctx, "page")
		if page == "" {
			panic("blank page")
		}
		io.WriteString(w, page)

		test1 := ctx.Value("test1").(string)
		test2 := ctx.Value("test2").(string)

		if test1 != "1" || test2 != "2" {
			t.Error("unexpected ctx value:", test1, test2)
		}
	})

	// 404 stuff
	mux.Use("/mux/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
		return context.WithValue(ctx, "ok", true)
	})
	mux.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		ok, _ := ctx.Value("ok").(bool)
		if !ok {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusTeapot)
	})

	stdMux := http.NewServeMux()
	stdMux.Handle("/mux/", mux)

	// test normal stuff
	resp := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "/mux/v2/papers/3", nil)
	if err != nil {
		t.Fatal(err)
	}

	stdMux.ServeHTTP(resp, req)
	if resp.Code != http.StatusOK {
		t.Error("should return HTTP OK", resp.Code, "≠", http.StatusOK)
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	if string(data) != "3" {
		t.Error("expected page 3, got", string(data))
	}

	// test 404
	resp = httptest.NewRecorder()
	req, err = http.NewRequest("GET", "/mux/missing/hello", nil)
	if err != nil {
		t.Fatal(err)
	}

	stdMux.ServeHTTP(resp, req)
	if resp.Code != http.StatusTeapot {
		t.Error("should return HTTP Teapot", resp.Code, "≠", http.StatusTeapot)
	}
}