Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 16f7259

Browse files
committed
Added start and stop to the REST API.
1 parent 3441e78 commit 16f7259

File tree

6 files changed

+95
-2
lines changed

6 files changed

+95
-2
lines changed

src/main/scala/org/codeoverflow/chatoverflow/instance/PluginInstance.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ class PluginInstance(val instanceName: String, pluginType: PluginType) extends W
164164
if (plugin.get.getLoopInterval > 0) {
165165
while (!threadStopAfterNextIteration) {
166166
plugin.get.loop()
167+
// FIXME: This is not a loop interval. Should be responsive to the loop runtime
167168
Thread.sleep(plugin.get.getLoopInterval)
168169
}
169170
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/DTOs.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ object DTOs {
77

88
case class PluginInstance(instanceName: String, pluginName: String, pluginAuthor: String, isRunning: Boolean, requirementIDs: Seq[String])
99

10+
case class PluginInstanceName(instanceName: String)
11+
1012
case class ConfigInfo(name: String, apiMajorVersion: Int, apiMinorVersion: Int, pluginFolderPath: String,
1113
configFolderPath: String, requirementPackage: String, pluginDataPath: String)
1214

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/config/ConfigController.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import org.codeoverflow.chatoverflow.Launcher
44
import org.codeoverflow.chatoverflow.api.APIVersion
55
import org.codeoverflow.chatoverflow.configuration.CryptoUtil
66
import org.codeoverflow.chatoverflow.ui.web.JsonServlet
7-
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConfigInfo, Password, ResultMessage}
7+
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{AuthKey, ConfigInfo, Password, ResultMessage}
88
import org.scalatra.swagger._
99

1010
class ConfigController(implicit val swagger: Swagger) extends JsonServlet with ConfigControllerDefinition {
@@ -24,6 +24,24 @@ class ConfigController(implicit val swagger: Swagger) extends JsonServlet with C
2424
}
2525
}
2626

27+
// Is this even a thing?
28+
post("/exit", operation(postExit)) {
29+
parsedAs[AuthKey] {
30+
case AuthKey(authKey) =>
31+
if (authKey != chatOverflow.credentialsService.generateAuthKey()) {
32+
ResultMessage(success = false, "Wrong auth key.")
33+
34+
} else {
35+
// Give enough time to return success. Then bye bye
36+
new Thread(() => {
37+
Thread.sleep(500)
38+
System.exit(0)
39+
}).start()
40+
ResultMessage(success = true)
41+
}
42+
}
43+
}
44+
2745
get("/login", operation(getLogin)) {
2846
chatOverflow.isLoaded
2947
}

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/config/ConfigControllerDefinition.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.codeoverflow.chatoverflow.ui.web.rest.config
22

3-
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{ConfigInfo, Password, ResultMessage}
3+
import org.codeoverflow.chatoverflow.ui.web.rest.DTOs.{AuthKey, ConfigInfo, Password, ResultMessage}
44
import org.scalatra.swagger.SwaggerSupport
55
import org.scalatra.swagger.SwaggerSupportSyntax.OperationBuilder
66

@@ -16,6 +16,13 @@ trait ConfigControllerDefinition extends SwaggerSupport {
1616
summary "Triggers the saving process of the framework manually (if loaded previously)."
1717
description "Triggers saving of credentials and configuration. Should not be needed manually.")
1818

19+
val postExit: OperationBuilder =
20+
(apiOperation[ResultMessage]("postExit")
21+
summary "Shuts the framework down."
22+
description "Shutdown the framework in the next second if a correct auth key is supplied."
23+
parameter bodyParam[AuthKey]("body").description("Requires the run specific auth key."))
24+
25+
1926
val getLogin: OperationBuilder =
2027
(apiOperation[Boolean]("getLogin")
2128
summary "Returns if the framework is already loaded."

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/plugin/PluginInstanceController.scala

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,59 @@ class PluginInstanceController(implicit val swagger: Swagger) extends JsonServle
2323
chatOverflow.pluginInstanceRegistry.getPluginInstance(instanceName).map(pluginInstanceToDTO)
2424
}
2525

26+
post("/start", operation(startInstance)) {
27+
parsedAs[PluginInstanceName] {
28+
case PluginInstanceName(instanceName) =>
29+
30+
if (!chatOverflow.isLoaded) {
31+
ResultMessage(success = false, "Framework not loaded.")
32+
33+
} else {
34+
val pluginInstance = chatOverflow.pluginInstanceRegistry.getPluginInstance(instanceName)
35+
36+
if (pluginInstance.isEmpty) {
37+
ResultMessage(success = false, "Plugin instance not found.")
38+
39+
} else if (pluginInstance.get.isRunning) {
40+
ResultMessage(success = false, "Plugin instance already running.")
41+
42+
} else if (!pluginInstance.get.getRequirements.isComplete) {
43+
ResultMessage(success = false, "Not all required requirements have been set.")
44+
45+
} else if (!pluginInstance.get.start()) {
46+
ResultMessage(success = false, "Unable to start plugin.")
47+
48+
} else {
49+
ResultMessage(success = true)
50+
}
51+
}
52+
}
53+
}
54+
55+
post("/stop", operation(stopInstance)) {
56+
parsedAs[PluginInstanceName] {
57+
case PluginInstanceName(instanceName) =>
58+
59+
if (!chatOverflow.isLoaded) {
60+
ResultMessage(success = false, "Framework not loaded.")
61+
62+
} else {
63+
val pluginInstance = chatOverflow.pluginInstanceRegistry.getPluginInstance(instanceName)
64+
65+
if (pluginInstance.isEmpty) {
66+
ResultMessage(success = false, "Plugin instance not found.")
67+
68+
} else if (!pluginInstance.get.isRunning) {
69+
ResultMessage(success = false, "Plugin instance is not running.")
70+
71+
} else {
72+
pluginInstance.get.stopPlease()
73+
ResultMessage(success = true)
74+
}
75+
}
76+
}
77+
}
78+
2679
get("/:instanceName/requirements", operation(getRequirements)) {
2780
val instanceName = params("instanceName")
2881
val returnSeq = ListBuffer[Requirement]()

src/main/scala/org/codeoverflow/chatoverflow/ui/web/rest/plugin/PluginInstanceControllerDefinition.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ trait PluginInstanceControllerDefinition extends SwaggerSupport {
5757
description "Removes a plugin instance specified by its name, if possible."
5858
parameter pathParam[String]("instanceName").description("The name of the plugin instance."))
5959

60+
val startInstance: OperationBuilder =
61+
(apiOperation[ResultMessage]("startInstance")
62+
summary "Starts a specific plugin instance."
63+
description "Starts a specific plugin instance if possible."
64+
parameter bodyParam[PluginInstanceName]("body").description("Requires the name of the plugin instance."))
65+
66+
val stopInstance: OperationBuilder =
67+
(apiOperation[ResultMessage]("stopInstance")
68+
summary "Stops a specific plugin instance."
69+
description "Requires the stop of a specified plugin instance. This can take up to one loop interval."
70+
parameter bodyParam[PluginInstanceName]("body").description("Requires the name of the plugin instance."))
71+
6072
override protected def applicationDescription: String = "Handles plugin instances and requirements."
6173

6274
}

0 commit comments

Comments
 (0)