Ejemplo n.º 1
0
//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)
	}
}
Ejemplo n.º 2
0
func Error(lg quimby.Logger) alice.Constructor {
	return func(h http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			h.ServeHTTP(w, req)
			e := context.Get(req, "error")
			if e != nil {
				lg.Printf("error (%s)\n", e)
				err := e.(error)
				if err.Error() == "not found" {
					w.WriteHeader(http.StatusNotFound)
				} else {
					w.WriteHeader(http.StatusInternalServerError)
				}
				w.Write([]byte(err.Error()))
			}
		})
	}
}
Ejemplo n.º 3
0
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))
	}
}