fix: resolve connectivity issues for Android and Web builds
- Android: Added INTERNET permission and enabled cleartext traffic in AndroidManifest.xml. - Web: Implemented and registered CORSMiddleware in backend to allow cross-origin requests. - Flutter: Updated SettingsProvider to use 10.0.2.2 as default for Android Emulator for easier local testing.
This commit is contained in:
@@ -40,3 +40,20 @@ func AuthMiddleware() gin.HandlerFunc {
|
|||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CORSMiddleware handles cross-origin requests from web clients
|
||||||
|
func CORSMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||||
|
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
|
||||||
|
|
||||||
|
if c.Request.Method == "OPTIONS" {
|
||||||
|
c.AbortWithStatus(204)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ func SetupRouter() *gin.Engine {
|
|||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
// Use CORS middleware to allow web access
|
||||||
|
r.Use(CORSMiddleware())
|
||||||
|
|
||||||
// 清除代理信任警告 "[WARNING] You trusted all proxies"
|
// 清除代理信任警告 "[WARNING] You trusted all proxies"
|
||||||
r.SetTrustedProxies(nil)
|
r.SetTrustedProxies(nil)
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
<application
|
<application
|
||||||
android:label="Hightube"
|
android:label="Hightube"
|
||||||
android:name="${applicationName}"
|
android:name="${applicationName}"
|
||||||
android:icon="@mipmap/launcher_icon">
|
android:icon="@mipmap/launcher_icon"
|
||||||
|
android:usesCleartextTraffic="true">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
class SettingsProvider with ChangeNotifier {
|
class SettingsProvider with ChangeNotifier {
|
||||||
// Default server address for local development.
|
// Use 10.0.2.2 for Android emulator to access host's localhost
|
||||||
// Using 10.0.2.2 for Android emulator or localhost for Desktop.
|
static String get _defaultUrl => (defaultTargetPlatform == TargetPlatform.android && !kIsWeb)
|
||||||
String _baseUrl = "http://localhost:8080";
|
? "http://10.0.2.2:8080"
|
||||||
|
: "http://localhost:8080";
|
||||||
|
|
||||||
|
String _baseUrl = _defaultUrl;
|
||||||
Color _themeColor = Colors.blue;
|
Color _themeColor = Colors.blue;
|
||||||
|
|
||||||
String get baseUrl => _baseUrl;
|
String get baseUrl => _baseUrl;
|
||||||
|
|||||||
Reference in New Issue
Block a user