Hosting Go. Overview of options and example of deploying a Golang application

Let's look at options for hosting a Golang application and deploy the code using the git push command to a linked Git repository.

Go Hosting

  1. Herokuforeign service with native support for the Golang environment. Make a git push to the linked Git repository, and the service will automatically deploy your Go project to the server. The cost of the service starts from $5 (from $25 if you want to receive guarantees of continuous operation and a dedicated resource).

  1. Amvera CloudRussian service with Heroku functionality. Supports deployment and updating via push to a linked Git repository. If you haven't worked with Git, you can use the upload interface. There is native support for both Golang and other environments; you just need to select the desired configuration in the interface. The cost starts from 170 rubles. per month, and there is a starting balance to start using for free. You can pay by RF card.

  1. Application service from Digital Ocean. Allows you to easily deploy your go application in the infrastructure of a given provider.

Deploying a Go application with SQLite3

In this article we will look at how to deploy an application in Golang using the built-in SQLite3 database. More precisely, we will deploy an echo telegram bot and connect a database to save data in the cloud.

We will use:

●Git

● Visual Studio Code (any other IDE will do)

●Telego

● SQLite3

Video example of deploying a Go project on a server

Detailed instructions for deployment

First, let's create a project in Amvera

To do this we need first register or log in to your existing account.

While in the main menu, click the “Create” button.

Next, enter any project name, select the “Application” service type and select a tariff plan convenient for you.

Click “Next” and proceed to downloading project data.

There are two options to upload a project: directly using git commands or through the service interface. We will look at both options for loading the project.

Connecting to a repository via Git

1. Call the terminal in the IDE where the application is open, or open the project folder in the terminal

2. Initialize the local Git repository with the command

git init

3. Add a remote repository for our project (the url of your repository will be different. To avoid syntax errors, copy the link in the second step of creating the project)

git remote add amvera https://git.amvera.ru/имя_пользователя/имя_проекта

4. Add files and make the first commit

git add .
git commit -m "init"

5. Push our code to the project repository

git push amvera master

Initialization via interface

To add files this way, we just need to drag and drop the project files into the area on the site

After adding the files, create a configuration file

To do this, go to link and generate the file

Among the environments, choose Go. Leave the rest of the configurations as default (change if you need it)

After this, click the “Generate YAML” button, after which the amvera.yml configuration file is saved

We upload this file to the project repository.

Important: The amvera.yml file appeared in the remote repository, but we don’t have it locally yet. Add the yml file to the repository so that Git does not throw an error during future updates to our application.

Go to the IDE terminal.

Enter:

git pull amvera master

Connecting SQLite

Since SQLite is an embedded database, we won't have to make many changes.

Let's go into the application code. Creating a database will look something like this:

package main
 
import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
)
 
func main() {
	// Открываем соединение с базой данных
	db, err := sql.Open("sqlite3", "example.db")
	if err != nil {
    	fmt.Println("Ошибка при открытии базы данных:", err)
    	return
	}
	defer db.Close()
 
	// Проверяем, что соединение действительно открыто
	err = db.Ping()
	if err != nil {
    	fmt.Println("Ошибка при проверке соединения с базой данных:", err)
    	return
	}
	fmt.Println("Соединение с базой данных установлено успешно")
}

Change the path to save the database file. To do this, instead of (in my example) “example.db” we write “/data/example.db”.

Now our database will be saved in /data.

Checking the functionality of the project

To do this, go to the project, click on the “Rebuild project” button in the button bar.

Button panel

Next, click “Restart project”.

After we restarted the project, for some time we will see the status “Launch in progress”, after which, if everything was done correctly, the status “Deployed successfully” will appear.

Possible problems

● Endless status “Startup in progress”

If you see an unchanged status, then you have a problem with the configuration file (amvera.yml). Try to recreate the file using the previously suggested method. You can also create a configuration file through the “Configuration” tab.

This error may also be caused by the fact that you are downloading a large project or have chosen an insufficient tariff for your project. To fix it, select a tariff higher than the current one in the “Settings” tab.

● “Application error”

This error may appear before the “Application running” status; there is nothing to worry about. When the code has just launched, the cloud cannot yet determine the status of the project.

Also, if this error does not disappear, you may have made an error in the project code or configured the configuration file incorrectly.

You can read the description of other possible errors in the documentation for link.

Example project code
package main

import (
    "database/sql"
    "fmt"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    // Открываем соединение с базой данных
    db, err := sql.Open("sqlite3", "example.db")
    if err != nil {
        fmt.Println("Ошибка при открытии базы данных:", err)
        return
    }
    defer db.Close()

    // Проверяем, что соединение действительно открыто
    err = db.Ping()
    if err != nil {
        fmt.Println("Ошибка при проверке соединения с базой данных:", err)
        return
    }
    fmt.Println("Соединение с базой данных установлено успешно")
}

Result: We have successfully deployed an application with SQLite on the Go server, so that you can update it with a simple command in the terminal – git push.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *