-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: cjkfyi <165058376+cjkfyi@users.noreply.github.com>
- Loading branch information
Showing
37 changed files
with
249 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ node_modules | |
.vscode-test/ | ||
*.vsix | ||
.env | ||
.archive/ | ||
|
||
# Generated files | ||
extension/build/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 🌬️ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.