Skip to content

Commit

Permalink
Merge pull request #39 from GreenTeaProgrammers/feature/backend/server
Browse files Browse the repository at this point in the history
feat(server): CloudStorageを利用できるようにサーバー機能を拡張
  • Loading branch information
KinjiKawaguchi authored Feb 12, 2024
2 parents d12e347 + 1f06890 commit 26d6293
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 27 deletions.
4 changes: 3 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ DB_PASSWORD=
DB_ADDR=
DB_NAME=
PORT=
MODE_DEV=
MODE_DEV=
GOOGLE_APPLICATION_CREDENTIALS=
STORAGE_BUCKET_NAME=
30 changes: 25 additions & 5 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/signal"
"syscall"

"cloud.google.com/go/storage"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/exp/slog"
Expand All @@ -19,9 +20,10 @@ import (
)

func main() {
// コンフィグを読み込む
config, _ := config.New()

// Open a connection to the database
// MySQLへの接続をテスト
db, err := sql.Open("mysql", config.DSN)
if err != nil {
log.Fatal("failed to open db connection", err)
Expand All @@ -31,7 +33,9 @@ func main() {
if err := db.Ping(); err != nil {
log.Fatalf("Failed to ping PlanetScale: %v", err)
}
log.Println("Successfully connected to PlanetScale!")

// PlanetScaleへの接続を開始
log.Println("Connecting to PlanetScale...")

mysqlConfig := &mysql.Config{
User: config.DBUser,
Expand All @@ -45,15 +49,15 @@ func main() {
InterpolateParams: true,
}

log.Println("Connecting to PlanetScale...")

entClient, err := ent.Open("mysql", mysqlConfig.FormatDSN())
if err != nil {
log.Fatal(err)
}

log.Println("Running migration...")
log.Println("Successfully connected to PlanetScale!")

// マイグレーションを実行
log.Println("Running migration...")
defer entClient.Close()
if err := entClient.Schema.Create(context.Background()); err != nil {
log.Println("Migration failed!")
Expand All @@ -62,12 +66,28 @@ func main() {

log.Println("Migration done!")

// Cloud Storageへの接続を開始
log.Println("Connecting to Cloud Storage...")
ctx := context.Background()
storageClient, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create Cloud Storage client: %v", err)
}
defer storageClient.Close()

log.Println("Connected to Cloud Storage!")

// loggerを作成
logger := slog.Default()

// gRPCサーバーを起動
srv := grpc_server.New(

grpc_server.WithLogger(logger),
grpc_server.WithEntClient(entClient),
grpc_server.WithReflection(config.ModeDev),
grpc_server.WithStorageClient(storageClient),
grpc_server.WithBucketName(config.StorageBucketName),
)
lsnr, err := net.Listen("tcp", ":"+config.GrpcPort)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
)

type Config struct {
DSN string `envconfig:"DSN" required:"true"`
DBUser string `envconfig:"DB_USER_NAME" required:"true"`
DBPassword string `envconfig:"DB_PASSWORD" required:"true"`
DBAddress string `envconfig:"DB_ADDR" required:"true"`
DBName string `envconfig:"DB_NAME" required:"true"`
GrpcPort string `envconfig:"PORT" default:"50051"`
ModeDev bool `envconfig:"MODE_DEV" default:"true"`
DSN string `envconfig:"DSN" required:"true"`
DBUser string `envconfig:"DB_USER_NAME" required:"true"`
DBPassword string `envconfig:"DB_PASSWORD" required:"true"`
DBAddress string `envconfig:"DB_ADDR" required:"true"`
DBName string `envconfig:"DB_NAME" required:"true"`
GrpcPort string `envconfig:"PORT" default:"50051"`
ModeDev bool `envconfig:"MODE_DEV" default:"true"`
GoogleApplicationCredentials string `envconfig:"GOOGLE_APPLICATION_CREDENTIALS" required:"true"`
StorageBucketName string `envconfig:"STORAGE_BUCKET_NAME" required:"true"`
}

func New() (*Config, error) {
Expand Down
35 changes: 31 additions & 4 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,48 @@ go 1.21.5

require (
entgo.io/ent v0.13.0
github.com/google/uuid v1.4.0
github.com/google/uuid v1.5.0
)

require google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
require (
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.157.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240122161410-6c6643bf1457 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
)

require (
github.com/golang/protobuf v1.5.3 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.17.0 // indirect
google.golang.org/grpc v1.61.0
google.golang.org/protobuf v1.31.0
google.golang.org/protobuf v1.32.0
)

require (
ariga.io/atlas v0.19.1-0.20240203083654-5948b60a8e43 // indirect
cloud.google.com/go/storage v1.37.0
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
Expand Down
Loading

0 comments on commit 26d6293

Please sign in to comment.