> ## Documentation Index
> Fetch the complete documentation index at: https://docs.shardcloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# How to Host Gin Applications

> Learn how to create and deploy Gin applications on Shard Cloud.

## Introduction

This guide covers deploying Gin applications on Shard Cloud. Gin is a high-performance HTTP web framework for Go.

## Creating Your Project

Ensure you have **Go** installed. Download from [go.dev](https://go.dev/dl/).

### Initialize Project

```bash theme={null}
mkdir my-gin-app
cd my-gin-app
go mod init my-gin-app
go get -u github.com/gin-gonic/gin
```

### Basic Gin Application

Create a `main.go` file:

```go main.go theme={null}
package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Hello World!",
		})
	})

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "pong",
		})
	})

	r.Run(":80")
}
```

<Note>
  Gin listens on port 80 which is required for web applications on Shard Cloud.
</Note>

## Building Your Application

Go applications must be compiled before deployment:

```bash theme={null}
# For Linux (Shard Cloud environment)
GOOS=linux GOARCH=amd64 go build -o app main.go
```

This creates a binary named `app` that can run on Shard Cloud.

## Shard Cloud Configuration

Create a `.shardcloud` file:

```systemd .shardcloud theme={null}
DISPLAY_NAME=Gin API
DESCRIPTION=High-performance Go Gin API
MAIN=app
MEMORY=256
VERSION=recommended
SUBDOMAIN=my-gin-api
```

<Note>
  Go applications are memory-efficient. 256MB is often sufficient for simple APIs.
</Note>

## Deploying

<Steps>
  <Step title="Build Your Application">
    Run `GOOS=linux GOARCH=amd64 go build -o app main.go` to create the Linux binary.
  </Step>

  <Step title="Prepare Your Files">
    Ensure you have:

    * `app` (compiled binary)
    * `.shardcloud`
  </Step>

  <Step title="Create ZIP Archive">
    Compress your files into a `.zip` file.
  </Step>

  <Step title="Upload to Shard Cloud">
    Go to [Shard Cloud Dashboard](https://shardcloud.app/dash/applications) and upload your project.
  </Step>
</Steps>

## Advanced Example: REST API with Routes

```go main.go theme={null}
package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type User struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

var users = []User{
	{ID: "1", Name: "John"},
	{ID: "2", Name: "Jane"},
}

func main() {
	r := gin.Default()

	// API routes
	api := r.Group("/api")
	{
		api.GET("/users", getUsers)
		api.GET("/users/:id", getUser)
		api.POST("/users", createUser)
	}

	r.Run(":80")
}

func getUsers(c *gin.Context) {
	c.JSON(http.StatusOK, users)
}

func getUser(c *gin.Context) {
	id := c.Param("id")
	for _, user := range users {
		if user.ID == id {
			c.JSON(http.StatusOK, user)
			return
		}
	}
	c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
}

func createUser(c *gin.Context) {
	var newUser User
	if err := c.ShouldBindJSON(&newUser); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	users = append(users, newUser)
	c.JSON(http.StatusCreated, newUser)
}
```

## Additional Resources

Visit the [official Gin documentation](https://gin-gonic.com/docs/) for more information.

## Troubleshooting

<AccordionGroup>
  <Accordion title="Binary won't execute">
    Ensure you compiled with `GOOS=linux GOARCH=amd64` for the Shard Cloud environment.
  </Accordion>

  <Accordion title="Port already in use">
    Make sure your application uses port 80.
  </Accordion>
</AccordionGroup>
