Search code examples
ballerina

Annotation 'ServiceConfig' is not allowed on var error


I have created the following service.

import ballerina/sql;
import ballerina/http;
import ballerinax/mysql;
import ballerinax/mysql.driver as _;
import ballerina/log;

configurable int api_port = 8085;
configurable int api_port_sec = 9090;
configurable string host = ?;//"localhost";
configurable int port = ?;//33063;
configurable string user = ?;
configurable string password = ?;
configurable string database = ?;

public type Brewery record {|..|};

http:JwtValidatorConfig config = {
    issuer: "https://api.asgardeo.io/t/xxxx/oauth2/token",
    audience: "E2Bk9......................JpyJjwa",
    signatureConfig: {
        jwksConfig: {url: "https://api.asgardeo.io/t/xxx/oauth2/jwks"}
    }
};
 
listener http:Listener securedEP = new (api_port_sec,
    secureSocket = {
        key: {
            certFile: "./resources/public.crt",
            keyFile: "./resources/private.key"
        }
    }
);

@http:ServiceConfig {
    auth: [
        {
            jwtValidatorConfig: config
        }
    ]
}

final mysql:Client mysqlClient = check new (host = host, port = port, user = user, password = password, database = database);

service / on securedEP {
    function init() {
        log:printInfo("API started", host = "0.0.0.0", port = api_port_sec, protocol = "HTTPS");
    }

    # Description
    # Get Brewery by state
    # + state - Parameter Description
    # + return - Return Value Description
    isolated resource function get brewery/state/[string state]() returns vBrewery[]|http:NotFound|error? {.....}

}

Here, I have a MySQL client and a service running on an http listener, securedEP.

However, when trying to run this project, I am getting the following compilation error.

ERROR [main.bal:(91:5,97:6)] annotation 'ballerina/http:2.4.5:ServiceConfig' is not allowed on var.

I am running this on Ballerina Swan Lake Update 2 (2201.2.3).


Solution

  • The http:ServiceConfig annotation should be attached to the service.

    @http:ServiceConfig {
        auth: [
            {
                jwtValidatorConfig: config
            }
        ]
    }
    service / on securedEP {
    

    Right now, it is specified on the mysqlClient variable declaration.