Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support connect redis with username and password #66

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions lib/api/redis/redis_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,72 @@
import 'package:redis/redis.dart';

class RedisApi {
static Future<void> set(String host, int port, String username, String password, String key, String value) async {
final conn = RedisConnection();
Command connect = await conn.connect(host, port);
await connect.send_object(["SET", key, value]);
static Future<dynamic> set(String host, int port, String password, String key, String value) async {
final command = await getCommand(host, port, password);
dynamic rs = await command
.send_object(["SET", key, value]).onError((error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static Future<String> get(String host, int port, String username, String password, String key) async {
final conn = RedisConnection();
Command connect = await conn.connect(host, port);
return await connect.send_object(["GET", key]);
static Future<dynamic> get(String host, int port, String password, String key) async {
final command = await getCommand(host, port, password);
dynamic rs =
await command.send_object(["GET", key]).onError((error, stackTrace) => throw Exception('redis error: $error'));
await command.get_connection().close();
return rs;
}

static Future<dynamic> delete(String host, int port, String password, String key) async {
final command = await getCommand(host, port, password);
dynamic rs =
await command.send_object(["DEL", key]).onError((error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static dynamic keys(String host, int port, String password, String patten) async {
final command = await getCommand(host, port, password);
dynamic rs = await command
.send_object(["KEYS", patten]).onError((error, stackTrace) => throw Exception('redis error: $error'));
await command.get_connection().close();
return rs;
}

static Future<dynamic> hGet(String host, int port, String password, String key, String field) async {
final command = await getCommand(host, port, password);
dynamic rs = await command
.send_object(["HGET", key, field]).onError((error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static Future<dynamic> hSet(
String host, int port, String password, String key, String field, String fieldValue) async {
final command = await getCommand(host, port, password);
dynamic rs = await command.send_object(["HSET", key, field, fieldValue]).onError(
(error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static Future<dynamic> hGetAll(String host, int port, String password, String key) async {
final command = await getCommand(host, port, password);
dynamic rs = await command
.send_object(["HGETALL", key]).onError((error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static Future<dynamic> hDel(String host, int port, String password, String key, String field) async {
final command = await getCommand(host, port, password);
dynamic rs = await command
.send_object(["HDEL", key, field]).onError((error, stackTrace) => throw Exception('redis error: $error'));
return rs;
}

static Future<Command> getCommand(String host, int port, String password) async {
final command = await RedisConnection().connect(host, port);
try {
await command.send_object(["AUTH", password]);
} catch (e) {
throw Exception('Password Error Exception: ${e.toString()}');
}
return command;
}
}
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_source_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_tenant_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_topic_view_model.dart';
import 'package:paas_dashboard_flutter/vm/redis/redis_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/redis/redis_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/sql/sql_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/sql/sql_view_model.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -217,6 +218,10 @@ class MyApp extends StatelessWidget {
final args = settings.arguments as PulsarSinkViewModel;
return RouteGen.pulsarSink(args);
}
if (settings.name == PageRouteConst.RedisInstance) {
final args = settings.arguments as RedisInstanceViewModel;
return RouteGen.redisInstance(args);
}
if (settings.name == PageRouteConst.SqlExecute) {
final args = settings.arguments as SqlViewModel;
return RouteGen.sqlExecute(args);
Expand Down
6 changes: 2 additions & 4 deletions lib/module/redis/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
//

class RedisConst {
static const String defaultHost = "localhost";
static const String defaultIp = "localhost";
static const int defaultPort = 6379;
static const String defaultAddr = "localhost:6379";
static const String defaultUsername = "";
static const String defaultPassword = "";
static const String defaultPassword = "123456";
}
10 changes: 5 additions & 5 deletions lib/persistent/persistent_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ class PersistentDb implements PersistentApi {
'CREATE TABLE code_list(id INTEGER PRIMARY KEY, name TEXT, code TEXT)',
);
await db.execute(
'CREATE TABLE redis_instances(id INTEGER PRIMARY KEY, name TEXT, addr TEXT, username TEXT, password TEXT)',
'CREATE TABLE redis_instances(id INTEGER PRIMARY KEY, name TEXT, ip TEXT, port INTEGER, password TEXT)',
);
await db.execute(
'INSERT INTO redis_instances(name, addr, username, password) VALUES ("example", "${RedisConst.defaultAddr}", "${RedisConst.defaultUsername}", "${RedisConst.defaultPassword}")',
'INSERT INTO redis_instances(name, ip, port, password) VALUES ("example", "${RedisConst.defaultIp}", ${RedisConst.defaultPort}, "${RedisConst.defaultPassword}")',
);
}

Expand Down Expand Up @@ -407,7 +407,7 @@ class PersistentDb implements PersistentApi {
Future<void> saveRedis(String name, String addr, String username, String password) async {
var aux = await getInstance();
var list = [name, addr, username, password];
aux.database.execute('INSERT INTO redis_instances(name, addr, username, password) VALUES (?, ?, ?, ?)', list);
aux.database.execute('INSERT INTO redis_instances(name, ip, port, password) VALUES (?, ?, ?, ?)', list);
}

@override
Expand All @@ -422,7 +422,7 @@ class PersistentDb implements PersistentApi {
final List<Map<String, dynamic>> maps = await aux.database.query('redis_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return RedisInstancePo(aux['id'], aux['name'], aux['addr'], aux['username'], aux['password']);
return RedisInstancePo(aux['id'], aux['name'], aux['ip'], aux['port'], aux['password']);
});
}

Expand All @@ -435,6 +435,6 @@ class PersistentDb implements PersistentApi {
return null;
}
var current = maps[0];
return RedisInstancePo(current['id'], current['name'], current['addr'], current['username'], current['password']);
return RedisInstancePo(current['id'], current['name'], current['ip'], current['port'], current['password']);
}
}
5 changes: 2 additions & 3 deletions lib/persistent/persistent_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,14 @@ class PersistentMemory implements PersistentApi {

Future<List<RedisInstancePo>> redisInstances() async {
return [
new RedisInstancePo(0, "example", RedisConst.defaultAddr, RedisConst.defaultUsername, RedisConst.defaultPassword)
new RedisInstancePo(0, "example", RedisConst.defaultIp, RedisConst.defaultPort, RedisConst.defaultPassword)
];
}

Future<RedisInstancePo?> redisInstance(String name) async {
if (name != "example") {
return null;
}
return new RedisInstancePo(
0, "example", RedisConst.defaultAddr, RedisConst.defaultUsername, RedisConst.defaultPassword);
return new RedisInstancePo(0, "example", RedisConst.defaultIp, RedisConst.defaultPort, RedisConst.defaultPassword);
}
}
12 changes: 6 additions & 6 deletions lib/persistent/po/redis_instance_po.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
class RedisInstancePo {
final int id;
final String name;
final String addr;
final String username;
final String ip;
final int port;
final String password;

RedisInstancePo(this.id, this.name, this.addr, this.username, this.password);
RedisInstancePo(this.id, this.name, this.ip, this.port, this.password);

RedisInstancePo deepCopy() {
return new RedisInstancePo(id, name, addr, username, password);
return new RedisInstancePo(id, name, ip, port, password);
}

Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'addr': addr,
'username': username,
'ip': ip,
'port': port,
'password': password,
};
}
Expand Down
10 changes: 10 additions & 0 deletions lib/route/route_gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_sink.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_source.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/ui/redis/widget/redis_instance_dart.dart';
import 'package:paas_dashboard_flutter/ui/sql/screen/sql_execute_screen.dart';
import 'package:paas_dashboard_flutter/vm/code/code_view_model.dart';
import 'package:paas_dashboard_flutter/vm/mongo/mongo_database_view_model.dart';
Expand All @@ -53,6 +54,7 @@ import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_sink_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_source_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_tenant_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_topic_view_model.dart';
import 'package:paas_dashboard_flutter/vm/redis/redis_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/sql/sql_view_model.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -195,6 +197,14 @@ class RouteGen {
));
}

static Route redisInstance(RedisInstanceViewModel viewModel) {
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: RedisInstanceWidget(),
));
}

static Route sqlExecute(SqlViewModel viewModel) {
// deep copy view model
return MaterialPageRoute(
Expand Down
16 changes: 8 additions & 8 deletions lib/ui/redis/redis_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class _RedisPageState extends State<RedisPage> {
columns: [
DataColumn(label: Text('Id')),
DataColumn(label: Text('Name')),
DataColumn(label: Text('Addr')),
DataColumn(label: Text('Username')),
DataColumn(label: Text('IP')),
DataColumn(label: Text('Port')),
DataColumn(label: Text('Delete instance')),
],
rows: vm.instances
Expand All @@ -81,8 +81,8 @@ class _RedisPageState extends State<RedisPage> {
cells: [
DataCell(Text(itemRow.id.toString())),
DataCell(Text(itemRow.name)),
DataCell(Text(itemRow.addr)),
DataCell(Text(itemRow.username)),
DataCell(Text(itemRow.ip)),
DataCell(Text(itemRow.port.toString())),
DataCellUtil.newDelDataCell(() {
vm.deleteRedis(itemRow.id);
}),
Expand All @@ -103,12 +103,12 @@ class _RedisPageState extends State<RedisPage> {
final vm = Provider.of<RedisInstanceListViewModel>(context, listen: false);
var list = [
FormFieldDef('Instance Name'),
FormFieldDef('Addr'),
FormFieldDef('Username'),
FormFieldDef('IP'),
FormFieldDef('Port'),
FormFieldDef('Password'),
];
return FormUtil.createButton4("Redis Instance", list, context, (name, addr, username, password) {
vm.createRedis(name, addr, username, password);
return FormUtil.createButton4("Redis Instance", list, context, (name, ip, port, password) {
vm.createRedis(name, ip, port, password);
});
}
}
Loading