{"id":15893605,"url":"https://github.com/alexdlaird/java-ngrok-example-dropwizard","last_synced_at":"2025-03-20T14:30:35.785Z","repository":{"id":40505464,"uuid":"399196106","full_name":"alexdlaird/java-ngrok-example-dropwizard","owner":"alexdlaird","description":"Dropwizard example for java-ngrok","archived":false,"fork":false,"pushed_at":"2024-04-29T11:00:07.000Z","size":162,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-02T05:50:25.667Z","etag":null,"topics":["dropwizard","education","java","java-ngrok","localhost","localtunnel","ngrok","reverse-proxy","tunnel","tunneling","webhook"],"latest_commit_sha":null,"homepage":"https://github.com/alexdlaird/java-ngrok","language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexdlaird.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"alexdlaird","patreon":"alexdlaird","custom":["https://www.paypal.me/alexdlaird"]}},"created_at":"2021-08-23T17:43:14.000Z","updated_at":"2024-05-18T00:35:49.986Z","dependencies_parsed_at":"2023-01-22T04:50:33.465Z","dependency_job_id":"22bcdcf9-3540-409e-8e17-10a2b2f0f5f7","html_url":"https://github.com/alexdlaird/java-ngrok-example-dropwizard","commit_stats":null,"previous_names":[],"tags_count":7,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdlaird%2Fjava-ngrok-example-dropwizard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdlaird%2Fjava-ngrok-example-dropwizard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdlaird%2Fjava-ngrok-example-dropwizard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdlaird%2Fjava-ngrok-example-dropwizard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexdlaird","download_url":"https://codeload.github.com/alexdlaird/java-ngrok-example-dropwizard/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244629983,"owners_count":20484292,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dropwizard","education","java","java-ngrok","localhost","localtunnel","ngrok","reverse-proxy","tunnel","tunneling","webhook"],"created_at":"2024-10-06T08:12:15.706Z","updated_at":"2025-03-20T14:30:35.515Z","avatar_url":"https://github.com/alexdlaird.png","language":"Java","readme":"[![Build](https://img.shields.io/github/actions/workflow/status/alexdlaird/java-ngrok-example-dropwizard/build.yml)](https://github.com/alexdlaird/java-ngrok-example-dropwizard/actions/workflows/build.yml)\n![GitHub License](https://img.shields.io/github/license/alexdlaird/java-ngrok-example-dropwizard)\n\n# java-ngrok Example - Dropwizard\n\nThis is an example project that shows how to easily integrate [`java-ngrok`](https://github.com/alexdlaird/java-ngrok)\nwith [Dropwizard](https://www.dropwizard.io/en/latest/index.html).\n\n## Configuration\n\nCreate\na [`NgrokConfiguration`](https://github.com/alexdlaird/java-ngrok-example-dropwizard/blob/main/src/main/java/com/github/alexdlaird/conf/NgrokConfiguration.java)\nclass that lets us use the config to enable `ngrok` and pass it some useful parameters.\n\n```java\npublic class NgrokConfiguration {\n    @JsonProperty\n    private boolean enabled = false;\n\n    @JsonProperty\n    private String region;\n\n    public boolean isEnabled() {\n        return enabled;\n    }\n\n    public String getRegion() {\n        return region;\n    }\n}\n```\n\nThen wire this class as a `JsonProperty`\nto [the Dropwizard Configuration for our Application](https://www.dropwizard.io/en/latest/getting-started.html#creating-a-configuration-class).\n\n```java\npublic class JavaNgrokExampleDropwizardConfiguration extends Configuration {\n    @JsonProperty\n    private String environment = \"dev\";\n\n    @JsonProperty(\"ngrok\")\n    private NgrokConfiguration ngrokConfiguration;\n\n    public String getEnvironment() {\n        return environment;\n    }\n\n    public NgrokConfiguration getNgrokConfiguration() {\n        return ngrokConfiguration;\n    }\n}\n```\n\nAnd pass parameters to our Dropwizard application through\n[our config file](https://github.com/alexdlaird/java-ngrok-example-dropwizard/blob/main/config.yml):\n\n```yaml\nngrok:\n  enabled: true\n```\n\n## Application Integration\n\nIf `ngrok.enabled` config flag is set, we want to initialize `java-ngrok` when Dropwizard is booting. An easy place to do\nthis is in the `run()` method of [the Application](https://github.com/alexdlaird/java-ngrok-example-dropwizard/blob/main/src/main/java/com/github/alexdlaird/JavaNgrokExampleDropwizardApplication.java).\n\n```java\npublic class JavaNgrokExampleDropwizardApplication extends Application\u003cJavaNgrokExampleDropwizardConfiguration\u003e {\n\n    private static final Logger LOGGER = Logger.getLogger(String.valueOf(JavaNgrokExampleDropwizardApplication.class));\n\n    @Override\n    public void run(final JavaNgrokExampleDropwizardConfiguration configuration,\n                    final Environment environment) {\n        // java-ngrok will only be installed, and should only ever be initialized, in a dev environment\n        if (configuration.getEnvironment().equals(\"dev\") \u0026\u0026\n                configuration.getNgrokConfiguration().isEnabled() \u0026\u0026\n                isNotBlank(System.getenv(\"NGROK_AUTHTOKEN\"))) {\n            final JavaNgrokConfig javaNgrokConfig = new JavaNgrokConfig.Builder()\n                    .withRegion(nonNull(configuration.getNgrokConfiguration().getRegion()) ? Region.valueOf(configuration.getNgrokConfiguration().getRegion().toUpperCase()) : null)\n                    .build();\n            final NgrokClient ngrokClient = new NgrokClient.Builder()\n                    .withJavaNgrokConfig(javaNgrokConfig)\n                    .build();\n\n            final int port = getPort(configuration);\n\n            final CreateTunnel createTunnel = new CreateTunnel.Builder()\n                    .withAddr(port)\n                    .build();\n            final Tunnel tunnel = ngrokClient.connect(createTunnel);\n\n            LOGGER.info(String.format(\"ngrok tunnel \\\"%s\\\" -\u003e \\\"http://127.0.0.1:%d\\\"\", tunnel.getPublicUrl(), port));\n\n            // Update any base URLs or webhooks to use the public ngrok URL\n            initWebhooks(tunnel.getPublicUrl());\n        }\n    }\n\n    private int getPort(JavaNgrokExampleDropwizardConfiguration configuration) {\n        final Stream\u003cConnectorFactory\u003e connectors = configuration.getServerFactory() instanceof DefaultServerFactory\n                ? ((DefaultServerFactory) configuration.getServerFactory()).getApplicationConnectors().stream()\n                : Stream.of((SimpleServerFactory) configuration.getServerFactory()).map(SimpleServerFactory::getConnector);\n\n        return connectors.filter(connector -\u003e connector.getClass().isAssignableFrom(HttpConnectorFactory.class))\n                .map(connector -\u003e (HttpConnectorFactory) connector)\n                .mapToInt(HttpConnectorFactory::getPort)\n                .findFirst()\n                .orElseThrow(IllegalStateException::new);\n    }\n\n    private void initWebhooks(final String publicUrl) {\n        // Update inbound traffic via APIs to use the public-facing ngrok URL\n    }\n\n    // ... The rest of our Dropwizard application\n}\n```\n\nNow Dropwizard can be started by the usual means, setting `ngrok.enabled` in the config to open a tunnel.\n\n1. Run `make build` to build the application\n1. Start application with `java -jar target/java-ngrok-example-dropwizard-1.0.0-SNAPSHOT.jar server src/main/resources/config.yml`\n1. Check the logs for the `ngrok` tunnel's public URL, which should tunnel to  `http://localhost:8080`\n","funding_links":["https://github.com/sponsors/alexdlaird","https://patreon.com/alexdlaird","https://www.paypal.me/alexdlaird"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdlaird%2Fjava-ngrok-example-dropwizard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdlaird%2Fjava-ngrok-example-dropwizard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdlaird%2Fjava-ngrok-example-dropwizard/lists"}