feat: implement NPM Proxy Hosts section with reusable entity patterns
Add full Proxy Hosts feature for Control Room: - Data layer: datasource, model with custom JSON converters for NPM API quirks - Domain layer: ProxyHost entity with SSL, caching, websocket support - Presentation: list page, detail/edit page, form widget Add reusable entity page patterns for Control Room sections: - EntityPageScaffold, EntitySection, EntitySettingRow components - EntityForm with create/view/edit mode support - EntityPageModeMixin for consistent mode management - CreateOnlyField for immutable-after-creation fields 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
67d2f777c8
commit
2adb50b352
@@ -0,0 +1,69 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:tatlock_ui/core/api/api_client.dart';
|
||||
import 'package:tatlock_ui/features/control_room/npm/data/models/proxy_host_model.dart';
|
||||
|
||||
part 'proxy_hosts_datasource.g.dart';
|
||||
|
||||
/// Remote data source for NPM proxy host operations via Core API.
|
||||
class ProxyHostsDatasource {
|
||||
ProxyHostsDatasource(this._dio);
|
||||
|
||||
final Dio _dio;
|
||||
|
||||
/// Gets all configured domains from Core API.
|
||||
Future<List<DomainInfoModel>> getDomains() async {
|
||||
final response = await _dio.get<List<dynamic>>(
|
||||
'/infrastructure/domains',
|
||||
);
|
||||
|
||||
return response.data!
|
||||
.map((json) => DomainInfoModel.fromJson(json as Map<String, dynamic>))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Gets detailed proxy host configuration by ID.
|
||||
Future<ProxyHostModel> getProxyHost(int proxyId) async {
|
||||
final response = await _dio.get<Map<String, dynamic>>(
|
||||
'/infrastructure/proxy/$proxyId',
|
||||
);
|
||||
|
||||
return ProxyHostModel.fromJson(response.data!);
|
||||
}
|
||||
|
||||
/// Creates a new proxy host.
|
||||
Future<void> createProxyHost({
|
||||
required List<String> domainNames,
|
||||
required String forwardScheme,
|
||||
required String forwardHost,
|
||||
required int forwardPort,
|
||||
bool sslEnabled = false,
|
||||
}) async {
|
||||
await _dio.post<void>(
|
||||
'/infrastructure/proxy',
|
||||
data: {
|
||||
'domain_names': domainNames,
|
||||
'forward_scheme': forwardScheme,
|
||||
'forward_host': forwardHost,
|
||||
'forward_port': forwardPort,
|
||||
'ssl_enabled': sslEnabled,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// Updates an existing proxy host.
|
||||
Future<void> updateProxyHost(int proxyId, Map<String, dynamic> config) async {
|
||||
await _dio.put<void>(
|
||||
'/infrastructure/proxy/$proxyId',
|
||||
data: config,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides the proxy hosts datasource.
|
||||
@riverpod
|
||||
ProxyHostsDatasource proxyHostsDatasource(Ref ref) {
|
||||
final dio = ref.watch(coreApiClientProvider);
|
||||
return ProxyHostsDatasource(dio);
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:tatlock_ui/features/control_room/npm/domain/entities/proxy_host.dart';
|
||||
|
||||
part 'proxy_host_model.freezed.dart';
|
||||
part 'proxy_host_model.g.dart';
|
||||
|
||||
/// Converts API values that can be either int or bool (false) to int?.
|
||||
/// NPM returns false instead of null for missing IDs.
|
||||
class NullableIntOrBoolConverter implements JsonConverter<int?, dynamic> {
|
||||
const NullableIntOrBoolConverter();
|
||||
|
||||
@override
|
||||
int? fromJson(dynamic json) {
|
||||
if (json == null || json == false) return null;
|
||||
if (json is int) return json;
|
||||
if (json is num) return json.toInt();
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
dynamic toJson(int? object) => object;
|
||||
}
|
||||
|
||||
/// Converts API values that can be either int or bool to int.
|
||||
/// NPM returns true/false for some 0/1 fields.
|
||||
class IntOrBoolConverter implements JsonConverter<int, dynamic> {
|
||||
const IntOrBoolConverter();
|
||||
|
||||
@override
|
||||
int fromJson(dynamic json) {
|
||||
if (json == null) return 0;
|
||||
if (json is bool) return json ? 1 : 0;
|
||||
if (json is int) return json;
|
||||
if (json is num) return json.toInt();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@override
|
||||
dynamic toJson(int object) => object;
|
||||
}
|
||||
|
||||
/// Proxy host data model for API serialization.
|
||||
///
|
||||
/// Maps to the NPM API response format via Core API.
|
||||
@freezed
|
||||
class ProxyHostModel with _$ProxyHostModel {
|
||||
const factory ProxyHostModel({
|
||||
required int id,
|
||||
@JsonKey(name: 'domain_names') required List<String> domainNames,
|
||||
@JsonKey(name: 'forward_scheme') required String forwardScheme,
|
||||
@JsonKey(name: 'forward_host') required String forwardHost,
|
||||
@JsonKey(name: 'forward_port') required int forwardPort,
|
||||
@JsonKey(name: 'ssl_forced') @Default(false) bool sslForced,
|
||||
@NullableIntOrBoolConverter() @JsonKey(name: 'certificate_id') int? certificateId,
|
||||
@IntOrBoolConverter() @Default(1) int enabled,
|
||||
@IntOrBoolConverter() @JsonKey(name: 'http2_support') @Default(0) int http2Support,
|
||||
@IntOrBoolConverter() @JsonKey(name: 'hsts_enabled') @Default(0) int hstsEnabled,
|
||||
@NullableIntOrBoolConverter() @JsonKey(name: 'access_list_id') int? accessListId,
|
||||
@IntOrBoolConverter() @JsonKey(name: 'caching_enabled') @Default(0) int cachingEnabled,
|
||||
@IntOrBoolConverter() @JsonKey(name: 'block_exploits') @Default(0) int blockExploits,
|
||||
@IntOrBoolConverter() @JsonKey(name: 'allow_websocket_upgrade') @Default(0) int allowWebsocketUpgrade,
|
||||
@JsonKey(name: 'created_on') String? createdOn,
|
||||
@JsonKey(name: 'modified_on') String? modifiedOn,
|
||||
@Default([]) List<ProxyLocationModel> locations,
|
||||
}) = _ProxyHostModel;
|
||||
|
||||
const ProxyHostModel._();
|
||||
|
||||
factory ProxyHostModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$ProxyHostModelFromJson(json);
|
||||
|
||||
/// Converts to domain entity.
|
||||
ProxyHost toEntity() {
|
||||
return ProxyHost(
|
||||
id: id,
|
||||
domainNames: domainNames,
|
||||
forwardScheme: forwardScheme,
|
||||
forwardHost: forwardHost,
|
||||
forwardPort: forwardPort,
|
||||
sslEnabled: certificateId != null && certificateId! > 0,
|
||||
certificateId: certificateId,
|
||||
enabled: enabled == 1,
|
||||
http2Support: http2Support == 1,
|
||||
hstsEnabled: hstsEnabled == 1,
|
||||
forceSSL: sslForced,
|
||||
accessListId: accessListId,
|
||||
cacheAssets: cachingEnabled == 1,
|
||||
blockExploits: blockExploits == 1,
|
||||
websocketSupport: allowWebsocketUpgrade == 1,
|
||||
locations: locations.map((l) => l.toEntity()).toList(),
|
||||
createdAt: createdOn != null ? DateTime.tryParse(createdOn!) : null,
|
||||
modifiedAt: modifiedOn != null ? DateTime.tryParse(modifiedOn!) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Proxy location model.
|
||||
@freezed
|
||||
class ProxyLocationModel with _$ProxyLocationModel {
|
||||
const factory ProxyLocationModel({
|
||||
required String path,
|
||||
@JsonKey(name: 'forward_scheme') required String forwardScheme,
|
||||
@JsonKey(name: 'forward_host') required String forwardHost,
|
||||
@JsonKey(name: 'forward_port') required int forwardPort,
|
||||
}) = _ProxyLocationModel;
|
||||
|
||||
const ProxyLocationModel._();
|
||||
|
||||
factory ProxyLocationModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$ProxyLocationModelFromJson(json);
|
||||
|
||||
ProxyLocation toEntity() {
|
||||
return ProxyLocation(
|
||||
path: path,
|
||||
forwardScheme: forwardScheme,
|
||||
forwardHost: forwardHost,
|
||||
forwardPort: forwardPort,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Domain info model for the /infrastructure/domains endpoint.
|
||||
///
|
||||
/// This is a simpler model used for listing domains.
|
||||
@freezed
|
||||
class DomainInfoModel with _$DomainInfoModel {
|
||||
const factory DomainInfoModel({
|
||||
required String domain,
|
||||
required String service,
|
||||
@JsonKey(name: 'proxy_host_id') required int proxyHostId,
|
||||
@JsonKey(name: 'ssl_enabled') @Default(false) bool sslEnabled,
|
||||
@JsonKey(name: 'certificate_id') int? certificateId,
|
||||
}) = _DomainInfoModel;
|
||||
|
||||
factory DomainInfoModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$DomainInfoModelFromJson(json);
|
||||
}
|
||||
Reference in New Issue
Block a user