Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement ListCatalog service #14

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ build-cli:

.PHONY: gen-proto
gen-proto:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative \
pkg/proto/catalogservice/service.proto
protoc \
-I . \
-I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=require_unimplemented_servers=false:. --go-grpc_opt=paths=source_relative \
--validate_out="lang=go:." --validate_opt=paths=source_relative \
pkg/proto/catalogservice/service.proto \
pkg/datastore/model/catalog.proto
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ module github.com/oam-dev/velacp
go 1.15

require (
github.com/go-sql-driver/mysql v1.5.0
github.com/golang/protobuf v1.4.3 // indirect
github.com/pkg/errors v0.8.1
github.com/envoyproxy/protoc-gen-validate v0.4.1
github.com/golang/protobuf v1.4.3
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.1.1
go.mongodb.org/mongo-driver v1.4.5 // indirect
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20210126160654-44e461bb6506 // indirect
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
gorm.io/driver/mysql v1.0.4
gorm.io/gorm v1.20.12
)
105 changes: 100 additions & 5 deletions go.sum

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions pkg/commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/oam-dev/velacp/pkg/datastore"
"github.com/oam-dev/velacp/pkg/datastore/mongodb"
"github.com/oam-dev/velacp/pkg/grpcapi"
"github.com/spf13/cobra"
"go.uber.org/zap"
Expand All @@ -15,10 +16,9 @@ type server struct {
grpcApiCfg grpcapi.Config
}


func NewServerCommand() *cobra.Command {
s := &server{}
logger:= newLogger()
logger := newLogger()
s.logger = logger
s.grpcApiCfg.Logger = logger

Expand All @@ -34,19 +34,19 @@ func NewServerCommand() *cobra.Command {
cmd.Flags().IntVar(&s.grpcApiCfg.Port, "api-port", s.grpcApiCfg.Port, "The port number used to serve the grpc APIs.")

// datastore
cmd.Flags().StringVar(&s.dataStoreCfg.User, "db-user", s.dataStoreCfg.User, "Username for database login")
cmd.Flags().StringVar(&s.dataStoreCfg.User, "db-user", s.dataStoreCfg.User, "The username for database login")
cmd.MarkFlagRequired("db-user")
cmd.Flags().StringVar(&s.dataStoreCfg.Password, "db-password", s.dataStoreCfg.Password, "Password for database login")
cmd.Flags().StringVar(&s.dataStoreCfg.Password, "db-password", s.dataStoreCfg.Password, "The password for database login")
cmd.MarkFlagRequired("db-password")
cmd.Flags().StringVar(&s.dataStoreCfg.Address, "db-address", s.dataStoreCfg.Address, "The address of the database")
cmd.MarkFlagRequired("db-address")
cmd.Flags().StringVar(&s.dataStoreCfg.DBName, "db-name", s.dataStoreCfg.DBName, "Database name")
cmd.Flags().StringVar(&s.dataStoreCfg.Database, "db-name", s.dataStoreCfg.Database, "The name of the database")
cmd.MarkFlagRequired("db-name")

return cmd
}

func newLogger() *zap.Logger{
func newLogger() *zap.Logger {
c := zap.Config{
Level: zap.NewAtomicLevel(),
Development: false,
Expand All @@ -58,7 +58,7 @@ func newLogger() *zap.Logger{
ErrorOutputPaths: []string{"stderr"},
}
var opt []zap.Option
l, err := c.Build(opt...)
l, err := c.Build(opt...)
if err != nil {
panic(err)
}
Expand All @@ -68,7 +68,7 @@ func newLogger() *zap.Logger{
func (s *server) run() error {
ctx := context.Background()

d, err := datastore.New(s.dataStoreCfg)
d, err := mongodb.New(ctx, s.dataStoreCfg)
if err != nil {
return err
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/datastore/catalogstore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package datastore

import (
"context"

"github.com/oam-dev/velacp/pkg/datastore/model"
)

const catalogKind = "Catalog"

type CatalogStore interface {
ListCatalogs(ctx context.Context) ([]*model.Catalog, error)
}

func NewCatalogStore(ds DataStore) CatalogStore {
return &catalogStore{ds: ds}
}

type catalogStore struct {
ds DataStore
}

func (c *catalogStore) ListCatalogs(ctx context.Context) ([]*model.Catalog, error) {
iter, err := c.ds.Find(ctx, catalogKind)
if err != nil {
return nil, err
}
cs := make([]*model.Catalog, 0)
for iter.Next() {
var c model.Catalog
err := iter.Decode(&c)
if err != nil {
return nil, err
}
cs = append(cs, &c)
}
return cs, nil
}
30 changes: 9 additions & 21 deletions pkg/datastore/datastore.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
package datastore

import (
"database/sql"
"fmt"

_ "github.com/go-sql-driver/mysql"
)
import "context"

type Config struct {
User string
Password string
Address string
DBName string
Database string
}

type DataStore interface {
// Find executes a find commandand returns an iterator over the matching items.
Find(ctx context.Context, kind string) (Iterator, error)
}

type mysqlStore struct {
db *sql.DB
}

func New(cfg Config) (DataStore, error) {
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@tcp(%s)/%s", cfg.User, cfg.Password, cfg.Address, cfg.DBName))
if err != nil {
return nil, err
}
type Iterator interface {
// Next gets the next item for this cursor.
Next(ctx context.Context) bool

s := mysqlStore{
db: db,
}
return s, nil
// Decode will unmarshal the current item into given value.
Decode(value interface{}) error
}
194 changes: 194 additions & 0 deletions pkg/datastore/model/catalog.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading