Browse Source

web2: Add dagger module, make port configurable.

pull/436/head
Chris Smith 8 years ago
parent
commit
0549085d84

+ 1
- 0
ui_web2/plugin.config View File

@@ -26,6 +26,7 @@ provides:
26 26
   #web ui
27 27
 
28 28
 defaults:
29
+  port=4567
29 30
 
30 31
 exports:
31 32
 

+ 47
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/WebServer.java View File

@@ -0,0 +1,47 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_web2;
24
+
25
+import spark.Spark;
26
+
27
+/**
28
+ * Web server used by the web UI.
29
+ */
30
+public class WebServer {
31
+
32
+    private final int port;
33
+
34
+    public WebServer(final int port) {
35
+        this.port = port;
36
+    }
37
+
38
+    public void start() {
39
+        Spark.port(port);
40
+        Spark.get("/test", (request, response) -> "HELLO");
41
+    }
42
+
43
+    public void stop() {
44
+        Spark.stop();
45
+    }
46
+
47
+}

+ 75
- 0
ui_web2/src/com/dmdirc/addons/ui_web2/WebUiModule.java View File

@@ -0,0 +1,75 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.ui_web2;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.ClientModule.GlobalConfig;
27
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
28
+import com.dmdirc.plugins.PluginDomain;
29
+import com.dmdirc.plugins.PluginInfo;
30
+
31
+import javax.inject.Singleton;
32
+
33
+import dagger.Module;
34
+import dagger.Provides;
35
+
36
+/**
37
+ * Dagger module that provides Web UI-specific dependencies.
38
+ */
39
+@Module(
40
+        addsTo = ClientModule.class,
41
+        injects = WebServer.class
42
+)
43
+@SuppressWarnings("TypeMayBeWeakened")
44
+public class WebUiModule {
45
+
46
+    private final PluginInfo pluginInfo;
47
+    private final String domain;
48
+
49
+    public WebUiModule(final PluginInfo pluginInfo, final String domain) {
50
+        this.pluginInfo = pluginInfo;
51
+        this.domain = domain;
52
+    }
53
+
54
+    @Provides
55
+    @PluginDomain(WebUiPlugin.class)
56
+    public String getSettingsDomain() {
57
+        return domain;
58
+    }
59
+
60
+    @Provides
61
+    @PluginDomain(WebUiPlugin.class)
62
+    public PluginInfo getPluginInfo() {
63
+        return pluginInfo;
64
+    }
65
+
66
+    @Provides
67
+    @Singleton
68
+    public WebServer getWebServer(
69
+            @PluginDomain(WebUiPlugin.class) final String domain,
70
+            @GlobalConfig final AggregateConfigProvider globalConfig) {
71
+        final int port = globalConfig.getOptionInt(domain, "port");
72
+        return new WebServer(port);
73
+    }
74
+
75
+}

+ 16
- 4
ui_web2/src/com/dmdirc/addons/ui_web2/WebUiPlugin.java View File

@@ -22,26 +22,38 @@
22 22
 
23 23
 package com.dmdirc.addons.ui_web2;
24 24
 
25
+import com.dmdirc.plugins.PluginInfo;
25 26
 import com.dmdirc.plugins.implementations.BasePlugin;
26 27
 
27
-import static spark.Spark.get;
28
-import static spark.Spark.stop;
28
+import dagger.ObjectGraph;
29 29
 
30 30
 /**
31 31
  * Web UI plugin.
32 32
  */
33 33
 public class WebUiPlugin extends BasePlugin {
34 34
 
35
+    private WebServer webServer;
36
+
37
+    @Override
38
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
39
+        super.load(pluginInfo, graph);
40
+
41
+        setObjectGraph(graph.plus(new WebUiModule(pluginInfo, pluginInfo.getDomain())));
42
+        getObjectGraph().validate();
43
+
44
+        webServer = getObjectGraph().get(WebServer.class);
45
+    }
46
+
35 47
     @Override
36 48
     public void onLoad() {
37 49
         super.onLoad();
38
-        get("/test", (request, response) -> "HELLO");
50
+        webServer.start();
39 51
     }
40 52
 
41 53
     @Override
42 54
     public void onUnload() {
43 55
         super.onUnload();
44
-        stop();
56
+        webServer.stop();
45 57
     }
46 58
 
47 59
 }

Loading…
Cancel
Save