Allow cors
This commit is contained in:
parent
170ad213fc
commit
60f1cb0313
4 changed files with 49 additions and 42 deletions
|
@ -16,4 +16,5 @@ tokio = { version = "1.32.0", features=["macros", "rt-multi-thread"] }
|
|||
chrono = "0.4.31"
|
||||
actix-web = "4.4.0"
|
||||
actix-files = "0.6.2"
|
||||
midea = { git = "https://gavania.de/hodasemi/Midea.git" }
|
||||
midea = { git = "https://gavania.de/hodasemi/Midea.git" }
|
||||
actix-cors = "0.6.4"
|
|
@ -11,13 +11,19 @@ class Category {
|
|||
List<Device> devices;
|
||||
|
||||
static Future<List<Category>> fetch(String base_url) async {
|
||||
final response = await http.get(Uri.parse("$base_url/devices"));
|
||||
print("fetch: http://$base_url/devices");
|
||||
|
||||
final response = await http.get(Uri.parse("http://$base_url/devices"));
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception("Failed to fetch devices");
|
||||
}
|
||||
|
||||
final List<Category> categories = [];
|
||||
|
||||
print("debug print");
|
||||
print(response.body);
|
||||
|
||||
final Map<String, List<List<dynamic>>> json = jsonDecode(response.body);
|
||||
|
||||
for (MapEntry<String, List<List<dynamic>>> category_entry in json.entries) {
|
||||
|
@ -35,6 +41,8 @@ class Category {
|
|||
throw Exception("Failed to fetch plug_state for $device_id");
|
||||
}
|
||||
|
||||
print(response.body);
|
||||
|
||||
final Map<String, dynamic> device_state = jsonDecode(response.body);
|
||||
|
||||
final Device device = Device(
|
||||
|
|
|
@ -59,46 +59,37 @@ class MyHomePage extends StatefulWidget {
|
|||
class _MyHomePageState extends State<MyHomePage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<Category>>(
|
||||
future: Category.fetch("smart.gavania.de"),
|
||||
builder: (context, AsyncSnapshot<List<Category>> categories) {
|
||||
if (!categories.hasData) {
|
||||
return const Text("still loading");
|
||||
}
|
||||
final Future<List<Category>> fut = Category.fetch("smart.gavania.de");
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
// TRY THIS: Try changing the color here to a specific color (to
|
||||
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
|
||||
// change color while the other colors stay the same.
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
// Here we take the value from the MyHomePage object that was created by
|
||||
// the App.build method, and use it to set our appbar title.
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
// Center is a layout widget. It takes a single child and positions it
|
||||
// in the middle of the parent.
|
||||
child: Column(
|
||||
// Column is also a layout widget. It takes a list of children and
|
||||
// arranges them vertically. By default, it sizes itself to fit its
|
||||
// children horizontally, and tries to be as tall as its parent.
|
||||
//
|
||||
// Column has various properties to control how it sizes itself and
|
||||
// how it positions its children. Here we use mainAxisAlignment to
|
||||
// center the children vertically; the main axis here is the vertical
|
||||
// axis because Columns are vertical (the cross axis would be
|
||||
// horizontal).
|
||||
//
|
||||
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
|
||||
// action in the IDE, or press "p" in the console), to see the
|
||||
// wireframe for each widget.
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: categories.data!
|
||||
.map((category) => CategoryWidget(category: category))
|
||||
.toList(),
|
||||
),
|
||||
));
|
||||
});
|
||||
List<Widget> children = List.empty();
|
||||
|
||||
fut.then((categories) {
|
||||
children = categories
|
||||
.map((category) => CategoryWidget(category: category))
|
||||
.toList();
|
||||
});
|
||||
|
||||
// return FutureBuilder<List<Category>>(
|
||||
// future: Category.fetch("smart.gavania.de"),
|
||||
// builder: (context, AsyncSnapshot<List<Category>> categories) {
|
||||
// if (!categories.hasData) {
|
||||
// return const Text("still loading");
|
||||
// }
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
// children: categories.data!
|
||||
// .map((category) => CategoryWidget(category: category))
|
||||
// .toList(),
|
||||
children: children,
|
||||
),
|
||||
));
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ mod midea_helper;
|
|||
mod tasmota;
|
||||
mod web_server;
|
||||
|
||||
use actix_cors::Cors;
|
||||
use actix_files::Files;
|
||||
use actix_web::{web::Data, App, HttpServer};
|
||||
use anyhow::Result;
|
||||
|
@ -60,7 +61,13 @@ async fn run_web_server(
|
|||
println!("Starting server on http://{IP}:{PORT}");
|
||||
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::default()
|
||||
.allow_any_origin()
|
||||
.allow_any_method()
|
||||
.allow_any_header();
|
||||
|
||||
App::new()
|
||||
.wrap(cors)
|
||||
.app_data(Data::new(devices.clone()))
|
||||
.app_data(Data::new(db.clone()))
|
||||
.app_data(Data::new(plugs.clone()))
|
||||
|
|
Loading…
Reference in a new issue