Skip to content

Commit

Permalink
docs: update go docs
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Najmabadi committed Dec 10, 2024
1 parent 60e95cf commit d58ef18
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 225 deletions.
10 changes: 10 additions & 0 deletions liara_nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,16 @@ location ~ ^/client-api/database/?$ {
return 301 /references/api/about/;
}

# golang platform

location ~ ^/paas/docker/related-apps/go?$ {
return 301 /paas/golang/getting-started/;
}

location ~ ^/paas/go/(.*)$ {
return 301 /paas/golang/$1;
}




Expand Down
21 changes: 11 additions & 10 deletions src/components/Sidebar/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2693,21 +2693,22 @@ export default {
icon: <IconContainer alt="sqlite" />,
link: "/paas/golang/how-tos/connect-to-db/sqlite"
},
{
title: "دیتابیس MongoDB",
icon: <IconContainer alt="mongodb" />,
link: "/paas/golang/how-tos/connect-to-db/mongodb"
},
{
title: "دیتابیس Redis",
icon: <IconContainer alt="redis" />,
link: "/paas/golang/how-tos/connect-to-db/redis"
},
// {
// title: "دیتابیس ElasticSearch",
// icon: <IconContainer alt="elastic" />,
// link: "/paas/golang/how-tos/connect-to-db/elastic-search"
// },
// {
// title: "دیتابیس MongoDB",
// icon: <IconContainer alt="mongodb" />,
// link: "/paas/golang/how-tos/connect-to-db/mongodb"
// },
{
title: "دیتابیس ElasticSearch",
icon: <IconContainer alt="elastic" />,
link: "/paas/golang/how-tos/connect-to-db/elastic-search"
},

