-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2289 from ballerina-platform/default-listener
Add HTTP default listener
- Loading branch information
Showing
15 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
ballerina-tests/http-advanced-tests/tests/http_default_listener_test.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/http; | ||
import ballerina/http_test_common.service1 as _; | ||
import ballerina/http_test_common.service2 as _; | ||
import ballerina/test; | ||
|
||
listener http:Listener defaultListener = http:getDefaultListener(); | ||
|
||
service /api/v3 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 3!"; | ||
} | ||
} | ||
|
||
service /api/v4 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 4!"; | ||
} | ||
} | ||
|
||
listener http:Listener defaultListenerNew = http:getDefaultListener(); | ||
|
||
service /api/v5 on defaultListenerNew { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 5!"; | ||
} | ||
} | ||
|
||
final http:Client defaultListenerClient = check new(string `localhost:${http:defaultListenerPort}/api`); | ||
|
||
@test:Config {} | ||
function testDefaultListener() returns error? { | ||
foreach int i in 1...5 { | ||
string response = check defaultListenerClient->/[string `v${i}`]/greeting; | ||
test:assertEquals(response, string `Hello, World from service ${i}!`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
ballerina-tests/http-security-tests/tests/http_default_listener_test.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/http; | ||
import ballerina/http_test_common as common; | ||
import ballerina/http_test_common.service1 as _; | ||
import ballerina/http_test_common.service2 as _; | ||
import ballerina/test; | ||
|
||
listener http:Listener defaultListener = http:getDefaultListener(); | ||
|
||
service /api/v3 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 3!"; | ||
} | ||
} | ||
|
||
service /api/v4 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 4!"; | ||
} | ||
} | ||
|
||
listener http:Listener defaultListenerNew = http:getDefaultListener(); | ||
|
||
service /api/v5 on defaultListenerNew { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 5!"; | ||
} | ||
} | ||
|
||
final http:Client defaultListenerClient = check new(string `localhost:${http:defaultListenerPort}/api`, | ||
secureSocket = { | ||
cert: { | ||
path: common:TRUSTSTORE_PATH, | ||
password: "ballerina" | ||
} | ||
}, | ||
httpVersion = "1.1" | ||
); | ||
|
||
@test:Config {} | ||
function testDefaultListenerWithConfiguration() returns error? { | ||
foreach int i in 1...5 { | ||
string response = check defaultListenerClient->/[string `v${i}`]/greeting; | ||
test:assertEquals(response, string `Hello, World from service ${i}!`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Module overview | ||
|
||
This module provides a mock HTTP service - service1. |
26 changes: 26 additions & 0 deletions
26
ballerina-tests/http-test-common/modules/service1/service.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/http; | ||
|
||
listener http:Listener defaultListener = http:getDefaultListener(); | ||
|
||
service /api/v1 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 1!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Module overview | ||
|
||
This module provides a mock HTTP service - service2. |
26 changes: 26 additions & 0 deletions
26
ballerina-tests/http-test-common/modules/service2/service.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/http; | ||
|
||
listener http:Listener defaultListener = http:getDefaultListener(); | ||
|
||
service /api/v2 on defaultListener { | ||
|
||
resource function get greeting() returns string { | ||
return "Hello, World from service 2!"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org). | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
isolated Listener? defaultListener = (); | ||
|
||
# Default HTTP listener port used by the HTTP Default Listener. | ||
# The default value is 9090. | ||
public configurable int defaultListenerPort = 9090; | ||
|
||
# Default HTTP listener configuration used by the HTTP Default Listener. | ||
public configurable ListenerConfiguration defaultListenerConfig = {}; | ||
|
||
# Returns the default HTTP listener. If the default listener is not already created, a new | ||
# listener will be created with the port and configuration. An error will be returned if | ||
# the listener creation fails. | ||
# | ||
# The default listener configuration can be changed in the `Config.toml` file. Example: | ||
# ```toml | ||
# [ballerina.http] | ||
# defaultListenerPort = 8080 | ||
# | ||
# [ballerina.http.defaultListenerConfig] | ||
# httpVersion = "1.1" | ||
# | ||
# [ballerina.http.defaultListenerConfig.secureSocket.key] | ||
# path = "resources/certs/key.pem" | ||
# password = "password" | ||
# ``` | ||
# | ||
# + return - The default HTTP listener or an error if the listener creation fails. | ||
public isolated function getDefaultListener() returns Listener|ListenerError { | ||
lock { | ||
Listener? tempListener = defaultListener; | ||
if tempListener is Listener { | ||
return tempListener; | ||
} | ||
Listener 'listener = check new Listener(defaultListenerPort, defaultListenerConfig); | ||
defaultListener = 'listener; | ||
return 'listener; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters