https://github.com/poabob/go-discord
A discord clone built with Golang.
https://github.com/poabob/go-discord
Last synced: over 1 year ago
JSON representation
A discord clone built with Golang.
- Host: GitHub
- URL: https://github.com/poabob/go-discord
- Owner: POABOB
- License: mit
- Created: 2023-10-29T10:10:21.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-26T09:03:02.000Z (over 2 years ago)
- Last Synced: 2025-01-21T11:45:14.298Z (over 1 year ago)
- Language: Go
- Size: 84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-discord
This is a discord backend project build with [go-zero](https://github.com/zeromicro/go-zero) and deploy on the Kubernetes.
## Tech Stack
- Remote Database Sevice: [Planetscale](https://app.planetscale.com)
- Database: [MySQL](https://www.mysql.com)
- Orm Library: [Prisma](https://www.prisma.io)
- Framework: [go-zero](https://github.com/zeromicro/go-zero)
- Kubernetes Service: [Scaleway](https://www.scaleway.com/en/)
- User Management and Authentication Service: [Clerk](https://clerk.com)
## Setup Prisma
1. make sure you have remote DB on [planetscale](https://app.planetscale.com)
2. get the `prisma-client-go` library
```bash
$ go get github.com/steebchen/prisma-client-go
```
3. define a prisma db schema, the code below is a demo schema
`prisma/schema.prisma`
```prisma
// 使用別人寫好的 prisma-client-go 來連線
generator client {
provider = "go run github.com/steebchen/prisma-client-go"
}
// DB 的類型、位置、模式
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
model Post {
id String @default(cuid()) @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String
published Boolean
desc String?
}
```
4. create .env file for DATABASE_URL
`.env`
```
DATABASE_URL='mysql://user:password@host:3306/db_name?sslaccept=strict'
```
5. sync the database with the schema
```bash
$ go run github.com/steebchen/prisma-client-go db push
# or regenerate the code
$ go run github.com/steebchen/prisma-client-go generate
```
6. create demo.go for test
`demo.go`
```go
package main
import (
"context"
"encoding/json"
"fmt"
"os"
// the db orm was generated by github.com/steebchen/prisma-client-go
"github.com/POABOB/go-discord/prisma/db"
"github.com/joho/godotenv"
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
if err := godotenv.Load(); err != nil {
fmt.Printf("Error loading .env file\n")
}
client := db.NewClient(
db.WithDatasourceURL(os.Getenv("DATABASE_URL")),
)
if err := client.Prisma.Connect(); err != nil {
return err
}
defer func() {
if err := client.Prisma.Disconnect(); err != nil {
panic(err)
}
}()
ctx := context.Background()
// create a post
createdPost, err := client.Post.CreateOne(
db.Post.Title.Set("Hi from Prisma!"),
db.Post.Published.Set(true),
db.Post.Desc.Set("Prisma is a database toolkit and makes databases easy."),
).Exec(ctx)
if err != nil {
return err
}
result, _ := json.MarshalIndent(createdPost, "", " ")
fmt.Printf("created post: %s\n", result)
// find a single post
post, err := client.Post.FindUnique(
db.Post.ID.Equals(createdPost.ID),
).Exec(ctx)
if err != nil {
return err
}
result, _ = json.MarshalIndent(post, "", " ")
fmt.Printf("post: %s\n", result)
// for optional/nullable values, you need to check the function and create two return values
// `desc` is a string, and `ok` is a bool whether the record is null or not. If it's null,
// `ok` is false, and `desc` will default to Go's default values; in this case an empty string (""). Otherwise,
// `ok` is true and `desc` will be "my description".
desc, ok := post.Desc()
if !ok {
return fmt.Errorf("post's description is null")
}
fmt.Printf("The posts's description is: %s\n", desc)
return nil
}
```
7. run it
```go
$ go run demo.go
created post: {
"id": "ckfnrp7ec0000oh9kygil9s94",
"createdAt": "2020-09-29T09:37:44.628Z",
"updatedAt": "2020-09-29T09:37:44.628Z",
"title": "Hi from Prisma!",
"published": true,
"desc": "Prisma is a database toolkit and makes databases easy."
}
post: {
"id": "ckfnrp7ec0000oh9kygil9s94",
"createdAt": "2020-09-29T09:37:44.628Z",
"updatedAt": "2020-09-29T09:37:44.628Z",
"title": "Hi from Prisma!",
"published": true,
"desc": "Prisma is a database toolkit and makes databases easy."
}
The posts's title is: Prisma is a database toolkit and makes databases easy.
```