import ballerina/config;
import ballerina/http;
import ballerina/log;
http:ListenerConfiguration helloWorldEPConfig = {
    secureSocket: {
        keyStore: {
            path: config:getAsString("b7a.home") +
                  "/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
};
listener http:Listener helloWorldEP = new (9095, config = helloWorldEPConfig);@http:ServiceConfig {
    basePath: "/hello"
}
service helloWorld on helloWorldEP {    @http:ResourceConfig {
        methods: ["GET"],
        path: "/"
    }
    resource function sayHello(http:Caller caller, http:Request req) {
        var result = caller->respond("Hello World!");
        if (result is error) {
            log:printError("Error in responding ", result);
        }
    }
}

HTTPS Listener

You can use the HTTPS Listener to connect or interact with an HTTPS client. Provide the Secure Socket configuration to the server to expose an HTTPS connection.

import ballerina/config;
import ballerina/http;
import ballerina/log;
http:ListenerConfiguration helloWorldEPConfig = {
    secureSocket: {
        keyStore: {
            path: config:getAsString("b7a.home") +
                  "/bre/security/ballerinaKeystore.p12",
            password: "ballerina"
        }
    }
};

An HTTP endpoint can be configured to communicate through HTTPS as well. To secure an endpoint using HTTPS, the endpoint needs to be configured with a keystore, a certificate, and a private key for the endpoint.

listener http:Listener helloWorldEP = new (9095, config = helloWorldEPConfig);

Create a listener endpoint.

@http:ServiceConfig {
    basePath: "/hello"
}
service helloWorld on helloWorldEP {
    @http:ResourceConfig {
        methods: ["GET"],
        path: "/"
    }
    resource function sayHello(http:Caller caller, http:Request req) {
        var result = caller->respond("Hello World!");
        if (result is error) {
            log:printError("Error in responding ", result);
        }
    }
}

Send the response back to the caller.

# To start the service, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command along with the Ballerina home path as a config.
$ ballerina run https_listener.bal --b7a.home=<ballerina_home_path>
[ballerina/http] started HTTPS/WSS listener 0.0.0.0:9095
# To invoke the service, execute the below cURL command with the `-k` option.
$ curl -k https://localhost:9095/hello
Hello World!