Skip to content

Commit

Permalink
Setup protobuf for web client
Browse files Browse the repository at this point in the history
  • Loading branch information
nandesh-dev committed Oct 18, 2024
1 parent 78304b4 commit 684d7ce
Show file tree
Hide file tree
Showing 12 changed files with 2,731 additions and 3,585 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.PHONY: proto run

proto:
rm -rf generated
mkdir generated
protoc ./proto/**/*.proto --go_out=. --go-grpc_out=.
rm -rf web/generated
mkdir web/generated
protoc --grpc-web_out=import_style=typescript,mode=grpcwebtext:web/generated/ proto/**/*.proto

run:
go run ./cmd/subtle
13 changes: 3 additions & 10 deletions cmd/subtle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"fmt"
"log"

"github.com/nandesh-dev/subtle/internal/routine/extract"
"github.com/nandesh-dev/subtle/internal/routine/media"
"github.com/nandesh-dev/subtle/internal/server"
"github.com/nandesh-dev/subtle/pkgs/config"
"github.com/nandesh-dev/subtle/pkgs/db"
)
Expand All @@ -21,13 +20,7 @@ func main() {
log.Fatal(err)
}

fmt.Println("Running media routine")
media.Run()
server := server.New()

fmt.Println("Running extract routine")
warns := extract.Run()

for _, warning := range warns.Warnings() {
fmt.Println(warning)
}
server.Listen(3000, true)
}
15 changes: 0 additions & 15 deletions internal/server/library/library.go

This file was deleted.

53 changes: 53 additions & 0 deletions internal/server/media/get_directory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package media

import (
"context"

"github.com/nandesh-dev/subtle/generated/api/media"
"github.com/nandesh-dev/subtle/pkgs/config"
"github.com/nandesh-dev/subtle/pkgs/filemanager"
)

func (s *MediaServiceServer) GetDirectory(ctx context.Context, req *media.GetDirectoryRequest) (*media.GetDirectoryResponse, error) {
if req.Path == "/" || req.Path == "" {
res := media.GetDirectoryResponse{
Directories: make([]*media.Directory, 0),
Videos: make([]*media.Video, 0),
}

for _, rootDirectory := range config.Config().Media.RootDirectories {
res.Directories = append(res.Directories, &media.Directory{
Path: rootDirectory.Path,
})
}

return &res, nil
}

res := media.GetDirectoryResponse{
Directories: make([]*media.Directory, 0),
}

dir, _, _ := filemanager.ReadDirectory(req.Path)

for _, child := range dir.Children() {
res.Directories = append(res.Directories, &media.Directory{
Path: child.Path(),
})
}

for _, video := range dir.VideoFiles() {
res.Videos = append(res.Videos, &media.Video{
Name: video.Basename(),
Extension: video.Extension(),
})
}

return &res, nil
}

func compileResponseDirectory(directory filemanager.Directory) *media.Directory {
return &media.Directory{
Path: directory.Path(),
}
}
9 changes: 9 additions & 0 deletions internal/server/media/media.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package media

import (
"github.com/nandesh-dev/subtle/generated/api/media"
)

type MediaServiceServer struct {
media.UnsafeMediaServiceServer
}
8 changes: 4 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"net"

"github.com/nandesh-dev/subtle/generated/api/library"
library_service "github.com/nandesh-dev/subtle/internal/server/library"
"github.com/nandesh-dev/subtle/generated/api/media"
media_service "github.com/nandesh-dev/subtle/internal/server/media"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
Expand Down Expand Up @@ -33,8 +33,8 @@ func (s *server) Listen(port int, enableReflection bool) error {
reflection.Register(s.grpcServer)
}

libraryService := library_service.LibraryServiceServer{}
library.RegisterLibraryServiceServer(s.grpcServer, &libraryService)
mediaService := media_service.MediaServiceServer{}
media.RegisterMediaServiceServer(s.grpcServer, &mediaService)

if err := s.grpcServer.Serve(listener); err != nil {
return fmt.Errorf("failed to serve: %v", err)
Expand Down
25 changes: 0 additions & 25 deletions proto/library/library.proto

This file was deleted.

27 changes: 27 additions & 0 deletions proto/media/media.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";

package media;

option go_package = "generated/api/media";

service MediaService {
rpc GetDirectory(GetDirectoryRequest) returns (GetDirectoryResponse);
}

message GetDirectoryRequest {
string path = 1;
}

message Video {
string name = 1;
string extension = 2;
}

message Directory {
string path = 1;
}

message GetDirectoryResponse {
repeated Directory directories = 1;
repeated Video videos = 2;
}
29 changes: 17 additions & 12 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
pkgs ? import <nixpkgs> { },
}:
pkgs.mkShell {
nativeBuildInputs = with pkgs.buildPackages; [
go
ffmpeg_7-headless
tesseract
leptonica
protobuf
protoc-gen-go-grpc
protoc-gen-go
graphviz
nodejs_22
pnpm
];
nativeBuildInputs = (
with pkgs.buildPackages;
[
go
ffmpeg_7-headless
tesseract
leptonica
protobuf
protoc-gen-go-grpc
protoc-gen-go
protoc-gen-grpc-web
protoc-gen-js
graphviz
nodejs_22
pnpm
]
);
}
2 changes: 2 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"preview": "vite preview"
},
"dependencies": {
"google-protobuf": "^3.21.4",
"grpc-web": "^1.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-responsive": "^10.0.0",
Expand Down
Loading

0 comments on commit 684d7ce

Please sign in to comment.