Skip to content

Commit

Permalink
feat(kb): knowledge base repository done (#18)
Browse files Browse the repository at this point in the history
Because

we need knowledge base api.

This commit
is partial implementation for 
1. fix service and repository dependency tanggled
2. create migration 
3. impl knowledge base related  db operation
4. make run-local and make build-local for local development

Note:
need some work to align the details with other backend services in the future
  • Loading branch information
Yougigun authored May 30, 2024
1 parent a91c6ec commit f6aeaae
Show file tree
Hide file tree
Showing 27 changed files with 2,410 additions and 676 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ COPY --from=docker:dind-rootless --chown=nobody:nogroup /usr/local/bin/docker /u
COPY --from=build --chown=nobody:nogroup /src/config ./config
COPY --from=build --chown=nobody:nogroup /src/release-please ./release-please
COPY --from=build --chown=nobody:nogroup /src/pkg/db/migration ./pkg/db/migration

COPY --from=build --chown=nobody:nogroup /${SERVICE_NAME}-migrate ./
COPY --from=build --chown=nobody:nogroup /${SERVICE_NAME} ./
27 changes: 25 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@ dev: ## Run dev container
--network instill-network \
--name ${SERVICE_NAME} \
instill/${SERVICE_NAME}:dev
.PHONY: run-local
run-local:
@if docker inspect --type container ${SERVICE_NAME} >/dev/null 2>&1; then \
echo "A container named ${SERVICE_NAME} is already running. \nRestarting..."; \
make rm; \
fi
@docker run -d --rm \
-p ${SERVICE_PORT}:${SERVICE_PORT} \
--network instill-network \
--name ${SERVICE_NAME} \
instill/${SERVICE_NAME}:local \
/bin/sh -c "\
./artifact-backend-migrate && \
./artifact-backend \
"

.PHONY: logs
logs: ## Tail service container logs with -n 10
@docker logs ${SERVICE_NAME} --follow --tail=10
@docker logs ${SERVICE_NAME} --follow

.PHONY: stop
stop: ## Stop container
Expand All @@ -36,7 +51,6 @@ rm: ## Remove container
.PHONY: top
top: ## Display all running service processes
@docker top ${SERVICE_NAME}

.PHONY: build
build: ## Build dev docker image
@docker build \
Expand All @@ -45,6 +59,15 @@ build: ## Build dev docker image
--build-arg K6_VERSION=${K6_VERSION} \
-f Dockerfile.dev -t instill/${SERVICE_NAME}:dev .

.PHONY: build-local
build-local: ## Build dev docker image
@docker build \
--no-cache \
--build-arg SERVICE_NAME=${SERVICE_NAME} \
--build-arg GOLANG_VERSION=${GOLANG_VERSION} \
--build-arg K6_VERSION=${K6_VERSION} \
-f Dockerfile -t instill/${SERVICE_NAME}:local .

.PHONY: go-gen
go-gen: ## Generate codes
go generate ./...
Expand Down
7 changes: 6 additions & 1 deletion cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import (

var propagator propagation.TextMapPropagator

// grpcHandlerFunc handles incoming HTTP requests and routes them to either the gRPC server or the gateway handler.
// It wraps the handler function with h2c.NewHandler to support HTTP/2 requests.
// The function extracts the B3 context from the incoming request headers and sets it in the request context.
// If the request is a gRPC request, it calls the gRPC server's ServeHTTP method.
// Otherwise, it calls the gateway handler's ServeHTTP method.
func grpcHandlerFunc(grpcServer *grpc.Server, gwHandler http.Handler) http.Handler {
return h2c.NewHandler(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -179,7 +184,7 @@ func main() {
reflection.Register(publicGrpcS)
artifactPB.RegisterArtifactPublicServiceServer(
publicGrpcS,
handler.NewPublicHandler(ctx),
handler.NewPublicHandler(ctx, service),
)

privateGrpcS := grpc.NewServer(grpcServerOpts...)
Expand Down
26 changes: 18 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,27 @@ func Init() error {
fileRelativePath := fs.String("file", "config/config.yaml", "configuration file")
flag.Parse()

if err := k.Load(file.Provider(*fileRelativePath), parser); err != nil {
if err := k.Load(
file.Provider(*fileRelativePath),
parser,
); err != nil {
log.Fatal(err.Error())
}

if err := k.Load(env.ProviderWithValue("CFG_", ".", func(s string, v string) (string, interface{}) {
key := strings.Replace(strings.ToLower(strings.TrimPrefix(s, "CFG_")), "_", ".", -1)
if strings.Contains(v, ",") {
return key, strings.Split(strings.TrimSpace(v), ",")
}
return key, v
}), nil); err != nil {
if err := k.Load(
env.ProviderWithValue(
"CFG_",
".",
func(k string, v string) (string, interface{}) {
key := strings.Replace(strings.ToLower(strings.TrimPrefix(k, "CFG_")), "_", ".", -1)
if strings.Contains(v, ",") {
return key, strings.Split(strings.TrimSpace(v), ",")
}
return key, v
},
),
nil,
); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ database:
host: pg-sql
port: 5432
name: artifact
version: 1
version: 2
timezone: Etc/UTC
pool:
idleconnections: 5
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240408123548-9a2337cdfe04
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240527081147-f7cc66e329f9
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c
github.com/knadh/koanf v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.12.3 h1:28nRlNMRIV4QbtIUvxhWqaxn0
github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiGQg4m2GbkaeJDcuWoxiWdQEbA0=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU=
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240408123548-9a2337cdfe04 h1:/i0RjGbDDAjc9SPYMoemjLfSHNX8S+Rau6Y8orhLiv8=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240408123548-9a2337cdfe04/go.mod h1:jhEL0SauySMoPLVvx105DWyThju9sYTbsXIySVCArmM=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240527081147-f7cc66e329f9 h1:bjvRlomNglZpKsxd5Fv+YhoRyFNAD56hVVReesaXqdI=
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20240527081147-f7cc66e329f9/go.mod h1:2blmpUwiTwxIDnrjIqT6FhR5ewshZZF554wzjXFvKpQ=
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61 h1:smPTvmXDhn/QC7y/TPXyMTqbbRd0gvzmFgWBChwTfhE=
github.com/instill-ai/usage-client v0.3.0-alpha.0.20240319060111-4a3a39f2fd61/go.mod h1:/TAHs4ybuylk5icuy+MQtHRc4XUnIyXzeNKxX9qDFhw=
github.com/instill-ai/x v0.3.0-alpha.0.20231219052200-6230a89e386c h1:a2RVkpIV2QcrGnSHAou+t/L+vBsaIfFvk5inVg5Uh4s=
Expand Down
1 change: 1 addition & 0 deletions pkg/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ const MaxPayloadSize = 1024 * 1024 * 32

// Constants for resource owner
const DefaultUserID string = "admin"
const HeaderUserUIDKey = "Instill-User-Uid"
2 changes: 1 addition & 1 deletion pkg/service/errors.go → pkg/customerror/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package service
package customerror

import "errors"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE knowledge_base;
22 changes: 22 additions & 0 deletions pkg/db/migration/000002_create_knowledge_base_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE TABLE knowledge_base (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
kb_id VARCHAR(255) NOT NULL UNIQUE,
name VARCHAR(255) NOT NULL,
description VARCHAR(1023),
tags VARCHAR(255)[],
owner VARCHAR(255) NOT NULL,
create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
delete_time TIMESTAMP
);

COMMENT ON TABLE knowledge_base IS 'Table to store knowledge base information';
COMMENT ON COLUMN knowledge_base.id IS 'Primary key, auto-generated UUID';
COMMENT ON COLUMN knowledge_base.name IS 'Name of the knowledge base, up to 255 characters';
COMMENT ON COLUMN knowledge_base.kb_id IS 'Unique identifier for the knowledge base, up to 255 characters';
COMMENT ON COLUMN knowledge_base.description IS 'Description of the knowledge base, up to 1023 characters';
COMMENT ON COLUMN knowledge_base.update_time IS 'Timestamp of the last update, stored in UTC';
COMMENT ON COLUMN knowledge_base.create_time IS 'Timestamp when the entry was created, stored in UTC';
COMMENT ON COLUMN knowledge_base.owner IS 'Owner of the knowledge base, up to 255 characters';
COMMENT ON COLUMN knowledge_base.tags IS 'Array of tags associated with the knowledge base';
COMMENT ON COLUMN knowledge_base.delete_time IS 'Timestamp when the entry was marked as deleted, stored in UTC';
31 changes: 31 additions & 0 deletions pkg/handler/knowledgebase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handler

import (
"context"
"fmt"

"google.golang.org/grpc/metadata"

artifactpb "github.com/instill-ai/protogen-go/artifact/artifact/v1alpha"
)

func (ph *PublicHandler) CreateKnowledgeBase(ctx context.Context, req *artifactpb.CreateKnowledgeBaseRequest) (*artifactpb.CreateKnowledgeBaseResponse, error) {
fmt.Println("CreateKnowledgeBase")
return nil, nil
}
func (ph *PublicHandler) GetKnowledgeBases(ctx context.Context, req *artifactpb.GetKnowledgeBasesRequest) (*artifactpb.GetKnowledgeBasesResponse, error) {
fmt.Println("GetKnowledgeBases")
return nil, nil
}
func (ph *PublicHandler) UpdateKnowledgeBase(ctx context.Context, req *artifactpb.UpdateKnowledgeBaseRequest) (*artifactpb.UpdateKnowledgeBaseResponse, error) {
fmt.Println("UpdateKnowledgeBase")
return nil, nil
}
func (ph *PublicHandler) DeleteKnowledgeBase(ctx context.Context, req *artifactpb.DeleteKnowledgeBaseRequest) (*artifactpb.DeleteKnowledgeBaseResponse, error) {
// get header
md, _ := metadata.FromIncomingContext(ctx)
fmt.Println(md)

fmt.Println("DeleteKnowledgeBase")
return nil, nil
}
15 changes: 11 additions & 4 deletions pkg/handler/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ package handler

import (
"context"
"fmt"

artifact "github.com/instill-ai/artifact-backend/pkg/service"
artifactpb "github.com/instill-ai/protogen-go/artifact/artifact/v1alpha"
healthcheckPB "github.com/instill-ai/protogen-go/common/healthcheck/v1beta"
)

// PublicHandler handles public API
type PublicHandler struct {
artifactpb.UnimplementedArtifactPublicServiceServer
ctx context.Context
service *artifact.Service
}

// NewPublicHandler initiates a handler instance
func NewPublicHandler(_ context.Context) artifactpb.ArtifactPublicServiceServer {
return &PublicHandler{}
func NewPublicHandler(ctx context.Context, service *artifact.Service) artifactpb.ArtifactPublicServiceServer {
return &PublicHandler{
ctx: ctx,
service: service}
}

// Liveness returns the health of the service.
func (h *PublicHandler) Liveness(_ context.Context, _ *artifactpb.LivenessRequest) (*artifactpb.LivenessResponse, error) {
func (ph *PublicHandler) Liveness(_ context.Context, _ *artifactpb.LivenessRequest) (*artifactpb.LivenessResponse, error) {
fmt.Println("Liveness")
return &artifactpb.LivenessResponse{
HealthCheckResponse: &healthcheckPB.HealthCheckResponse{
Status: healthcheckPB.HealthCheckResponse_SERVING_STATUS_SERVING,
Expand All @@ -27,7 +34,7 @@ func (h *PublicHandler) Liveness(_ context.Context, _ *artifactpb.LivenessReques
}

// Readiness returns the state of the service.
func (h *PublicHandler) Readiness(_ context.Context, _ *artifactpb.ReadinessRequest) (*artifactpb.ReadinessResponse, error) {
func (ph *PublicHandler) Readiness(_ context.Context, _ *artifactpb.ReadinessRequest) (*artifactpb.ReadinessResponse, error) {
return &artifactpb.ReadinessResponse{
HealthCheckResponse: &healthcheckPB.HealthCheckResponse{
Status: healthcheckPB.HealthCheckResponse_SERVING_STATUS_SERVING,
Expand Down
12 changes: 6 additions & 6 deletions pkg/middleware/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"

"github.com/instill-ai/artifact-backend/pkg/acl"
"github.com/instill-ai/artifact-backend/pkg/customerror"
"github.com/instill-ai/artifact-backend/pkg/handler"
"github.com/instill-ai/artifact-backend/pkg/service"
"github.com/instill-ai/x/errmsg"
)

Expand Down Expand Up @@ -89,7 +89,7 @@ func AsGRPCError(err error) error {
code = codes.AlreadyExists
case
errors.Is(err, gorm.ErrRecordNotFound),
errors.Is(err, service.ErrNotFound),
errors.Is(err, customerror.ErrNotFound),
errors.Is(err, acl.ErrMembershipNotFound):

code = codes.NotFound
Expand All @@ -98,7 +98,7 @@ func AsGRPCError(err error) error {
errors.Is(err, handler.ErrCheckUpdateImmutableFields),
errors.Is(err, handler.ErrCheckOutputOnlyFields),
errors.Is(err, handler.ErrCheckRequiredFields),
errors.Is(err, service.ErrExceedMaxBatchSize),
errors.Is(err, customerror.ErrExceedMaxBatchSize),
errors.Is(err, handler.ErrFieldMask),
errors.Is(err, handler.ErrResourceID),
errors.Is(err, handler.ErrSematicVersion),
Expand All @@ -107,16 +107,16 @@ func AsGRPCError(err error) error {

code = codes.InvalidArgument
case
errors.Is(err, service.ErrNoPermission):
errors.Is(err, customerror.ErrNoPermission):

code = codes.PermissionDenied
case
errors.Is(err, service.ErrUnauthenticated):
errors.Is(err, customerror.ErrUnauthenticated):

code = codes.Unauthenticated

case
errors.Is(err, service.ErrRateLimiting):
errors.Is(err, customerror.ErrRateLimiting):

code = codes.ResourceExhausted
default:
Expand Down
2 changes: 1 addition & 1 deletion pkg/mock/generator.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mock

//go:generate minimock -g -i github.com/instill-ai/artifact-backend/pkg/service.RegistryClient -o ./ -s "_mock.gen.go"
//go:generate minimock -g -i github.com/instill-ai/artifact-backend/pkg/service.Repository -o ./ -s "_mock.gen.go"
//go:generate minimock -g -i github.com/instill-ai/artifact-backend/pkg/repository.RepositoryI -o ./ -s "_mock.gen.go"
Loading

0 comments on commit f6aeaae

Please sign in to comment.