Skip to content

Commit

Permalink
Properly wait for local HTTP port to become ready
Browse files Browse the repository at this point in the history
  • Loading branch information
schnatterer committed Jun 21, 2020
1 parent 81fd22a commit 1cbef1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ For the whole process to work, your container requires the following packages:
* openssl and
* curl

Your tomcat server must be configured to serve static content from `/static` in order for to be able to answer to the
letsencrypt challenges.
In addition, it must serve traffic via port 80, in order to succeed in letsencrypt's http-01 challenge.
Your tomcat server must be configured to
* serve static content from `/static/.well-known/acme-challenge` on `http://${DOMAIN}/static/.well-known/acme-challenge`
in order for to be able to answer to the letsencrypt challenges,
* serve traffic via port 80 (externally), in order to succeed in letsencrypt's http-01 challenge,
* respond with HTTP return code less than 400, on `http://localhost:${LOCAL_HTTP_PORT}/` (default port 8080).

If successful, the certificate files will be stored here:
* Certificate file: `/certs/${DOMAIN}/cert.pem`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;

public class Main {

Expand All @@ -21,17 +26,28 @@ public static void main(String[] args) throws Exception {
// Without this call the connector seems not to start
tomcat.getConnector();

serveStaticContentFrom(tomcat, "/static");
Context ctx = tomcat.addContext("", new File("/static").getAbsolutePath());

Tomcat.addServlet(ctx, "HelloServlet", new HttpServlet() {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)throws IOException {
Writer w = resp.getWriter();
w.write("Hello Embedded Tomcat.\n");
w.flush();
w.close();
}
});
ctx.addServletMappingDecoded("", "HelloServlet");

serveStaticContentFrom(ctx);

ReloadingTomcatConnectorFactory.addHttpsConnector(tomcat, HTTPS_PORT, PK, CRT, CA);

tomcat.start();
tomcat.getServer().await();
}

private static void serveStaticContentFrom(Tomcat tomcat, String docbase) {
Context ctx = tomcat.addContext("", new File(docbase).getAbsolutePath());

private static void serveStaticContentFrom(Context ctx) {
Wrapper defaultServlet = ctx.createWrapper();
defaultServlet.setName("default");
defaultServlet.setServletClass("org.apache.catalina.servlets.DefaultServlet");
Expand Down
6 changes: 3 additions & 3 deletions meta-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ function fetchCerts() {
fi

green "Waiting for tomcat to become ready on localhost:${LOCAL_HTTP_PORT}"
while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:${LOCAL_HTTP_PORT})" -ge 500 ]]; do sleep 1; done
green "Tomcat is ready."
until $(curl -s -o /dev/null --head --fail localhost:${LOCAL_HTTP_PORT}); do sleep 1; done
green "Tomcat is ready for letsencrypt"

trap 'SIG_INT_RECEIVED="true" && green "Stopping certificate process"' INT

SIG_INT_RECEIVED='false'

while [[ "${SIG_INT_RECEIVED}" == 'false' ]]; do
green "Trying to fetch certificates"
green "Fetching certificates"
dehydrated --domain ${DOMAIN} --cron --accept-terms --out ${CERT_DIR} && exitCode=$? || exitCode=$?
if [[ "${exitCode}" > 0 ]]; then
red "Fetching certificates failed"
Expand Down

0 comments on commit 1cbef1f

Please sign in to comment.