Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created an Example HTTP Function #1

Merged
merged 3 commits into from
Nov 24, 2021
Merged

Created an Example HTTP Function #1

merged 3 commits into from
Nov 24, 2021

Conversation

aneeshsharma
Copy link
Contributor

Created a basic HTTP function that returns a message when called. We can build the backend on top of this from hereon. There is one thing that needs to be noted - the go build had to be done to output a static binary because when compiling normally, there is a glibc version mismatch. I haven't looked into how to fix this issue yet but it might not be necessary to fix and we can simply use the static build as the deployment.

@aneeshsharma aneeshsharma self-assigned this Nov 19, 2021
Copy link
Contributor

@daltonfury42 daltonfury42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to look for a good folder structure and found this project: https://github.com/koltyakov/az-fun-go-sp

Can you check if this is possible?

Idea is that all things related to AzureFunctions should go to AzureFunctions, and outside of it, there should be minimal references to it. Say, tomorrow if we decide to host on AWS Lambda, it should hopefully require us to create another folder and add AWS plumbing there.

HttpExample/function.json Outdated Show resolved Hide resolved
handler.go Outdated Show resolved Hide resolved
host.json Outdated Show resolved Hide resolved
.funcignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
handler.go Outdated Show resolved Hide resolved
@maaverik
Copy link
Contributor

Just to clarify, is the high-level plan to port all the logic from Java as-is and replace the current AWS setup with Azure serverless?

@aneeshsharma
Copy link
Contributor Author

@daltonfury42 I will look into the folder structure.
Will update you soon.

@daltonfury42
Copy link
Contributor

Just to clarify, is the high-level plan to port all the logic from Java as-is and replace the current AWS setup with Azure serverless?

Yes, move the entire backend from Java/Postgres to Serverless/MongoDB.

We will try to keep the business logic independent of mongo and azure.

@maaverik
Copy link
Contributor

@daltonfury42 I will look into the folder structure. Will update you soon.

A slightly different take on the folder structure. This seems to be something a lot of people agree on based on the stars. Following it strictly seems too much, but I feel like using this as a guideline is a good idea since a lot of routing, CI scripts and DB logic will come in later. Adding a go.mod file is also a standard I believe for dependency management.

@daltonfury42
Copy link
Contributor

daltonfury42 commented Nov 20, 2021 via email

@aneeshsharma
Copy link
Contributor Author

For Azure Functions, the structure has to be like -

| /MyQueueFunction
|   function.json
|
| host.json
| local.settings.json
| handler.exe

Ref: https://docs.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers#application-structure

So, we can have this inside of AzureFunction/

The source for the handler can be inside AzureFunction/internal

So it can look like

| /MyQueueFunction
|   function.json
| /internal/handler/
|   main.go
|   token.go
| host.json
| local.settings.json
| handler.exe

The host.json takes the path to the executable which is our custom handler. So, the source doesn't necessarily have to be together with the Azure Functions configuration files (just the executable is required). I think it is also possible to have the go application just be a normal HTTP server application and then when we build the application, we can simply place all the configuration files and the handler executable inside a single directory.

This can make sure we are not just limited to azure functions and if needed it would be possible to deploy the application as a standalone executable or as a container.

So, finally it would be like

/internal/handler
    main.go
    token.go
/build
/AzureFunction
    /MyQueueFunciton
        function.json
    host.json
    .
    .
Makefile
go.mod
.
.

@daltonfury42
Copy link
Contributor

daltonfury42 commented Nov 20, 2021

This looks good. Minor modification, will this work:

  1. Move main.go out of internal, other go devs might be more used to this.
  2. Have a mux.go, it will be nice to have all routes defined in one place.
/internal
    /handler
        mux.go
        createqueue.go
        createtoken.go
        ...
    /persistance (storage layer, will come later)
    /service (for business logic, will come later)
/cmd
    main.go
/build (we can remove if this need not be in git)
/AzureFunction
    /CreateQueueFunction
        function.json
    /CreateTokenFunction
        function.json
    ...
    host.json
    .
    .
Makefile
go.mod
.
.

@aneeshsharma
Copy link
Contributor Author

I've reorganized the files a bit to reflect the structure we decided. I've yet to move the function logic itself to /internal. So, right now everything is in /cmd/handler/main.go.

  • Added a Makefile to build the project
  • Currently I pass CGO_ENABLED=0 along with linker flags to compile the binary as static
  • make would also zip the contents of /AzureFunctions (and output it to /build)
  • This zip can be used to deploy the function

CGO_ENABLED=0 disables CGO. That along with -static linker flag are needed to compile a static bianry. I am not sure why CGO_ENABLED is important. Shouldn't passing the -static flag itself link all libraries dependent on C anyways? It still uses dynamic C libraries until I set CGO_ENABLED=0.

Reason I want to compile as static is because when I deploy the dynamic binary, there is a version mismatch for glibc on Azure Functions.

I am still working on the deployment which seems to be broken from VS Code Azure tools since I moved the Azure Function files. Installing Azure CLI tools was taking a lot of time so I will try with that tomorrow again.

Copy link
Contributor

@daltonfury42 daltonfury42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this PR as is, and the pending items can be done a follow up PR.

@maaverik maaverik merged commit 36479b7 into main Nov 24, 2021
@daltonfury42
Copy link
Contributor

@maaverik Aneesh is added as a collaborator, so will be able to merge his PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants