diff --git a/Makefile b/Makefile index f409492..8ac1af3 100644 --- a/Makefile +++ b/Makefile @@ -16,3 +16,6 @@ bin/sqlc-gen-go.wasm: bin/sqlc-gen-go bin: mkdir -p bin + +server: bin/sqlc-gen-go + cd server && go build -o ../bin/server ./main.go \ No newline at end of file diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..f0935db --- /dev/null +++ b/server/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "context" + "log" + "net" + "os" + + "github.com/sqlc-dev/plugin-sdk-go/codegen" + pb "github.com/sqlc-dev/plugin-sdk-go/plugin" + "google.golang.org/grpc" + + golang "github.com/sqlc-dev/sqlc-gen-go/internal" +) + +func main() { + listenAddr := os.Getenv("LISTEN_ADDR") + sock, err := net.Listen("tcp", listenAddr) + if err != nil { + log.Fatalln("listening failed:", err) + } + + codegenServer := &server{handler: golang.Generate} + grpcServer := grpc.NewServer() + pb.RegisterCodegenServiceServer(grpcServer, codegenServer) + + if err := grpcServer.Serve(sock); err != nil { + log.Fatalln("serving gRPC failed:", err) + } +} + +type server struct { + pb.UnimplementedCodegenServiceServer + + handler codegen.Handler +} + +func (s *server) Generate(ctx context.Context, req *pb.GenerateRequest) (*pb.GenerateResponse, error) { + return s.handler(ctx, req) +}