示例#1
0
文件: web.go 项目: cswank/quimby
func (s *Server) startServer() {
	r := rex.New("main")
	r.Get("/gadgets", http.HandlerFunc(s.status))
	r.Put("/gadgets", http.HandlerFunc(s.update))
	r.Post("/gadgets", http.HandlerFunc(s.update))
	r.Get("/gadgets/values", http.HandlerFunc(s.values))
	r.Get("/gadgets/locations/{location}/devices/{device}/status", http.HandlerFunc(s.deviceValue))
	if s.isMaster {
		r.Post("/clients", http.HandlerFunc(s.setClient))
		r.Get("/clients", http.HandlerFunc(s.getClients))
		r.Delete("/clients", http.HandlerFunc(s.removeClient))
	}

	s.lg.Printf("listening on 0.0.0.0:%d\n", s.port)

	srv := &http.Server{
		Addr:         fmt.Sprintf(":%d", s.port),
		Handler:      r,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 10 * time.Second,
	}

	err := srv.ListenAndServe()
	if err != nil {
		s.lg.Fatal(err)
	}
}
示例#2
0
文件: main.go 项目: cswank/rex
func main() {
	r := rex.New("example")
	r.Get("/things", http.HandlerFunc(getThings))
	r.Get("/things/{thing}", http.HandlerFunc(getThing))
	r.ServeFiles(http.FileServer(http.Dir("./www")))

	http.Handle("/", r)

	log.Println(http.ListenAndServe(":8888", r))
}
示例#3
0
文件: bench_test.go 项目: cswank/rex
func BenchmarkRex(b *testing.B) {
	r := rex.New("bench")
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
	r.Get("/v1/{v1}", handler)

	request, _ := http.NewRequest("GET", "/v1/anything", nil)
	for i := 0; i < b.N; i++ {
		r.ServeHTTP(nil, request)
	}
}
示例#4
0
文件: main.go 项目: cswank/quimby
//This is the endpoint that the gadgets report to. It is
//served on a separate port so it doesn't have to be exposed
//publicly if the main port is exposed.
func startInternal(iRoot string, db *bolt.DB, lg quimby.Logger, port string) {
	r := rex.New("internal")
	r.Post("/internal/updates", getMiddleware(handlers.Write, handlers.RelayMessage))
	r.Post("/internal/gadgets/{id}/sources/{name}", getMiddleware(handlers.Write, handlers.AddDataPoint))

	chain := alice.New(handlers.Auth(db, lg, "internal"), handlers.FetchGadget()).Then(r)

	http.Handle(iRoot, chain)
	a := fmt.Sprintf(":%s", port)
	lg.Printf("listening on %s", a)
	err := http.ListenAndServe(a, chain)
	if err != nil {
		log.Fatal(err)
	}
}
示例#5
0
文件: main.go 项目: cswank/quimby
func start(db *bolt.DB, port, internalPort, root string, iRoot string, lg quimby.Logger, clients *quimby.ClientHolder, tfa quimby.TFAer) {

	quimby.Clients = clients
	quimby.DB = db
	quimby.LG = lg
	handlers.DB = db
	handlers.LG = lg
	handlers.TFA = tfa

	go startInternal(iRoot, db, lg, internalPort)
	go startHomeKit(db, lg)

	r := rex.New("main")
	r.Get("/", getMiddleware(handlers.Read, webapp.IndexPage))
	r.Get("/gadgets/{id}", getMiddleware(handlers.Read, webapp.GadgetPage))
	r.Get("/gadgets/{id}/method.html", getMiddleware(handlers.Read, webapp.EditMethodPage))
	r.Get("/gadgets/{id}/chart.html", getMiddleware(handlers.Read, webapp.ChartPage))
	r.Get("/gadgets/{id}/chart-setup.html", getMiddleware(handlers.Read, webapp.ChartSetupPage))
	r.Get("/gadgets/{id}/chart-setup/{name}", getMiddleware(handlers.Read, webapp.ChartInputPage))
	r.Get("/login.html", getMiddleware(handlers.Anyone, webapp.LoginPage))
	r.Post("/login.html", getMiddleware(handlers.Anyone, webapp.LoginForm))
	r.Get("/logout.html", getMiddleware(handlers.Read, webapp.LogoutPage))
	r.Get("/links.html", getMiddleware(handlers.Read, webapp.LinksPage))
	r.Get("/admin.html", getMiddleware(handlers.Admin, webapp.AdminPage))
	r.Get("/admin/confirmation", getMiddleware(handlers.Admin, webapp.DeleteConfirmPage))
	r.Get("/admin/gadgets/{gadgetid}", getMiddleware(handlers.Admin, webapp.GadgetEditPage))
	r.Post("/admin/gadgets/{gadgetid}", getMiddleware(handlers.Admin, webapp.GadgetForm))
	r.Delete("/admin/gadgets/{gadgetid}", getMiddleware(handlers.Admin, webapp.DeleteGadgetPage))
	r.Get("/admin/users/{username}", getMiddleware(handlers.Admin, webapp.UserEditPage))
	r.Get("/admin/users/{username}/password", getMiddleware(handlers.Admin, webapp.UserPasswordPage))
	r.Post("/admin/users/{username}/password", getMiddleware(handlers.Admin, webapp.UserChangePasswordPage))
	r.Post("/admin/users/{username}/tfa", getMiddleware(handlers.Admin, webapp.UserTFAPage))
	r.Delete("/admin/users/{username}", getMiddleware(handlers.Admin, webapp.DeleteUserPage))
	r.Post("/admin/users/{username}", getMiddleware(handlers.Admin, webapp.UserForm))

	//api
	r.Post("/api/login", http.HandlerFunc(handlers.Login))
	r.Post("/api/logout", http.HandlerFunc(handlers.Logout))
	r.Get("/api/ping", getMiddleware(handlers.Read, handlers.Ping))
	r.Get("/api/currentuser", getMiddleware(handlers.Read, handlers.GetCurrentUser))
	r.Get("/api/users", getMiddleware(handlers.Admin, handlers.GetUsers))
	r.Post("/api/users", getMiddleware(handlers.Admin, handlers.AddUser))
	r.Delete("/api/users/{username}", getMiddleware(handlers.Admin, handlers.DeleteUser))
	r.Post("/api/users/{username}/permission", getMiddleware(handlers.Admin, handlers.UpdateUserPermission))
	r.Post("/api/users/{username}/password", getMiddleware(handlers.Admin, handlers.UpdateUserPassword))
	r.Get("/api/users/{username}", getMiddleware(handlers.Admin, handlers.GetUser))
	r.Get("/api/gadgets", getMiddleware(handlers.Read, handlers.GetGadgets))
	r.Post("/api/gadgets", getMiddleware(handlers.Read, handlers.AddGadget))
	r.Get("/api/gadgets/{id}", getMiddleware(handlers.Read, handlers.GetGadget))
	r.Post("/api/gadgets/{id}", getMiddleware(handlers.Write, handlers.UpdateGadget))
	r.Delete("/api/gadgets/{id}", getMiddleware(handlers.Write, handlers.DeleteGadget))
	r.Post("/api/gadgets/{id}/command", getMiddleware(handlers.Write, handlers.SendCommand))
	r.Post("/api/gadgets/{id}/method", getMiddleware(handlers.Write, handlers.SendMethod))
	r.Get("/api/gadgets/{id}/websocket", getMiddleware(handlers.Write, handlers.Connect))
	r.Get("/api/gadgets/{id}/values", getMiddleware(handlers.Read, handlers.GetUpdates))
	r.Get("/api/gadgets/{id}/status", getMiddleware(handlers.Read, handlers.GetStatus))
	r.Post("/api/gadgets/{id}/notes", getMiddleware(handlers.Write, handlers.AddNote))
	r.Get("/api/gadgets/{id}/notes", getMiddleware(handlers.Read, handlers.GetNotes))
	r.Get("/api/gadgets/{id}/locations/{location}/devices/{device}/status", getMiddleware(handlers.Read, handlers.GetDevice))
	r.Post("/api/gadgets/{id}/locations/{location}/devices/{device}/status", getMiddleware(handlers.Write, handlers.UpdateDevice))
	r.Get("/api/gadgets/{id}/sources", getMiddleware(handlers.Read, handlers.GetDataPointSources))
	r.Get("/api/gadgets/{id}/sources/{name}", getMiddleware(handlers.Read, handlers.GetDataPoints))
	r.Get("/api/gadgets/{id}/sources/{name}/csv", getMiddleware(handlers.Read, handlers.GetDataPointsCSV))
	r.Get("/api/beer/{name}", getMiddleware(handlers.Read, handlers.GetRecipe))
	r.Get("/api/admin/clients", getMiddleware(handlers.Admin, handlers.GetClients))

	r.ServeFiles(http.FileServer(box.HTTPBox()))

	chain := alice.New(handlers.Auth(db, lg, "main"), handlers.FetchGadget(), handlers.Error(lg)).Then(r)
	http.Handle(root, chain)

	addr := fmt.Sprintf("%s:%s", iface, port)
	lg.Printf("listening on %s\n", addr)
	if keyPath == "" {
		lg.Println(http.ListenAndServe(addr, chain))
	} else {
		lg.Println(http.ListenAndServeTLS(fmt.Sprintf("%s:443", iface), certPath, keyPath, chain))
	}
}
示例#6
0
文件: rex_test.go 项目: cswank/rex
			tmp string
		)
		BeforeEach(func() {
			var err error
			tmp, err = ioutil.TempDir("", "")
			Expect(err).To(BeNil())
			Expect(ioutil.WriteFile(path.Join(tmp, "hello.html"), []byte("hello, world!"), 777)).To(BeNil())
			method = ""
			data = ""

			pals := http.HandlerFunc(func(ww http.ResponseWriter, rr *http.Request) {
				method = rr.Method
				ww.Write([]byte("pals"))
			})

			r = rex.New("my other router")
			r.Get("/pals", pals)

			r.ServeFiles(http.FileServer(http.Dir(tmp)))
			http.Handle("/", r)

			w = httptest.NewRecorder()
		})

		AfterEach(func() {
			os.RemoveAll(tmp)
		})

		It("gets a file", func() {
			var err error
			req, err = http.NewRequest("GET", "/hello.html", nil)