Skip to main content

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.

Initialize Project

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:
main.go
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")
}
Gin listens on port 80 which is required for web applications on Shard Cloud.

Building Your Application

Go applications must be compiled before deployment:
# 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:
.shardcloud
DISPLAY_NAME=Gin API
DESCRIPTION=High-performance Go Gin API
MAIN=app
MEMORY=256
VERSION=recommended
SUBDOMAIN=my-gin-api
Go applications are memory-efficient. 256MB is often sufficient for simple APIs.

Deploying

1

Build Your Application

Run GOOS=linux GOARCH=amd64 go build -o app main.go to create the Linux binary.
2

Prepare Your Files

Ensure you have:
  • app (compiled binary)
  • .shardcloud
3

Create ZIP Archive

Compress your files into a .zip file.
4

Upload to Shard Cloud

Go to Shard Cloud Dashboard and upload your project.

Advanced Example: REST API with Routes

main.go
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 for more information.

Troubleshooting

Ensure you compiled with GOOS=linux GOARCH=amd64 for the Shard Cloud environment.
Make sure your application uses port 80.