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 main() { c := cron.New() c.AddFunc("5 * * * * *", Hoge) // 毎分5秒 c.AddFunc("*/5 * * * * *", Fuga) // 5秒毎 c.Start() isRunning := true kami.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte("OK")) }) kami.Get("/start", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { if !isRunning { c.Start() isRunning = true } w.Write([]byte("START OK")) }) kami.Get("/stop", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { if isRunning { c.Stop() isRunning = false } w.Write([]byte("STOP OK")) }) defer func() { if isRunning { c.Stop() isRunning = false } }() 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 main() { flag.Parse() switch flag.Arg(0) { case "index": search.Index(flag.Arg(1)) case "search": search.OpenIndex() result, err := search.Search(flag.Arg(1)) if err != nil { fmt.Println(err) return } fmt.Println(result) case "server": search.OpenIndex() kami.Get("/search/:keyword", searchHandler) kami.Serve() } }
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() { 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 main() { ctx := context.Background() kami.Context = ctx kami.PanicHandler = PanicHandler kami.Get("/", index) kami.Serve() }
func main() { ctx := context.Background() kami.Context = ctx kami.PanicHandler = PanicHandler kami.Get("/", index) rollbar.Message(rollbar.INFO, "test") kami.Serve() }
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 BenchmarkStaticRoute(b *testing.B) { kami.Reset() kami.Get("/hello", noop) for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/hello", nil) kami.Handler().ServeHTTP(resp, req) if resp.Code != 200 { panic(resp.Code) } } }
func BenchmarkParameter(b *testing.B) { kami.Reset() kami.Get("/hello/:name", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { kami.Param(ctx, "name") }) req, _ := http.NewRequest("GET", "/hello/bob", nil) b.ResetTimer() for n := 0; n < b.N; n++ { resp := httptest.NewRecorder() kami.Handler().ServeHTTP(resp, req) } }
func main() { cpus := runtime.NumCPU() runtime.GOMAXPROCS(cpus) log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) ctx := context.Background() ctx = NewContext(ctx) ctx = WithAuthCallbackFunc(ctx, func(ctx context.Context, w http.ResponseWriter, r *http.Request, token *oauth2.Token) { fmt.Println(token.AccessToken) }) ctx = WithAuthErrorFunc(ctx, func(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) { fmt.Println(err) w.Write([]byte(err.Error())) }) kami.Context = ctx kami.Get("/login/paypal", LoginPayPal) kami.Get("/auth/paypal/callback", AuthPayPalCallback) kami.Get("/payment/create", PaymentCreate) kami.Get("/payment/done", PaymentDone) kami.Get("/paypal/payment/execute", PayPalPaymentExecute) kami.Get("/paypal/payment/cancel", PayPalPaymentCancel) log.Println("Starting server...") log.Println("GOMAXPROCS: ", cpus) kami.Serve() }
func BenchmarkParameter5(b *testing.B) { kami.Reset() kami.Get("/:a/:b/:c/:d/:e", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { for _, v := range []string{"a", "b", "c", "d", "e"} { kami.Param(ctx, v) } }) req, _ := http.NewRequest("GET", "/a/b/c/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(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) } }
func TestCloseHandler(t *testing.T) { called := false closeHandler := func(ctx context.Context, r *http.Request) { called = true } kami.Reset() kami.CloseHandler = closeHandler kami.Get("/test", func(ctx context.Context, w http.ResponseWriter, r *http.Request) { t.Log("TestCloseHandler") }) expectResponseCode(t, "GET", "/test", http.StatusOK) if called != true { t.Fatal("expected closeHandler to be called") } }
// 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 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 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() { dbcon := PrePareDB() tmpls := template.Must(template.ParseGlob("./template/*.html")) ctx := context.Background() ctx = db.WithSQL(ctx, "main", dbcon) ctx = view.NewContext(ctx, tmpls) // dbを閉じる defer db.CloseSQLAll(ctx) // 神コンテキスト! kami.Context = ctx kami.Get("/", todo.Index) kami.Post("/create", todo.Create) kami.Post("/finish", todo.Finish) kami.Post("/delete", todo.Delete) kami.Serve() }
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 main() { kami.Get("/contacts", getContacts) kami.Serve() }
func main() { kami.Get("/", web.Form) kami.Post("/keyring", web.KeychainHandler) kami.Serve() }
func main() { flag.Set("bind", ":1234") kami.Get("/hello/:name", hello) kami.Serve() }