{"id":22852468,"url":"https://github.com/waley-z/dpm-wearable-backend","last_synced_at":"2026-04-30T16:31:32.100Z","repository":{"id":45525297,"uuid":"503965302","full_name":"Waley-Z/dpm-wearable-backend","owner":"Waley-Z","description":"Server backend for DPM Wearable project.","archived":false,"fork":false,"pushed_at":"2022-10-28T06:45:48.000Z","size":29,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-06T11:37:51.944Z","etag":null,"topics":["flask","gcp","mysql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Waley-Z.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-16T00:51:50.000Z","updated_at":"2023-05-12T15:57:52.000Z","dependencies_parsed_at":"2023-01-20T16:31:51.312Z","dependency_job_id":null,"html_url":"https://github.com/Waley-Z/dpm-wearable-backend","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/Waley-Z%2Fdpm-wearable-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Waley-Z%2Fdpm-wearable-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Waley-Z%2Fdpm-wearable-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Waley-Z%2Fdpm-wearable-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Waley-Z","download_url":"https://codeload.github.com/Waley-Z/dpm-wearable-backend/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246429487,"owners_count":20775807,"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":["flask","gcp","mysql"],"created_at":"2024-12-13T06:08:23.178Z","updated_at":"2026-04-30T16:31:27.078Z","avatar_url":"https://github.com/Waley-Z.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Connecting to Cloud SQL - MySQL\n\n## Before you begin\n\n1. If you haven't already, set up a Python Development Environment by following the [python setup guide](https://cloud.google.com/python/setup) and \n[create a project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project).\n\n1. Create a 2nd Gen Cloud SQL Instance by following these \n[instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string,\ndatabase user, and database password that you create.\n\n1. Create a database for your application by following these \n[instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database\nname. \n\n1. Create a service account with the 'Cloud SQL Client' permissions by following these \n[instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account).\nDownload a JSON key to use to authenticate your connection. \n\n## Running locally\n\nTo run this application locally, download and install the `cloud_sql_proxy` by\nfollowing the instructions\n[here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).\n\nInstructions are provided below for using the proxy with a TCP connection or a Unix Domain Socket.\nOn Linux or Mac OS you can use either option, but on Windows the proxy currently requires a TCP\nconnection.\n\n### Launch proxy with TCP\n\nTo run the sample locally with a TCP connection, set environment variables and launch the proxy as\nshown below.\n\n#### Linux / Mac OS\nUse these terminal commands to initialize environment variables:\n```bash\nexport GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json\nexport INSTANCE_HOST='127.0.0.1'\nexport DB_PORT='3306'\nexport DB_USER='\u003cYOUR_DB_USER_NAME\u003e'\nexport DB_PASS='\u003cYOUR_DB_PASSWORD\u003e'\nexport DB_NAME='\u003cYOUR_DB_NAME\u003e'\n```\nNote: Saving credentials in environment variables is convenient, but not secure - consider a more\nsecure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to\nhelp keep secrets safe.\n\nThen use this command to launch the proxy in the background:\n```bash\n./cloud_sql_proxy -instances=\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e=tcp:3306 -credential_file=$GOOGLE_APPLICATION_CREDENTIALS \u0026\n```\n\n#### Windows/PowerShell\nUse these PowerShell commands to initialize environment variables:\n```powershell\n$env:GOOGLE_APPLICATION_CREDENTIALS=\"\u003cCREDENTIALS_JSON_FILE\u003e\"\n$env:INSTANCE_HOST=\"127.0.0.1\"\n$env:DB_PORT=\"3306\"\n$env:DB_USER=\"\u003cYOUR_DB_USER_NAME\u003e\"\n$env:DB_PASS=\"\u003cYOUR_DB_PASSWORD\u003e\"\n$env:DB_NAME=\"\u003cYOUR_DB_NAME\u003e\"\n```\nNote: Saving credentials in environment variables is convenient, but not secure - consider a more\nsecure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to\nhelp keep secrets safe.\n\nThen use this command to launch the proxy in a separate PowerShell session:\n```powershell\nStart-Process -filepath \"C:\\\u003cpath to proxy exe\u003e\" -ArgumentList \"-instances=\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e=tcp:3306 -credential_file=\u003cCREDENTIALS_JSON_FILE\u003e\"\n```\n\n### Launch proxy with Unix Domain Socket\nNOTE: this option is currently only supported on Linux and Mac OS. Windows users should use the\n[Launch proxy with TCP](#launch-proxy-with-tcp) option.\n\nTo use a Unix socket, you'll need to create a directory and give write access to the user running\nthe proxy. For example:\n\n```bash\nsudo mkdir /cloudsql\nsudo chown -R $USER /cloudsql\n```\n\nUse these terminal commands to initialize other environment variables as well:\n```bash\nexport GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json\nexport INSTANCE_UNIX_SOCKET='./cloudsql/\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e'\nexport DB_USER='\u003cYOUR_DB_USER_NAME\u003e'\nexport DB_PASS='\u003cYOUR_DB_PASSWORD\u003e'\nexport DB_NAME='\u003cYOUR_DB_NAME\u003e'\n```\nNote: Saving credentials in environment variables is convenient, but not secure - consider a more\nsecure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to\nhelp keep secrets safe.\n\nThen use this command to launch the proxy in the background:\n```bash\n./cloud_sql_proxy -dir=./cloudsql --instances=\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e --credential_file=$GOOGLE_APPLICATION_CREDENTIALS \u0026\n```\n\n### Testing the application\n\nNext, setup install the requirements into a virtual environment:\n```bash\nvirtualenv --python python3 env\nsource env/bin/activate\npip install -r requirements.txt\n```\n\nFinally, start the application:\n```bash\npython app.py\n```\n\nNavigate towards `http://127.0.0.1:8080` to verify your application is running correctly.\n\n## Deploy to App Engine Standard\n\nTo run on GAE-Standard, create an App Engine project by following the setup for these \n[instructions](https://cloud.google.com/appengine/docs/standard/python3/quickstart#before-you-begin).\n\nFirst, update `app.standard.yaml` with the correct values to pass the environment \nvariables into the runtime. Your `app.standard.yaml` file should look like this:\n\n```yaml\nruntime: python37\nentrypoint: gunicorn -b :$PORT app:app\n\nenv_variables:\n  INSTANCE_UNIX_SOCKET: /cloudsql/\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e\n  DB_USER: \u003cYOUR_DB_USER_NAME\u003e\n  DB_PASS: \u003cYOUR_DB_PASSWORD\u003e\n  DB_NAME: \u003cYOUR_DB_NAME\u003e\n```\n\nNote: Saving credentials in environment variables is convenient, but not secure - consider a more\nsecure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to\nhelp keep secrets safe.\n\nNext, the following command will deploy the application to your Google Cloud project:\n\n```bash\ngcloud app deploy app.standard.yaml\n```\n\n## Deploy to App Engine Flexible\n\nTo run on GAE-Flexible, create an App Engine project by following the setup for these \n[instructions](https://cloud.google.com/appengine/docs/flexible/python/quickstart#before-you-begin).\n\nFirst, update `app.flexible.yaml` with the correct values to pass the environment \nvariables into the runtime. Your `app.flexible.yaml` file should look like this:\n\n```yaml\nruntime: custom\nenv: flex\nentrypoint: gunicorn -b :$PORT app:app\n\nenv_variables:\n  INSTANCE_UNIX_SOCKET: /cloudsql/\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e\n  DB_USER: \u003cYOUR_DB_USER_NAME\u003e\n  DB_PASS: \u003cYOUR_DB_PASSWORD\u003e\n  DB_NAME: \u003cYOUR_DB_NAME\u003e\n\nbeta_settings:\n  cloud_sql_instances: \u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e\n```\n\nNote: Saving credentials in environment variables is convenient, but not secure - consider a more\nsecure solution such as [Secret Manager](https://cloud.google.com/secret-manager/docs/overview) to\nhelp keep secrets safe.\n\nNext, the following command will deploy the application to your Google Cloud project:\n\n```bash\ngcloud app deploy app.flexible.yaml\n```\n\n## Deploy to Cloud Run\n\nSee the [Cloud Run documentation](https://cloud.google.com/sql/docs/mysql/connect-run)\nfor more details on connecting a Cloud Run service to Cloud SQL.\n\n1. Build the container image:\n\n```sh\ngcloud builds submit --tag gcr.io/\u003cYOUR_PROJECT_ID\u003e/run-mysql\n```\n\n2. Deploy the service to Cloud Run:\n\n```sh\ngcloud run deploy run-mysql --image gcr.io/\u003cYOUR_PROJECT_ID\u003e/run-mysql \\\n  --add-cloudsql-instances '\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e' \\\n  --set-env-vars INSTANCE_UNIX_SOCKET='/cloudsql/\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e' \\\n  --set-env-vars DB_USER='\u003cYOUR_DB_USER_NAME\u003e' \\\n  --set-env-vars DB_PASS='\u003cYOUR_DB_PASSWORD\u003e' \\\n  --set-env-vars DB_NAME='\u003cYOUR_DB_NAME\u003e'\n```\n\nTake note of the URL output at the end of the deployment process.\n\nReplace environment variables with the correct values for your Cloud SQL\ninstance configuration.\n\nIt is recommended to use the [Secret Manager integration](https://cloud.google.com/run/docs/configuring/secrets) for Cloud Run instead\nof using environment variables for the SQL configuration. The service injects the SQL credentials from\nSecret Manager at runtime via an environment variable.\n\nCreate secrets via the command line:\n```sh\necho -n $INSTANCE_UNIX_SOCKET | \\\n    gcloud secrets create [INSTANCE_UNIX_SOCKET_SECRET] --data-file=-\n```\n\nDeploy the service to Cloud Run specifying the env var name and secret name:\n```sh\ngcloud beta run deploy SERVICE --image gcr.io/\u003cYOUR_PROJECT_ID\u003e/run-sql \\\n    --add-cloudsql-instances \u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e \\\n    --update-secrets INSTANCE_UNIX_SOCKET=[INSTANCE_UNIX_SOCKET_SECRET]:latest,\\\n      DB_USER=[DB_USER_SECRET]:latest, \\\n      DB_PASS=[DB_PASS_SECRET]:latest, \\\n      DB_NAME=[DB_NAME_SECRET]:latest\n```\n\n3. Navigate your browser to the URL noted in step 2.\n\nFor more details about using Cloud Run see http://cloud.run.\nReview other [Python on Cloud Run samples](../../../run/).\n\n## Deploy to Cloud Functions\n\nTo deploy the service to [Cloud Functions](https://cloud.google.com/functions/docs) run the following command:\n\n```sh\ngcloud functions deploy votes --runtime python39 --trigger-http --allow-unauthenticated \\\n--set-env-vars INSTANCE_UNIX_SOCKET=/cloudsql/\u003cPROJECT-ID\u003e:\u003cINSTANCE-REGION\u003e:\u003cINSTANCE-NAME\u003e \\\n--set-env-vars DB_USER=$DB_USER \\\n--set-env-vars DB_PASS=$DB_PASS \\\n --set-env-vars DB_NAME=$DB_NAME\n```\n\nTake note of the URL output at the end of the deployment process or run the following to view your function:\n\n```sh\ngcloud app browse\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaley-z%2Fdpm-wearable-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwaley-z%2Fdpm-wearable-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwaley-z%2Fdpm-wearable-backend/lists"}