{"id":21655898,"url":"https://github.com/koltyakov/az-fun-go-sp","last_synced_at":"2025-04-11T21:30:57.716Z","repository":{"id":73404461,"uuid":"320542595","full_name":"koltyakov/az-fun-go-sp","owner":"koltyakov","description":"Azure Functions Golang \u0026 SharePoint Sample","archived":false,"fork":false,"pushed_at":"2024-03-24T20:05:57.000Z","size":599,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T17:22:55.618Z","etag":null,"topics":["azure","azure-functions","go","golang","sample-code","sharepoint","sharepoint-online"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koltyakov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-12-11T10:34:01.000Z","updated_at":"2024-12-30T22:25:39.000Z","dependencies_parsed_at":"2023-04-29T07:38:01.005Z","dependency_job_id":null,"html_url":"https://github.com/koltyakov/az-fun-go-sp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koltyakov%2Faz-fun-go-sp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koltyakov%2Faz-fun-go-sp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koltyakov%2Faz-fun-go-sp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koltyakov%2Faz-fun-go-sp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koltyakov","download_url":"https://codeload.github.com/koltyakov/az-fun-go-sp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248482863,"owners_count":21111393,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["azure","azure-functions","go","golang","sample-code","sharepoint","sharepoint-online"],"created_at":"2024-11-25T08:37:44.127Z","updated_at":"2025-04-11T21:30:57.689Z","avatar_url":"https://github.com/koltyakov.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure Functions Golang \u0026 SharePoint Sample\n\n![banner](assets/banner.jpg)\n\n## Prerequisites\n\n- [Azure Functions Core Tools, v4](https://www.npmjs.com/package/azure-functions-core-tools)\n- [Go SDK](https://golang.org/dl/)\n- [Visual Studio Code](https://code.visualstudio.com) \u0026 [Azure Functions extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions)\n- [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) [optional]\n- [Terraform](./infra/README.md) [optional, for IaC provisioning]\n\n\u003e Mac or Linux machine, or Windows WSL2 are preferred.\n\u003e All the commands within the sample assumed running in `bash`.\n\n## Create Azure Function Resource\n\n\u003e This flow is dev friendly when it comes to quick experiments. For production deployments please consider [infrastructure as code and automation](./infra) via DevOps.\n\nIn VS Code with Azure Functions extension installed:\n\n- `CMD+Shift+P` type `Azure Functions: Create Function App in Azure... (Advanced)` and follow the wizard (further is the example of the steps)\n- Select dev subscription\n- Enter a globally unique name for the new function app\n- Select a runtime stack: `Custom Handler`\n- Select an OS: `Linux`\n- Select a hosting plan: `Consumption`\n- Create a new resource group\n- Enter the name of the new resource group\n- Create new storage account\n- Enter the name of the new storage account\n- Create new Application Insights resource\n- Enter the name of the new Application Insights resource\n- Select a location for new resources (closer to your location)\n\n\u003e Don't forget to remove the resource group after experiments to avoid any unpredictable charges\n\n## Project structure highlights\n\n- `.vscode/` - VSCode settings folder, mostly standard, however, `settings.json` has important modification `\"azureFunctions.deploySubpath\": \"./functions\"` which changes root folder for functions deployment via `ms-azuretools` extension\n- `functions/` - everything related to Azure Functions, isolation makes things simpler while no mixing with custom handler related project files\n  - Functions folders, containing `function.json` file\n  - `.funcignore` - functions ignore file with handy ignore options\n  - `host.json` - host configuration file, mostly standard, interesting and important section is `customHandler`, especially `\"enableForwardingHttpRequest\": true` which is not added by default\n  - `local.settings[.sample].json` - contains configuration which is needed for local development and debug, local settings should be excluded from Git to avoid secrets leak\n  - `proxies.json` - bypassing endpoints to external resources\n- `handlers/` - Handlers sub package grouping by extensible handlers methods\n- `debug.go` - grabs Azure Functions settings to use when starting the handlers' server separately, is not included into production build\n- `main.go` - Custom Handler Go server entry point\n- `mux.go` - routes and handler binding configuration\n- `sp.go` - SharePoint Client and authentication constructor\n- `Makefile` - useful tasks/commands collection for project automation\n\n## Local start/debug\n\n- Copy/rename `local.settings.sample.json` to `local.settings.json`.\n- Provide authentication parameters:\n  - SPAUTH_SITEURL\n  - SPAUTH_CLIENTID\n  - SPAUTH_CLIENTSECRET\n\n[Add-in Only auth](https://go.spflow.com/auth/strategies/addin) is used as a sample.\n\n- Start the local server with `make start`\n- Navigate to one of the URL endpoints printed in the console\n\n## Debugging Go code\n\nFor breaking points debugging in VSCode a number of approaches can be used: attaching to process or connecting to server. In most cases, one would prefer restarting Go server without stoping and restarting `func start` session. Also, it's simpler to place a breakpoint, switch to `main.go` file, and launch debug. Such a scenario can be covered with:\n\n```bash\nmake debug\n```\n\nThe local function app is started without actually starting the Go side-car server, but mimicking the process for functions core utility.\n\nThen, the Go application can be started or restarted as many times as needed, e.g. using `go run ./` or launch debug UI.\n\n## Build and deploy\n\n1\\. Run build command:\n\n```bash\nmake build-prod\n```\n\nThe assumption is that a Linux plan was selected for the function app.\n\nThe build creates `bin/server` binary with the Go custom handler.\n\n2\\. In VSCode,\n\n- `CMD+Shift+P` type `Azure Functions: Deploy to Function App...`\n- Select subscription\n- Select Function App in Azure\n- Confirm deployment\n\n## Publish via CLI\n\nPublishing via CLI is a faster and more robust scalable way.\n\n```bash\nmake appname=\"functionapp_name\" publish\n```\n\nwhere `\"functionapp_name\"` is the name of your Function App instance.\n\n\u003e `az` CLI is required.\n\n## Configure environment variables\n\nIn the Azure Function app, create and provide the following environment variables:\n\n- SPAUTH_SITEURL\n- SPAUTH_CLIENTID\n- SPAUTH_CLIENTSECRET\n\nDepending on the SharePoint environment and use case, [auth strategy](https://go.spflow.com/auth/strategies) can be different. For a production installation [Azure Certificate Auth](https://go.spflow.com/auth/custom-auth/azure-certificate-auth) might be preferred.\n\n`sp.go/getSP` handler should be aligned with authentication parameters.\n\n## Overriding host's settings\n\nAzure Functions use .Net native approaches dealing with settings. Hierarchical settings can be overriden with environment variables.\n\nE.g., you need `server.exe` locally but a Linux Functions host still needs `server` (without extention) in `hosts.json`, it can be redefined in `local.settings.json` with a value:\n\n```jsonc\n{\n  // ...\n  \"Values\": {\n    // ...\n    \"AzureFunctionsJobHost__customHandler__description__defaultExecutablePath\": \"bin/server.exe\"\n  }\n}\n```\n\n## Adding new function\n\n1\\. `CMD+Shift+P` type `Azure Functions: Create Function...`\n\n2\\. Select a template for your function, e.g. `HTTP trigger`\n\n3\\. Provide a function name. e.g. `HttpTrigger1`\n\n4\\. Select authorization level, e.g. `Function`\n\nA function's folder and bindings definition is created `functions/HttpTrigger1/function.json`:\n\n```json\n{\n  \"bindings\": [\n    {\n      \"authLevel\": \"function\",\n      \"type\": \"httpTrigger\",\n      \"direction\": \"in\",\n      \"name\": \"req\",\n      \"methods\": [\"get\", \"post\"]\n    },\n    {\n      \"type\": \"http\",\n      \"direction\": \"out\",\n      \"name\": \"res\"\n    }\n  ]\n}\n```\n\n5\\. Run `make start` and follow via `http://localhost:7071/api/HttpTrigger1` for just created function's binding\n\nThe Echo service handler will response with URL path `/api/HttpTrigger1`, that's because created function is not processed with specific route Go custom handler yet.\n\n`Ctrl+C` to stop running functions host.\n\n6\\. Create `handlers/HttpTrigger1.go` and paste:\n\n```golang\npackage handlers\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n)\n\n// HTTPTrigger1 handles a function's logic\nfunc (h *Handlers) HTTPTrigger1(w http.ResponseWriter, r *http.Request) {\n\tname := r.URL.Query().Get(\"name\")\n\tfmt.Fprintf(w, \"Hello, %s!\", name)\n}\n```\n\n7\\. Bind a handler with a route in `mux.go` in API routes section:\n\n```golang\nhttp.HandleFunc(\"/api/HttpTrigger1\", h.HTTPTrigger1)\n```\n\n8\\. Run `make start` and navigate to `http://localhost:7071/api/HttpTrigger1?name=Function`\n\n`Hello, Function!` should be responded back.\n\n## SharePoint API in Go\n\nCheck `handlers/fields.go` and `handlers/lists.go` as samples of comuting with SharePoint instance.\n\nIn a handler, `h.sp.` exposes access to SharePoint object model.\n\nThe API layer is powered by [gosip](https://github.com/koltyakov/gosip).\n\n## Reference\n\n- [Azure Functions custom handlers](https://docs.microsoft.com/en-us/azure/azure-functions/functions-custom-handlers)\n- [Create a Go function in Azure using VSCode](https://docs.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-other)\n- [Functions Custom Handlers (Go)](https://github.com/Azure-Samples/functions-custom-handlers/tree/master/go)\n- [Serverless Go in Azure Functions with custom handlers (video)](https://m.youtube.com/watch?v=RPCEH247twU)\n- [Working with SharePoint in Go](https://go.spflow.com/samples/basic-crud)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoltyakov%2Faz-fun-go-sp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoltyakov%2Faz-fun-go-sp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoltyakov%2Faz-fun-go-sp/lists"}