func BenchmarkRecordPostEvent(b *testing.B) { db := mem.NewDB() u, _, err := user.Create(db, "username", "password") if err != nil { b.Fatalf("user.Create error: %v", err) } ctx := user.NewContext(context.Background(), u) logger := services.NewTestLogger(b) for i := 0; i < b.N; i++ { rec := httptest.NewRecorder() req, err := http.NewRequest( "POST", "http://www.elos.pw/record/?"+url.Values{ "kind": []string{models.EventKind.String()}, }.Encode(), bytes.NewBuffer( []byte(`{ "name": "event name", "data": { "sensor1": 34, "sensor2": 4.3 }, "owner_id": "`+u.ID().String()+`" }`), )) if err != nil { b.Fatalf("http.NewRequest error: %v", err) } routes.RecordPOST(ctx, rec, req, logger, db) } }
func router(ctx context.Context, m *Middleware, s *Services) (http.Handler, context.CancelFunc) { mux := http.NewServeMux() requestBackground, cancelAll := context.WithCancel(ctx) mux.Handle("/app/", http.StripPrefix("/app/", http.FileServer(s.AppFileSystem))) mux.HandleFunc(routes.Index, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": w.Write([]byte("Who is John Galt?")) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/query/ mux.HandleFunc(routes.RecordsQuery, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": routes.Records.QueryGET(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/new/ mux.HandleFunc(routes.RecordsNew, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": routes.Records.NewGET(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/create/ mux.HandleFunc(routes.RecordsCreate, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": routes.Records.CreateGET(ctx, w, r, s.WebUIClient) case "POST": routes.Records.CreatePOST(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/edit/ mux.HandleFunc(routes.RecordsEdit, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": routes.Records.EditGET(ctx, w, r, s.WebUIClient) case "POST": routes.Records.EditPOST(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/view/ mux.HandleFunc(routes.RecordsView, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": routes.Records.ViewGET(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /records/delete/ mux.HandleFunc(routes.RecordsDelete, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": routes.Records.DeletePOST(ctx, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /register/ mux.HandleFunc(routes.Register, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": routes.RegisterPOST(requestBackground, w, r, s.WebUIClient) case "GET": routes.RegisterGET(requestBackground, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /login/ mux.HandleFunc(routes.Login, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": routes.LoginPOST(ctx, w, r, s.WebUIClient) case "GET": routes.LoginGET(requestBackground, w, r, s.WebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /record/ mux.HandleFunc(routes.Record, logRequest(cors(func(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { routes.RecordOPTIONS(requestBackground, w, r) return } ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB) if !ok { return } switch r.Method { case "GET": routes.RecordGET(ctx, w, r, s.Logger, s.DB) case "POST": routes.RecordPOST(ctx, w, r, s.Logger, s.DB) case "DELETE": routes.RecordDELETE(ctx, w, r, s.Logger, s.DB) case "OPTIONS": routes.RecordOPTIONS(ctx, w, r) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }), s.Logger)) // /record/query/ mux.HandleFunc(routes.RecordQuery, logRequest(cors(func(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { routes.RecordOPTIONS(requestBackground, w, r) return } ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB) if !ok { return } switch r.Method { case "POST": routes.RecordQueryPOST(ctx, w, r, s.Logger, s.DB) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }), s.Logger)) // /event/ mux.HandleFunc(routes.Event, logRequest(cors(func(w http.ResponseWriter, r *http.Request) { ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB) if !ok { return } switch r.Method { case "POST": routes.EventPOST(ctx, w, r, s.DB, s.Logger) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }), s.Logger)) // /record/changes/ mux.HandleFunc(routes.RecordChanges, logRequest(websocket.Handler( routes.ContextualizeRecordChangesGET(requestBackground, s.DB, s.Logger), ).ServeHTTP, s.Logger)) // /command/sms/ mux.HandleFunc(routes.CommandSMS, logRequest(func(w http.ResponseWriter, r *http.Request) { ctx := requestBackground switch r.Method { case "POST": routes.CommandSMSPOST(ctx, w, r, s.Logger, s.SMSCommandSessions) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /command/web/ mux.HandleFunc(routes.CommandWeb, logRequest(websocket.Handler( routes.ContextualizeCommandWebGET(requestBackground, s.DB, s.Logger), ).ServeHTTP, s.Logger)) // /mobile/location/ mux.HandleFunc(routes.MobileLocation, logRequest(func(w http.ResponseWriter, r *http.Request) { ctx, ok := routes.Authenticate(requestBackground, w, r, s.Logger, s.DB) if !ok { return } switch r.Method { case "POST": routes.MobileLocationPOST(ctx, w, r, s.Logger, s.DB) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // /cal/week/ mux.HandleFunc(routes.CalWeek, logRequest(func(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": cal.WeekGET(ctx, w, r, s.CalWebUIClient) default: http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } }, s.Logger)) // Handle letsencrypt fs := http.FileServer(http.Dir("/var/www/elos/")) mux.Handle("/.well-known/", logRequest(func(w http.ResponseWriter, r *http.Request) { fs.ServeHTTP(w, r) }, s.Logger)) return mux, cancelAll }