func BenchmarkMiddleware5(b *testing.B) { kami.Reset() numbers := []int{1, 2, 3, 4, 5} for _, n := range numbers { n := n // wtf kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, n, n) }) } kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { for _, n := range numbers { if ctx.Value(n) != n { w.WriteHeader(501) return } } }) for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/test", nil) kami.Handler().ServeHTTP(resp, req) if resp.Code != 200 { panic(resp.Code) } } }
func main() { dbMap := InitDB() defer dbMap.Db.Close() dbMap.TraceOn("[gorp]", log.New(os.Stdout, "scribble: ", log.Lmicroseconds)) ctx := context.Background() kami.Context = context.WithValue(ctx, "db", dbMap) // Middlwares kami.Use("/api/", auth.AuthMiddleware) // Authentication APIs kami.Post("/api/register", handler.Register) kami.Post("/api/login", handler.Login) kami.Post("/api/logout", handler.Logout) // Note APIs kami.Get("/api/notes", handler.ListNotes) kami.Post("/api/notes", handler.AddNote) kami.Get("/api/notes/:noteId", handler.GetNote) kami.Put("/api/notes/:noteId", handler.UpdateNote) kami.Delete("/api/notes/:noteId", handler.DeleteNote) kami.Serve() }
func BenchmarkMiddleware5Afterware1(b *testing.B) { kami.Reset() numbers := []int{1, 2, 3, 4, 5} for _, n := range numbers { n := n // wtf kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, n, n) }) } kami.After("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { for _, n := range numbers { if ctx.Value(n) != n { panic(n) } } return ctx }) kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { for _, n := range numbers { if ctx.Value(n) != n { w.WriteHeader(http.StatusServiceUnavailable) return } } }) req, _ := http.NewRequest("GET", "/test", nil) b.ResetTimer() for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() kami.Handler().ServeHTTP(resp, req) } }
func TestNotFound(t *testing.T) { kami.Reset() kami.Use("/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "ok", true) }) kami.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) { ok, _ := ctx.Value("ok").(bool) if !ok { w.WriteHeader(500) return } w.WriteHeader(420) }) resp := httptest.NewRecorder() req, err := http.NewRequest("GET", "/missing/hello", nil) if err != nil { t.Fatal(err) } kami.Handler().ServeHTTP(resp, req) if resp.Code != 420 { t.Error("should return HTTP 420", resp.Code, "≠", 420) } }
func main() { ctx := context.Background() kami.Context = ctx // set our "god context", the base context for all requests kami.Use("/", greetMiddleware) // use this middleware for paths under /hello/ kami.Get("/hello/:name", greet) // add a GET handler with a parameter in the URL kami.Serve() // gracefully serve with support for einhorn and systemd }
func TestKami(t *testing.T) { kami.Reset() kami.Use("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "test1", "1") }) kami.Use("/v2/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "test2", "2") }) kami.Get("/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) } }) resp := httptest.NewRecorder() req, err := http.NewRequest("GET", "/v2/papers/3", nil) if err != nil { t.Fatal(err) } kami.Handler().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)) } }
func routeBench(b *testing.B, route string) { kami.Reset() kami.Use("/Z/", noopMW) kami.After("/Z/", noopMW) kami.Get(route, noop) req, _ := http.NewRequest("GET", route, nil) b.ResetTimer() for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() kami.Handler().ServeHTTP(resp, req) } }
func TestNotFound(t *testing.T) { kami.Reset() kami.Use("/missing/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "ok", true) }) kami.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) }) expectResponseCode(t, "GET", "/missing/hello", http.StatusTeapot) }
func BenchmarkMiddleware(b *testing.B) { kami.Reset() kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "test", "ok") }) kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { if ctx.Value("test") != "ok" { w.WriteHeader(http.StatusServiceUnavailable) } }) req, _ := http.NewRequest("GET", "/test", nil) b.ResetTimer() for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() kami.Handler().ServeHTTP(resp, req) } }
// This tests just the URL walking middleware engine. func BenchmarkMiddlewareAfterwareMiss(b *testing.B) { kami.Reset() kami.Use("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return nil }) kami.After("/dog/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return nil }) kami.Get("/a/bbb/cc/d/e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) req, _ := http.NewRequest("GET", "/a/bbb/cc/d/e", nil) b.ResetTimer() for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() kami.Handler().ServeHTTP(resp, req) } }
func BenchmarkMiddleware(b *testing.B) { kami.Reset() kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "test", "ok") }) kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { if ctx.Value("test") != "ok" { w.WriteHeader(501) } }) for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/test", nil) kami.Handler().ServeHTTP(resp, req) if resp.Code != 200 { panic(resp.Code) } } }
func main() { var err error db, err = sql.Open("mysql", connectionString) if err != nil { log.Fatalf("Error opening database: %v", err) } db.SetMaxIdleConns(maxConnectionCount) flag.Set("bind", ":8080") kami.Use("/", serverMiddleware) kami.Get("/json", jsonHandler) kami.Get("/db", dbHandler) kami.Get("/queries", queriesHandler) kami.Get("/fortunes", fortuneHandler) kami.Get("/updates", updateHandler) kami.Get("/plaintext", plaintextHandler) kami.Serve() }
func main() { var mongoUrl = flag.String("db", "petrucho-db", "MongoDB URL") flag.Parse() ctx := context.Background() ctx = db.OpenMongoDB(ctx, "main", *mongoUrl) defer db.Close(ctx) kami.Context = ctx secret := func(email, realm string) string { u := User{} users := userStorage(ctx) err := users.Find(bson.M{"email": email, "confirmed": true}).One(&u) if err != nil { return "" } return u.Password } authenticator := auth.NewBasicAuthenticator("Restricted", secret) kami.Get("/", homeHandler) kami.Get("/register", registrationForm) kami.Post("/register", registerUser) kami.Get("/confirm/:token", confirmRegistration) kami.Get("/users", viewAllUsers) kami.Get("/users/:email", viewUser) kami.Use("/users/:email/edit", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { var username string authenticator.Wrap(func(w http.ResponseWriter, r *auth.AuthenticatedRequest) { username = r.Username })(w, r) if len(username) == 0 { return nil } return context.WithValue(ctx, "auth", username) }) kami.Get("/users/:email/edit", editProfileForm) kami.Post("/users/:email/edit", updateProfile) kami.Serve() }
func TestMethodNotAllowed(t *testing.T) { kami.Reset() kami.Use("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { return context.WithValue(ctx, "ok", true) }) kami.Post("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { panic("test panic") }) kami.MethodNotAllowed(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) }) expectResponseCode(t, "GET", "/test", http.StatusTeapot) }