Spaces:
Sleeping
Deploying Go Backend to Hugging Face Spaces
This guide outlines the steps to deploy your Go application (whatsapp-backend) to Hugging Face Spaces using Docker.
Prerequisites
- A Hugging Face account.
- Your project code locally.
Step 1: Create a New Space
- Log in to your Hugging Face account.
- Click on your profile picture in the top right and select New Space.
- Owner: Select your username or organization.
- Space Name: Enter a name for your space (e.g.,
whatsapp-backend). - License: Choose a license (e.g., MIT) or leave it empty correctly.
- SDK: Select Docker.
- Blank: Choose "Blank" to start with an empty repository.
- Click Create Space.
Step 2: Prepare Your Code (Already Done by Agent)
Your Dockerfile has been configured to:
- Use port
7860(required by Hugging Face Spaces). - Bind to address
0.0.0.0.
Ensure your Dockerfile contains:
ENV HOST_ADDRESS=0.0.0.0
ENV HOST_PORT=7860
EXPOSE 7860
Step 3: Deployment Methods
You can deploy using one of two methods:
Method A: Upload via Web Interface (Easiest)
- In your newly created Space, go to the Files tab.
- Click Add file -> Upload files.
- Drag and drop all your project files (including
.dockerignoreif you have one,Dockerfile,go.mod,go.sum,main.go, and all source directories likeconfig,controllers, etc.) into the upload area.- Note: Do not upload
.gitfolder.
- Note: Do not upload
- Commit the changes with a message like "Initial commit for deployment".
- Hugging Face will automatically start building your Docker image. You can see the progress in the App tab.
Method B: Push via Git (Recommended for updates)
- In your Space, click on the Clone repository button (or just copy the HTTPS URL).
- Open your terminal in your project directory.
- Initialize git if you haven't (or use your existing git setup):
git remote add space https://huggingface.co/spaces/YOUR_USERNAME/SPACE_NAME - Push your code:
(Note: You might need to use a Hugging Face Access Token as your password if prompted).git push space main
Method C: Automated Sync (GitHub Actions) - RECOMMENDED
This method automatically pushes your code to the Hugging Face Space whenever you push to the test branch of your GitHub repository.
Generate a Hugging Face Token:
- Go to your Hugging Face Settings > Tokens.
- Create a new token with write permissions.
- Copy the token.
Add Secret to GitHub:
- Go to your GitHub repository.
- Navigate to Settings > Secrets and variables > Actions.
- Click New repository secret.
- Name:
HF_TOKEN - Value: Paste your Hugging Face token.
- Click Add secret.
Update Workflow File:
- Open
.github/workflows/sync-to-hub.ymlin your project. - Find the
git pushcommand at the bottom. - Replace
YOUR_USERNAMEandSPACE_NAMEwith your actual Hugging Face username and Space name. - Example:
git push https://ryu:Start@huggingface.co/spaces/ryu/whatsapp-backend test:main -f
- Open
Trigger Deployment:
- Simply push any change to your
testbranch:git checkout test git add . git commit -m "New feature" git push origin test - The GitHub Action will run and sync your code to the Spaces.
- Simply push any change to your
Step 4: Configure Secrets (Environment Variables)
Your application uses environment variables (like Database credentials). You must set these in Hugging Face settings, not in the Dockerfile (for security).
Go to your Space's Settings tab.
Scroll down to Variables and secrets.
Click New secret for sensitive data (like passwords) or New variable for public config.
Add the following based on your
env_config.goand.envneeds:DB_HOSTDB_PORTDB_USERDB_PASSWORDDB_NAMESALT(Optional, defaults to "Def4u|7")TZ(Optional, e.g., "Asia/Jakarta")
Note:
HOST_ADDRESSandHOST_PORTare already set in the Dockerfile, so you don't need to add them here unless you want to override them.
Step 5: Verify Deployment
- Go to the App tab.
- Wait for the status to change from Building to Running.
- If successful, you will see your application output or a blank screen (since it's a backend API).
- You can verify it's running by checking the Logs tab for server startup messages.