net::ERR_CONNECTION_REFUSED on API call to my backend

For a hobby project (books.cihanbatasul.com) I have a golang application that runs on my backend as a docker container. The application has a database container and the API container.

The error happens on my frontend when I try to interact with the backend (register, searching for a book. It says:

index-149c02ff.js:166 
        
        
       POST https://cihanbatasul.com:8081/register net::ERR_CONNECTION_REFUSED
st @ index-149c02ff.js:166
(anonymous) @ index-149c02ff.js:166
await in (anonymous) (async)
Nb @ index-149c02ff.js:37
Tb @ index-149c02ff.js:37
Ub @ index-149c02ff.js:37
nf @ index-149c02ff.js:37
se$2 @ index-149c02ff.js:37
(anonymous) @ index-149c02ff.js:37
Rk @ index-149c02ff.js:40
Jb @ index-149c02ff.js:37
hd @ index-149c02ff.js:37
fd @ index-149c02ff.js:37
ed @ index-149c02ff.js:37
index-149c02ff.js:166 TypeError: Failed to fetch
    at st (index-149c02ff.js:166:107788)
    at index-149c02ff.js:166:98541

My main.go file accepts three origins:

package main

import (
    "crypto/rand"
    "crypto/tls"
    "database/sql"
    "encoding/base64"
    "flag"
    "fmt"
    "log"
    "net/http"
    "os"

    "github.com/cihanbatasul/bookapi/internal/models"
    "github.com/joho/godotenv"
    _ "github.com/lib/pq"
    "github.com/rs/cors"
)

type application struct {
    error_log    *log.Logger
    info_log     *log.Logger
    books        *models.BookServiceImplementation
    users        *models.UserServiceImplementation
    user_helpers *models.UserHelperServiceImplementation
    ai           *models.AiServiceImplementation
}

func openDB(dsn string) (*sql.DB, error) {
    db, err := sql.Open("postgres", dsn)
    if err != nil {
        return nil, err
    }

    if err = db.Ping(); err != nil {
        return nil, err
    }

    return db, nil
}

func generateRandomSecret(length int) (string, error) {
    secretBytes := make([]byte, length)
    _, err := rand.Read(secretBytes)
    if err != nil {
        return "", err
    }
    return base64.StdEncoding.EncodeToString(secretBytes), nil
}

func main() {

    if err := godotenv.Load(".env"); err != nil {
        log.Fatalf("Fehler beim Laden der .env-Datei %v", err)
    }

    cors := cors.New(cors.Options{
        AllowedOrigins:   []string{"https://cihanbatasul.com", "http://localhost", "https://books.cihanbatasul.com"},
        AllowedMethods:   []string{"GET", "POST", "OPTIONS"},
        AllowedHeaders:   []string{"Content-Type"},
        AllowCredentials: true,
    })

    addr := flag.String("addr", ":8081", "HTTPS network address")
    flag.Parse()

    dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable", os.Getenv("HOST"), os.Getenv("DB_PORT"), os.Getenv("USER_NAME"), os.Getenv("PASSWORD"), os.Getenv("DB_NAME"))

    info_log := log.New(os.Stdout, "INFOt", log.Ldate|log.Ltime)
    error_log := log.New(os.Stderr, "ERRORt", log.Ldate|log.Ltime|log.Lshortfile)

    db, err := openDB(dsn)
    if err != nil {
        fmt.Printf("%s", dsn)
        error_log.Fatal(err)
    }

    defer db.Close()

    app := &application{
        error_log:    error_log,
        info_log:     info_log,
        books:        &models.BookServiceImplementation{},
        users:        &models.UserServiceImplementation{DB: db},
        user_helpers: &models.UserHelperServiceImplementation{},
        ai:           &models.AiServiceImplementation{},
    }

    handler := cors.Handler(app.Routes())

    certPath := "certs/cert.pem"
    keyPath := "certs/privkey.pem"
    // Load TLS certificate and key
    cert, err := tls.LoadX509KeyPair(certPath, keyPath)
    if err != nil {
        log.Fatal("Error loading certificate and key:", err)
    }

    // Create TLS config
    tlsConfig := &tls.Config{
        Certificates: []tls.Certificate{cert},
    }

    // Create HTTPS server
    srv := &http.Server{
        Addr:      *addr,
        ErrorLog:  app.error_log,
        Handler:   handler,
        TLSConfig: tlsConfig,
    }

    info_log.Println("Starting server on port ", *addr)

    // Start HTTPS server
    err = srv.ListenAndServeTLS("", "")
    if err != nil {
        app.error_log.Fatal(err)
    }
}

When the error happens the API container exits:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ca807a67f67a booksbackend “./main” 14 minutes ago Exited (1) 4 minutes ago booksbackend_container
3516610db914 booksbackend_db “docker-entrypoint.s…” 14 minutes ago Up 14 minutes 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp booksbackend_db_container
6a2f27202298 portfolio2023:latest “portfolio2023 -p 80…” 2 days ago Up 2 days 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp portfolio_backend

with the docker logs saying this:

intero@231:~$ sudo docker logs booksbackend_container
ERROR 2024/06/12 18:13:55 main.go:75: dial tcp 92.205.28.231:5432: connect: connection timed out

I don’t know what the issue could be. I hope someone can help me solve this problem. Sadly I don’t have a lot of experience which is why I don’t know which kind of information would be valuable in solving this issue. If you need further information, please let me know.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật