Skip to content

Commit

Permalink
Refreshed some?
Browse files Browse the repository at this point in the history
Signed-off-by: cjkfyi <165058376+cjkfyi@users.noreply.github.com>
  • Loading branch information
cjkfyi committed Mar 31, 2024
1 parent 3e48b0c commit 8bc7e02
Show file tree
Hide file tree
Showing 37 changed files with 249 additions and 94 deletions.
1 change: 0 additions & 1 deletion .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ node_modules
.vscode-test/
*.vsix
.env
.archive/

# Generated files
extension/build/*
Expand Down
4 changes: 1 addition & 3 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# GEMIFY

> v0.4.2
Yet another coding assistant 🌬️
> Yet another coding assistant 🌬️
6 changes: 4 additions & 2 deletions backend/api/api.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
)

var (
prox_port = flag.Int("prox_port", 8080, "Proxy server port")
prox_port = flag.Int("prox_port", 8000, "Proxy server port")
gRPC_port = flag.Int("grpc_port", 50051, "gRPC server port")
store *Store
client *genai.Client
)

func init() { // $PWD/.env
flag.Parse()

err := godotenv.Load()
if err != nil { // Check if file exists
log.Fatal("Error loading .env file")
Expand All @@ -34,7 +36,7 @@ func init() { // $PWD/.env
if err != nil {
log.Fatal("Error initializing data store: ", err)
}
store = ds
store = ds // vvv breaks
// defer store.Close()

ctx := context.Background()
Expand Down
Empty file modified backend/api/gen/.gitkeep
100644 → 100755
Empty file.
48 changes: 23 additions & 25 deletions backend/api/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"context"
"flag"
"fmt"
"log"
"net"
Expand All @@ -16,6 +15,29 @@ import (
pb "gemify/api/gen"
)

func SetupGRPC() (*grpc.Server, net.Listener, string, error) {
// gRPC address
var port = *gRPC_port
var host = "127.0.0.1"
var addr = fmt.Sprintf(
"%v:%v",
host,
port,
)
// Create a listener for that address
lis, err := net.Listen("tcp", addr)
if err != nil { // Wrap error
return nil, nil, "", fmt.Errorf("failed to listen: %w", err)
}

grpcSvr := grpc.NewServer()
reflection.Register(grpcSvr) // Only TEST env
pb.RegisterGeminiAPIServer(grpcSvr, &server{})

// Return on success
return grpcSvr, lis, addr, nil
}

type server struct {
pb.UnimplementedGeminiAPIServer
}
Expand Down Expand Up @@ -63,30 +85,6 @@ func (s *server) SendMessage(ctx context.Context, in *pb.Message) (*pb.Message,
return botReply, nil
}

func StartGRPCServer() error {
flag.Parse() // gRPC_port

// Create the listener for the gRPC server address constructed
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *gRPC_port))
if err != nil {
fmt.Printf("failed to listen: %v", err)
return err
}

s := grpc.NewServer() // Basic initialization
reflection.Register(s) // Only for test env
pb.RegisterGeminiAPIServer(s, &server{})

fmt.Printf("\n🌵 gRPC live at: 127.0.0.1:%v\n\n", *gRPC_port)

// Serve gRPC or kick upwards an error
if err := s.Serve(lis); err != nil {
fmt.Printf("failed to serve: %v", err)
return err
} // goofy
return nil
}

func printResponse(resp *genai.GenerateContentResponse) string {
var output []string
// List the different parts, construct
Expand Down
Empty file modified backend/api/proto/api.proto
100644 → 100755
Empty file.
62 changes: 56 additions & 6 deletions backend/api/proxy.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,71 @@ import (
"net/http"

"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

pb "gemify/api/gen"
)

func StartHTTPProxy() error {
flag.Parse() // prox_port
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}

func SetupProxy() (*http.Server, string, error) {
// Proxy address
var port = *prox_port
var host = "127.0.0.1"
var addr = fmt.Sprintf(
"%v:%v",
host,
port,
)
// Chi router instance
r := chi.NewRouter()
r.Post("/api/gemini", GeminiHandler)
fmt.Printf("\n🌵 Proxy live at: 127.0.0.1:%v\n", *prox_port)
return http.ListenAndServe(fmt.Sprintf(":%v", *prox_port), r)
// Chi proxy routes
r.Get("/ws", websocketHandler)
r.Post("/api/gemini", geminiHandler)

// Construct the server
proxySvr := http.Server{
Addr: addr,
Handler: r,
}

// Return on success
return &proxySvr, addr, nil
}

func websocketHandler(w http.ResponseWriter, r *http.Request) {

ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
fmt.Println("Error during WebSocket upgrade:", err) // Log the error
return
}
defer ws.Close()

// Simple echo logic for testing
for {
messageType, message, err := ws.ReadMessage()
if err != nil {
fmt.Println("Error reading message:", err)
break
}

fmt.Printf("Received: %s\n", message)

err = ws.WriteMessage(messageType, message)
if err != nil {
fmt.Println("Error sending message:", err)
break
}
}
}

func GeminiHandler(w http.ResponseWriter, r *http.Request) {
func geminiHandler(w http.ResponseWriter, r *http.Request) {
flag.Parse() // gRPC_port

// Establish gRPC conn
Expand Down
Empty file modified backend/api/store.go
100644 → 100755
Empty file.
126 changes: 96 additions & 30 deletions backend/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,112 @@
package main

import (
"context"
"fmt"
"log"
"sync"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"golang.org/x/sync/errgroup"

"gemify/api"
)

func main() {
ctx, cancel := signal.NotifyContext(
context.Background(),
os.Interrupt,
syscall.SIGTERM,
)
defer cancel()

// Initialize WaitGroup
var wg sync.WaitGroup
//

wg.Add(1)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
log.Printf("HTTP proxy server panicked: %v", r)
// recovery for the Proxy server
}
}()
// Start Proxy Server
if err := api.StartHTTPProxy(); err != nil {
log.Fatalf("HTTP proxy server exited with error: %v", err)
// Server initialization
proxySvr, proxyAddr, err := api.SetupProxy()
if err != nil {
log.Fatal(err)
}
grpcSvr, grpcLis, grpcAddr, err := api.SetupGRPC()
if err != nil {
log.Fatal(err)
}

//

g, gCtx := errgroup.WithContext(ctx)

errChanProxy := make(chan error)
errChanGRPC := make(chan error)

//

// Launch servers
g.Go(func() error {
fmt.Printf("\n 🌱 Proxy Server running @ %v\n", proxyAddr)
err := proxySvr.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
errChanProxy <- err
}
}()

wg.Add(2)
go func() {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
log.Printf("gRPC server panicked: %v", r)
// recovery for the gRPC server
return nil
})
g.Go(func() error {
fmt.Printf("\n 🌱 gRPC Server running @ %v\n\n", grpcAddr)
err := grpcSvr.Serve(grpcLis)
if err != nil {
errChanGRPC <- err
}
return nil
})

//

// Error monitoring
g.Go(func() error {
for {
select {
case err := <-errChanProxy:
fmt.Println("Proxy server error:", err)
cancel() // Call the original cancel function
case err := <-errChanGRPC:
fmt.Println("gRPC server error:", err)
cancel() // Call the original cancel function
case <-gCtx.Done():
return gCtx.Err()
}
}()
// Start gRPC Server
if err := api.StartGRPCServer(); err != nil {
log.Fatalf("gRPC server exited with error: %v", err)
}
}()
})

// Shutdown logic
g.Go(func() error {
<-gCtx.Done()

shutdownCtx, cancelShutdown := context.WithTimeout(context.Background(), 30*time.Second)
defer cancelShutdown()

// Collect any errors
var shutdownErrors []error

// Shutdown proxy server
if err := proxySvr.Shutdown(shutdownCtx); err != nil {
shutdownErrors = append(shutdownErrors, err)
}

// Shutdown gRPC server
grpcSvr.GracefulStop()

// Return a more appropriate error
if len(shutdownErrors) > 0 {
return fmt.Errorf("multiple shutdown errors: %v", shutdownErrors)
}
return nil
})

wg.Wait()
// Wait for all goroutines (errgroup)
if err := g.Wait(); err != nil {
log.Fatalf("Exit due to err: %v", err)
}
}
Empty file modified backend/data/.gitkeep
100644 → 100755
Empty file.
15 changes: 9 additions & 6 deletions backend/go.mod
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ go 1.22.1
require (
github.com/go-chi/chi/v5 v5.0.12
github.com/google/generative-ai-go v0.10.0
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
google.golang.org/api v0.171.0
go.mills.io/bitcask/v2 v2.0.3
golang.org/x/sync v0.6.0
google.golang.org/api v0.172.0
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
)

require (
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go v0.112.2 // indirect
cloud.google.com/go/ai v0.3.4 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
Expand All @@ -27,26 +30,26 @@ require (
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.3 // indirect
github.com/hashicorp/go-immutable-radix/v2 v2.0.0 // indirect
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattetti/filebuffer v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.mills.io/bitcask/v2 v2.0.3 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240325203815-454cdb8f5daa // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240325203815-454cdb8f5daa // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)
Loading

0 comments on commit 8bc7e02

Please sign in to comment.