diff --git a/Makefile b/Makefile index 9527fd5..e837f0a 100644 --- a/Makefile +++ b/Makefile @@ -2,5 +2,5 @@ windows: @echo "Building for Windows..." - go build -ldflags="-X polaris/db.Version=$(git describe --tags --long)" -buildmode=c-shared -o ui/windows/libpolaris.dll ./cmd/binding + go build -tags c -ldflags="-X polaris/db.Version=$(git describe --tags --long)" -buildmode=c-shared -o ui/windows/libpolaris.dll ./cmd/binding cd ui && flutter build windows \ No newline at end of file diff --git a/cmd/binding/main.go b/cmd/binding/main.go index a5e3f83..92c71dd 100644 --- a/cmd/binding/main.go +++ b/cmd/binding/main.go @@ -1,11 +1,21 @@ package main import "C" -import "polaris/cmd" +import ( + "os" + "polaris/cmd" + "polaris/log" +) func main() {} //export Start func Start() { - cmd.Start() + cmd.Start(true) +} + +//export Stop +func Stop() { + log.Infof("stop polaris") + os.Exit(0) } diff --git a/cmd/doc.go b/cmd/doc.go index c5818c0..eada3fe 100644 --- a/cmd/doc.go +++ b/cmd/doc.go @@ -1,15 +1,20 @@ package cmd import ( + "os" "polaris/db" "polaris/log" "polaris/server" ) +func Start(sharedLib bool) { + if sharedLib || os.Getenv("GIN_MODE") == "release" { + log.InitLogger(true) + } else { + log.InitLogger(false) + } -func Start() { log.Infof("------------------- Starting Polaris ---------------------") - dbClient, err := db.Open() if err != nil { log.Panicf("init db error: %v", err) diff --git a/cmd/polaris/polaris.go b/cmd/polaris/polaris.go index cd964ee..81053ee 100644 --- a/cmd/polaris/polaris.go +++ b/cmd/polaris/polaris.go @@ -3,5 +3,5 @@ package main import "polaris/cmd" func main() { - cmd.Start() + cmd.Start(false) } \ No newline at end of file diff --git a/log/log.go b/log/log.go index 180bf83..5a816a7 100644 --- a/log/log.go +++ b/log/log.go @@ -15,12 +15,12 @@ var atom zap.AtomicLevel const dataPath = "./data" -func init() { +func InitLogger(toFile bool) { atom = zap.NewAtomicLevel() atom.SetLevel(zap.DebugLevel) w := zapcore.Lock(os.Stdout) - if os.Getenv("GIN_MODE") == "release" { + if toFile { w = zapcore.AddSync(&lumberjack.Logger{ Filename: filepath.Join(dataPath, "logs", "polaris.log"), MaxSize: 50, // megabytes diff --git a/server/systems.go b/server/systems.go index de9a606..46687b5 100644 --- a/server/systems.go +++ b/server/systems.go @@ -20,7 +20,7 @@ type LogFile struct { func (s *Server) GetAllLogs(c *gin.Context) (interface{}, error) { fs, err := os.ReadDir(db.LogPath) if err != nil { - return nil, errors.Wrap(err, "read log dir") + return []LogFile{}, nil } var logs []LogFile for _, f := range fs { diff --git a/ui/embed.go b/ui/embed.go index ff5a42f..e729db2 100644 --- a/ui/embed.go +++ b/ui/embed.go @@ -1,3 +1,4 @@ +//go:build !c package ui import "embed" diff --git a/ui/embed_c.go b/ui/embed_c.go new file mode 100644 index 0000000..50e8271 --- /dev/null +++ b/ui/embed_c.go @@ -0,0 +1,6 @@ +//go:build c +package ui + +import "embed" + +var Web embed.FS \ No newline at end of file diff --git a/ui/lib/ffi/backend.dart b/ui/lib/ffi/backend.dart new file mode 100644 index 0000000..e0c66b7 --- /dev/null +++ b/ui/lib/ffi/backend.dart @@ -0,0 +1,41 @@ +import 'dart:ffi'; +import 'dart:io'; +import 'dart:isolate'; + +import 'package:ui/widgets/utils.dart' as Utils; + + +class FFIBackend { + final lib = DynamicLibrary.open(libname()); + + static String libname() { + if (Utils.isDesktop()) { + if (Platform.isWindows) { + return 'libpolaris.dll'; + } else if (Platform.isLinux) { + return 'libpolaris.so'; + } else if (Platform.isMacOS) { + return 'libpolaris.dylib'; + } else { + throw UnsupportedError( + 'Unsupported platform: ${Platform.operatingSystem}'); + } + } else { + return ""; + } + } + + Future start() async { + var s = lib + .lookup>('Start') + .asFunction(); + + return Isolate.run(s); + } + Future stop() async { + var s = lib + .lookup>('Stop') + .asFunction(); + return s(); + } +} diff --git a/ui/lib/main.dart b/ui/lib/main.dart index e4b187a..464c1e3 100644 --- a/ui/lib/main.dart +++ b/ui/lib/main.dart @@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:ui/activity.dart'; +import 'package:ui/ffi/backend.dart'; import 'package:ui/init_wizard.dart'; import 'package:ui/login_page.dart'; import 'package:ui/movie_watchlist.dart'; @@ -15,7 +16,11 @@ import 'package:ui/tv_details.dart'; import 'package:ui/welcome_page.dart'; import 'package:ui/widgets/utils.dart'; -void main() { +void main() async { + if (isDesktop()) { + FFIBackend().start(); + } + initializeDateFormatting() .then((_) => runApp(const ProviderScope(child: MyApp()))); } diff --git a/ui/lib/providers/APIs.dart b/ui/lib/providers/APIs.dart index b2191d6..a14b28a 100644 --- a/ui/lib/providers/APIs.dart +++ b/ui/lib/providers/APIs.dart @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:ui/providers/server_response.dart'; +import 'package:ui/widgets/utils.dart'; class APIs { static final _baseUrl = baseUrl(); @@ -68,6 +69,9 @@ class APIs { static String baseUrl() { if (kReleaseMode) { + if (isDesktop()) { + return "http://127.0.0.1:8080"; + } return ""; } return "http://127.0.0.1:8080"; diff --git a/ui/pubspec.lock b/ui/pubspec.lock index 280213b..5e2f329 100644 --- a/ui/pubspec.lock +++ b/ui/pubspec.lock @@ -97,6 +97,14 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.3.2" + ffi: + dependency: "direct main" + description: + name: ffi + sha256: "289279317b4b16eb2bb7e271abccd4bf84ec9bdcbe999e278a94b804f5630418" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.4" flutter: dependency: "direct main" description: flutter @@ -547,5 +555,5 @@ packages: source: hosted version: "1.1.1" sdks: - dart: ">=3.7.0-0 <4.0.0" + dart: ">=3.7.0 <4.0.0" flutter: ">=3.27.0" diff --git a/ui/pubspec.yaml b/ui/pubspec.yaml index 6638065..09126b9 100644 --- a/ui/pubspec.yaml +++ b/ui/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: flutter: sdk: flutter dio: ^5.7.0 + ffi: ^2.0.0 # The following adds the Cupertino Icons font to your application. diff --git a/ui/windows/CMakeLists.txt b/ui/windows/CMakeLists.txt index 6bffb3d..bdfbe6b 100644 --- a/ui/windows/CMakeLists.txt +++ b/ui/windows/CMakeLists.txt @@ -81,6 +81,9 @@ install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR} install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" COMPONENT Runtime) +install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/libpolaris.dll" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + if(PLUGIN_BUNDLED_LIBRARIES) install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}"