Skip to content

Commit

Permalink
Merge pull request #1849 from get10101/chore/access-rust-api-from-webapp
Browse files Browse the repository at this point in the history
feat: Add version endpoint
  • Loading branch information
holzeis authored Jan 19, 2024
2 parents c7e5eec + 01e5c44 commit d58b9d7
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions webapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ anyhow = "1"
axum = { version = "0.7", features = ["tracing"] }
mime_guess = "2.0.4"
rust-embed = "8.2.0"
serde = "1.0.147"
serde_json = "1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.5", features = ["fs", "trace"] }
Expand Down
21 changes: 21 additions & 0 deletions webapp/frontend/lib/common/scaffold_with_nav.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:flutter/material.dart';
import 'package:get_10101/common/version_service.dart';

import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class NavigationDestinations {
const NavigationDestinations(this.label, this.icon, this.selectedIcon);
Expand Down Expand Up @@ -32,6 +35,8 @@ class _ScaffoldWithNestedNavigation extends State<ScaffoldWithNestedNavigation>
late bool showNavigationDrawer;
late bool showAsDrawer;

String version = "unknown";

void _goBranch(int index) {
widget.navigationShell.goBranch(
index,
Expand All @@ -46,15 +51,23 @@ class _ScaffoldWithNestedNavigation extends State<ScaffoldWithNestedNavigation>
showAsDrawer = MediaQuery.of(context).size.width >= 1024;
}

@override
void initState() {
super.initState();
context.read<VersionService>().fetchVersion().then((v) => setState(() => version = v));
}

@override
Widget build(BuildContext context) {
final navigationShell = widget.navigationShell;

if (showNavigationDrawer) {
return ScaffoldWithNavigationRail(
body: navigationShell,
selectedIndex: navigationShell.currentIndex,
onDestinationSelected: _goBranch,
showAsDrawer: showAsDrawer,
version: version,
);
} else {
return ScaffoldWithNavigationBar(
Expand Down Expand Up @@ -102,12 +115,14 @@ class ScaffoldWithNavigationRail extends StatelessWidget {
required this.selectedIndex,
required this.onDestinationSelected,
required this.showAsDrawer,
required this.version,
});

final Widget body;
final int selectedIndex;
final ValueChanged<int> onDestinationSelected;
final bool showAsDrawer;
final String version;

@override
Widget build(BuildContext context) {
Expand All @@ -118,6 +133,12 @@ class ScaffoldWithNavigationRail extends StatelessWidget {
extended: showAsDrawer,
selectedIndex: selectedIndex,
onDestinationSelected: onDestinationSelected,
trailing: Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [Text("v$version"), const SizedBox(height: 50)],
),
),
leading: showAsDrawer
? Image.asset("assets/10101_flat_logo.png", width: 200, height: 50)
: Image.asset("assets/10101_logo_icon.png", width: 50, height: 50),
Expand Down
36 changes: 36 additions & 0 deletions webapp/frontend/lib/common/version_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:http/http.dart' as http;
import 'dart:convert';

class Version {
final String version;

const Version({required this.version});

factory Version.fromJson(Map<String, dynamic> json) {
return switch (json) {
{
'version': String version,
} =>
Version(version: version),
_ => throw const FormatException('Failed to load version.'),
};
}
}

class VersionService {
const VersionService();

Future<String> fetchVersion() async {
// TODO(holzeis): this should come from the config
const port = "3001";
const host = "localhost";

final response = await http.get(Uri.http('$host:$port', '/api/version'));

if (response.statusCode == 200) {
return Version.fromJson(jsonDecode(response.body) as Map<String, dynamic>).version;
} else {
return 'unknown';
}
}
}
8 changes: 6 additions & 2 deletions webapp/frontend/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:get_10101/common/version_service.dart';
import 'package:get_10101/routes.dart';
import 'package:provider/provider.dart';

import 'common/color.dart';
import 'common/theme.dart';

void main() {
runApp(const TenTenOneApp());
var providers = [
Provider(create: (context) => const VersionService()),
];
runApp(MultiProvider(providers: providers, child: const TenTenOneApp()));
}

class TenTenOneApp extends StatefulWidget {
Expand Down
3 changes: 0 additions & 3 deletions webapp/frontend/lib/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ final goRouter = GoRouter(
pageBuilder: (context, state) => const NoTransitionPage(
child: TradeScreen(),
),
routes: [],
),
],
),
Expand All @@ -35,7 +34,6 @@ final goRouter = GoRouter(
pageBuilder: (context, state) => const NoTransitionPage(
child: WalletScreen(),
),
routes: [],
),
],
),
Expand All @@ -47,7 +45,6 @@ final goRouter = GoRouter(
pageBuilder: (context, state) => const NoTransitionPage(
child: SettingsScreen(),
),
routes: [],
),
],
)
Expand Down
2 changes: 2 additions & 0 deletions webapp/frontend/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
go_router: ^9.0.0
provider: ^6.0.5
http: ^1.1.0

dev_dependencies:
flutter_test:
Expand Down
13 changes: 13 additions & 0 deletions webapp/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use axum::Json;
use serde::Serialize;

#[derive(Serialize)]
pub struct Version {
version: String,
}

pub async fn version() -> Json<Version> {
Json(Version {
version: env!("CARGO_PKG_VERSION").to_string(),
})
}
4 changes: 4 additions & 0 deletions webapp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
mod api;

use crate::api::version;
use anyhow::Result;
use axum::http::header;
use axum::http::Request;
Expand Down Expand Up @@ -36,6 +39,7 @@ async fn main() -> Result<()> {
fn using_serve_dir() -> Router {
Router::new()
.route("/", get(index_handler))
.route("/api/version", get(version))
.route("/main.dart.js", get(main_dart_handler))
.route("/flutter.js", get(flutter_js))
.route("/index.html", get(index_handler))
Expand Down

0 comments on commit d58b9d7

Please sign in to comment.