// {
// hr: true
// },
Expand Down
2 changes: 1 addition & 1 deletion src/pages/paas/docker/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import Head from "next/head";
{ alt: 'https://files.liara.ir/liara/deno/deno-logo.png', platform: 'Deno', link: "/paas/docker/related-apps/deno" },
{ alt: 'bun', platform: 'Bun', link: "/paas/docker/related-apps/bun" },
{ alt: 'seq', platform: 'Seq', link: "/paas/docker/related-apps/seq" },
{ alt: 'go', platform: 'Golang', link: "/paas/docker/related-apps/go" },
// { alt: 'go', platform: 'Golang', link: "/paas/docker/related-apps/go" },
].map(item =>
<Link href={item.link}>
<Card className="flex cursor-pointer w-full items-center justify-between">
Expand Down
5 changes: 3 additions & 2 deletions src/pages/paas/golang/how-tos/connect-to-db/about.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import Head from "next/head";
<div className="grid md:grid-cols-3 gap-4">
{[
{ alt: 'mysql', platform: 'MySQL/MariaDB', link: "./mysql" },
{ alt: 'postgres', platform: 'PostgreSQL', link: "./postgresql" },
{ alt: 'mssql', platform: 'MSSQL', link: "./mssql" },
{ alt: 'postgres', platform: 'PostgreSQL', link: "./postgresql" },
{ alt: 'sqlite', platform: 'SQLite', link: "./sqlite" },
{ alt: 'mongodb', platform: 'MongoDB', link: "./mongodb" },
{ alt: 'redis', platform: 'Redis', link: "./redis" },
// { alt: 'elastic', platform: 'ElasticSearch', link: "./elastic-search" },
{ alt: 'elastic', platform: 'ElasticSearch', link: "./elastic-search" },
].map(item =>
<Link href={item.link}>
<Card className="flex cursor-pointer w-full items-center justify-between">
Expand Down
80 changes: 35 additions & 45 deletions src/pages/paas/golang/how-tos/connect-to-db/elastic-search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,31 @@ import Head from "next/head";
<Head>
<title>مستندات اتصال به ElasticSearch در go - لیارا</title>
<meta property="og:title" content="مستندات خدمات رایانش ابری لیارا" />
<meta property="og:description" content="مستندات قدم به قدم اتصال با go به دیتابیس ElasticSearch" />
<meta property="og:description" content="مستندات اتصال به دیتابیس ElasticSearch در فریم‌ورک go" />
<meta property="og:image" content="https://files.liara.ir/liara/logos/liara-poster.jpg" />
</Head>
# اتصال به دیتابیس ElasticSearch در برنامه‌های go
<hr className="mb-2" />

برای اتصال به دیتابیس ElasticSearch در برنامه‌های go، در ابتدا باید ماژول‌های مربوط به آنرا با اجرای دستورات زیر، نصب کنید:
برای اتصال به دیتابیس ElasticSearch در برنامه‌های go، در ابتدا باید ماژول مربوط به آن را با اجرای دستور زیر، نصب کنید:

<div className="h-2" />
<div dir='ltr'>
<Highlight className="bash">
{`go get github.com/joho/godotenv
`}
{`go get github.com/elastic/go-elasticsearch/v8`}
</Highlight>
</div>
<div className="h-2" />

پس از آن، کافیست تا
اطلاعات مربوط به دیتابیس خود را
به متغیرهای محیطی برنامه خود، اضافه کنید؛ به عنوان مثال:

<div className="h-2" />
<div dir='ltr'>
<Highlight className="txt">
{`DB_URL=http://host:port/_cluster/health?pretty
DB_USERNAME=username
DB_PASSWORD=password`}
{`ELASTICSEARCH_URI=http://fitz-roy.liara.cloud:33492/
ELASTICSEARCH_USERNAME=elastic
ELASTICSEARCH_PASSWORD=uWrfTv0eW5ESQPYDxpX8EQWI`}
</Highlight>
</div>
<div className="h-2" />
Expand All @@ -54,61 +52,53 @@ DB_PASSWORD=password`}
{`package main
import (
"encoding/base64"
"context"
"fmt"
"io"
"net/http"
"log"
"os"
"github.com/joho/godotenv"
"github.com/elastic/go-elasticsearch/v8"
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file:", err)
return
// Get Elasticsearch configuration from environment variables
esURI := os.Getenv("ELASTICSEARCH_URI")
esUsername := os.Getenv("ELASTICSEARCH_USERNAME")
esPassword := os.Getenv("ELASTICSEARCH_PASSWORD")
if esURI == "" || esUsername == "" || esPassword == "" {
log.Fatal("Environment variables ELASTICSEARCH_URI, ELASTICSEARCH_USERNAME, and ELASTICSEARCH_PASSWORD must be set")
}
// Elasticsearch connection information from environment variables
url := os.Getenv("DB_URL")
username := os.Getenv("DB_USERNAME")
password := os.Getenv("DB_PASSWORD")
// Define the Elasticsearch connection configuration
esConfig := elasticsearch.Config{
Addresses: []string{
esURI,
},
Username: esUsername,
Password: esPassword,
}
// Creating an HTTP request
req, err := http.NewRequest("GET", url, nil)
// Create a new Elasticsearch client
es, err := elasticsearch.NewClient(esConfig)
if err != nil {
fmt.Println("Error creating HTTP request:", err)
return
log.Fatalf("Error creating the Elasticsearch client: %s", err)
}
// Adding authentication information to the request
req.Header.Add("Authorization", "Basic "+basicAuth(username, password))
// Sending the request to Elasticsearch
client := http.DefaultClient
res, err := client.Do(req)
// Test the connection
res, err := es.Info()
if err != nil {
fmt.Println("Error sending HTTP request:", err)
return
log.Fatalf("Error getting Elasticsearch info: %s", err)
}
defer res.Body.Close()
// Reading and printing the response
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
return
// Print the response
if res.IsError() {
log.Fatalf("Elasticsearch returned an error: %s", res.String())
}
fmt.Println(string(body))
}
// Function to create a Base64-encoded authentication string
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
fmt.Println("Connected to Elasticsearch successfully!")
fmt.Println(res.String())
}
`}
</Highlight>
Expand Down
Loading

0 comments on commit d58ef18

Please sign in to comment.