aboutsummaryrefslogtreecommitdiffstats
path: root/examples/ColorPalette/Program.cs
blob: 6f0b9401289270941c18f7869f42602b6a5c45e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/***************************************************************************************************
 Copyright (C) 2025 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

using System.Diagnostics;
using System.Reflection;
using Qt.Quick;

namespace ColorPalette
{
    internal class Program
    {
        private static bool UseServer { get; set; } = true;

        private static Process Server { get; set; }

        private static void StartServer()
        {
            if (!UseServer || Server != null)
                return;
            try {
                var serverPath = Path.Combine(
                    Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location),
                    "server", "colorpaletteserver.exe");
                if (!File.Exists(serverPath))
                    return;
                Server = Process.Start(new ProcessStartInfo()
                {
                    FileName = serverPath,
                    CreateNoWindow = true,
                    UseShellExecute = false,
                });
            } catch (Exception e) {
                Debug.WriteLine($"Error starting server: {e.Message}");
            }
        }

        private static void StopServer()
        {
            if (Server == null)
                return;
            Server.Kill();
            Server.WaitForExit();
        }

        public static void Main(string[] args)
        {
            if (args.Contains("--log-requests"))
                RestService.LogRequests = true;
            if (args.Contains("--no-server"))
                UseServer = false;

            PaginatedResource.RegisterResourceType<ColorResource>();
            PaginatedResource.RegisterResourceType<UserResource>();

            StartServer();

            Qml.LoadFromRootModule("Main");
            Qml.WaitForExit();

            StopServer();
        }
    }
}