how to handle too many endpoints azure functions

2023-02-24T12:08:25.6633333+00:00

I have an application written in Go, Gin, and libpq. This app has too many endpoints, and I am having problems. I deployed the application on azure functions, the problem is that for each endpoint I need to create a folder with the ENDPOINT NAME + the function.json file, thus generating my endpoint.. the problem is that I have many endpoints, is there any way to register all my endpoints in just one function.json file?

My project is divided like this:

routes: routes.go main.go

my main.go looks like this:

//tem toda minha parte de configuração
    routes.ConfiguracaoSistema(Router)
    routes.ConfiguracaoModulo(Router)
    routes.ModuloSistema(Router)
    routes.ArvoreMenu(Router)
    routes.ModuloArvoreMenu(Router)
    //tem toda minha parte de Usuario
    routes.Usuario(Router)
    routes.EmpresaUsuario(Router)
    routes.CampoTabelaUsuario(Router)
    routes.ProgramaUsuario(Router)
    routes.FuncionalidadeProgramaUsuario(Router)
    routes.TabelaUsuario(Router)
    routes.ParametroFiltroTabelaUsuario(Router)
    //meu servidor de autenticação
    routes.JWTAutentication(Router)
    handler := azfunc.HTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        setupRouter().ServeHTTP(w, r)
    })
    azfunc.Main(handler)
    listenAddr := ":1000"
    if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
        listenAddr = ":" + val
    }
    Router.Run(listenAddr)
}

my PACKAGE ROUTES (IT IS A SEPARATE PACKGE FROM THE MAIN) is like this

routeS: campotabela.go: package routes



I have 10 more of these files, with different names..

how do i have just one function.json file and register all my endpoints in it?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,239 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,421 Reputation points
    2023-03-03T10:31:45.7866667+00:00

    @Guilherme Rodrigues Silva de Jesus

    In Azure Functions, you can register multiple endpoints in a single function using the function.json file. You need to define multiple input and output bindings in the function.json file for each of your endpoints.

    Here's an example of how you can structure your function.json file to register multiple endpoints:

    {
      "bindings": [
        {
          "name": "req1",
          "type": "httpTrigger",
          "direction": "in",
          "methods": [ "get" ],
          "route": "endpoint1/{id}"
        },
        {
          "name": "res1",
          "type": "http",
          "direction": "out"
        },
        {
          "name": "req2",
          "type": "httpTrigger",
          "direction": "in",
          "methods": [ "post" ],
          "route": "endpoint2"
        },
        {
          "name": "res2",
          "type": "http",
          "direction": "out"
        },
        ...
      ]
    }
    
    

    In this example, we have defined two endpoints: endpoint1 and endpoint2. We have defined two input bindings (req1 and req2) and two output bindings (res1 and res2) for each endpoint.

    You can add more input and output bindings to register more endpoints. You can also specify the HTTP method and route for each endpoint using the methods and route properties.

    Once you have defined your function.json file, you can create a single Azure Function to handle all your endpoints. In your Go code, you can use the http.Request object to determine which endpoint was called and route the request to the appropriate handler function.

    Here's an example of how you can handle multiple endpoints in a single Azure Function:

    func handleRequest(w http.ResponseWriter, r *http.Request) {
      switch r.URL.Path {
      case "/endpoint1":
        handleEndpoint1(w, r)
      case "/endpoint2":
        handleEndpoint2(w, r)
      ...
      default:
        http.NotFound(w, r)
      }
    }
    
    

    In this example, we are using a switch statement to route the request to the appropriate handler function based on the URL path. You can define your own routing logic based on your application's needs.

    With this approach, you can reduce the number of function apps you need to deploy and manage, and make your code more organized and easier to maintain.