Skip to content

Commit

Permalink
Added ability to configure the context it runs from
Browse files Browse the repository at this point in the history
  • Loading branch information
danielflower committed Mar 2, 2018
1 parent 0761df7 commit 9a9ce59
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 15 deletions.
3 changes: 3 additions & 0 deletions docker/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ restabuild.data=data

# The number of builds that can run at the same time
restabuild.concurrent.builds=3

# The path to serve the website from
restabuild.context=restabuild
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<dependency>
<groupId>io.muserver</groupId>
<artifactId>mu-server</artifactId>
<version>0.2.6</version>
<version>0.3.1</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
Expand Down
3 changes: 3 additions & 0 deletions sample-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ restabuild.data=target/local

# The number of builds that can run at the same time
restabuild.concurrent.builds=1

# The path to serve the website from
restabuild.context=restabuild
4 changes: 3 additions & 1 deletion src/main/java/com/danielflower/restabuild/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.danielflower.restabuild.build.BuildQueue;
import com.danielflower.restabuild.web.BuildResource;
import com.danielflower.restabuild.web.WebServer;
import io.muserver.Mutils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -43,7 +44,8 @@ public void start() {
buildQueue.start();

BuildResource buildResource = new BuildResource(fileSandbox, buildQueue, database);
webServer = WebServer.start(appRunnerPort, buildResource);
String context = Mutils.trim(config.get(Config.CONTEXT, "restabuild"), "/");
webServer = WebServer.start(appRunnerPort, context, buildResource);
}

private void deleteOldTempFiles(File tempDir) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/danielflower/restabuild/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public class Config {
static final String SERVER_PORT = "restabuild.port";
public static final String DATA_DIR = "restabuild.data";
public static final String CONTEXT = "restabuild.context";
public static final String CONCURRENT_BUILDS = "restabuild.concurrent.builds";

public static Config load(String[] commandLineArgs) throws IOException {
Expand Down Expand Up @@ -45,7 +46,7 @@ private Config(Map<String, String> raw) {
this.raw = raw;
}

private String get(String name, String defaultVal) {
public String get(String name, String defaultVal) {
return raw.getOrDefault(name, defaultVal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

@Path("/restabuild/api/v1/builds")
@Path("api/v1/builds")
public class BuildResource {

private final FileSandbox fileSandbox;
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/danielflower/restabuild/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,24 @@ private WebServer(MuServer server) {
this.server = server;
}

public static WebServer start(int port, BuildResource buildResource) {
public static WebServer start(int port, String context, BuildResource buildResource) {
MuServer server = muServer()
.withHttpConnection(port)
.addHandler((request, response) -> {
log.info(request.toString());
if (request.uri().getPath().equals("/")) {
response.redirect("/restabuild/");
response.redirect("/" + context + "/");
return true;
}
return false;
})
.addHandler(new CORSFilter())
.addHandler(ResourceHandler
.fileOrClasspath("src/main/resources/web", "/web")
.withPathToServeFrom("/restabuild"))
.addHandler(RestHandlerBuilder.create(buildResource))
.addHandler(ContextHandlerBuilder.context(context,
new CORSFilter(),
RestHandlerBuilder.create(buildResource),
ResourceHandler.fileOrClasspath("src/main/resources/web", "/web").build()))
.start();

log.info("Started web server at " + server.uri());
log.info("Started web server at " + server.uri().resolve("/" + context + "/"));
return new WebServer(server);
}

Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h1>Restabuild</h1>
<h2>Submit a build</h2>
<p>Enter the URL of your git repo that has a <code>build.sh</code> file in the root of the directory. Tip: make the
script executable with <code>git update-index --chmod=+x build.sh</code></p>
<form id="createForm" action="/restabuild/api/v1/builds" method="post">
<form id="createForm" action="api/v1/builds" method="post">
<input type="hidden" name="responseType" value="html">
<label>
Git URL:
Expand All @@ -43,14 +43,17 @@ <h2>Submit a build</h2>

<h2>View builds</h2>

<p>Use the API at <a href="/restabuild/api/v1/builds">/restabuild/api/v1/builds</a></p>
<p>Use the API at <a href="api/v1/builds" id="apiLink">api/v1/builds</a></p>

</body>
<script>
document.addEventListener('DOMContentLoaded', function () {
var cc = document.getElementById('curlCommand');
var form = document.getElementById('createForm');
var urlBox = document.getElementById('gitUrlBox');
var path = location.pathname;
var apiLink = document.getElementById('apiLink');
apiLink.textContent = apiLink.href;

if ('URLSearchParams' in window) {
var qs = new URLSearchParams(location.search);
Expand All @@ -62,7 +65,7 @@ <h2>View builds</h2>
cc.textContent = 'curl -X POST -d \'gitUrl=' + encodeURIComponent(value || 'URL-Encoded-Git-URL')
+ '\' -H \'Content-Type: application/x-www-form-urlencoded\' '
+ form.action;
history.replaceState(null, null, value ? '/restabuild/?url=' + encodeURIComponent(value) : '/restabuild/');
history.replaceState(null, null, value ? encodeURI(path) + '?url=' + encodeURIComponent(value) : encodeURI(path));
};
urlBox.addEventListener('input', update);
update();
Expand Down

0 comments on commit 9a9ce59

Please sign in to comment.