grpc-api-example/main.go

123 lines
3.2 KiB
Go
Raw Permalink Normal View History

2024-02-26 10:12:30 +00:00
package main
import (
2024-02-27 19:21:35 +00:00
"context"
2024-02-26 10:12:30 +00:00
"errors"
2024-02-27 19:21:35 +00:00
"flag"
"fmt"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/sundowndev/grpc-api-example/gen"
notesv1 "github.com/sundowndev/grpc-api-example/proto/notes/v1"
2024-02-26 10:12:30 +00:00
"github.com/sundowndev/grpc-api-example/server"
2024-02-27 19:21:35 +00:00
"google.golang.org/grpc"
2024-02-26 13:57:19 +00:00
"google.golang.org/grpc/credentials/insecure"
2024-02-26 10:12:30 +00:00
"google.golang.org/grpc/grpclog"
"io"
2024-02-27 19:21:35 +00:00
"io/fs"
"mime"
2024-02-26 10:12:30 +00:00
"net"
2024-02-27 19:21:35 +00:00
"net/http"
2024-02-26 10:12:30 +00:00
"os"
"os/signal"
2024-02-27 19:21:35 +00:00
"strings"
2024-02-26 10:12:30 +00:00
"syscall"
)
2024-02-27 19:21:35 +00:00
var (
httpServerEndpoint = flag.String("http-server-endpoint", "localhost:8000", "HTTP server endpoint")
grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:9090", "gRPC server endpoint")
)
func getOpenAPIHandler() (http.Handler, error) {
err := mime.AddExtensionType(".svg", "image/svg+xml")
if err != nil {
return nil, err
}
// Use subdirectory in embedded files
subFS, err := fs.Sub(gen.OpenAPI, "openapiv2")
if err != nil {
return nil, fmt.Errorf("couldn't create sub filesystem: %v", err)
}
return http.FileServer(http.FS(subFS)), nil
}
func httpServer(ctx context.Context, addr string) error {
// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()), // TODO: Replace with your own certificate!
}
// Register services
err := notesv1.RegisterNotesServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
if err != nil {
return err
}
oa, err := getOpenAPIHandler()
if err != nil {
return err
}
gwServer := &http.Server{
Addr: *httpServerEndpoint,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api") {
mux.ServeHTTP(w, r)
return
}
oa.ServeHTTP(w, r)
}),
}
// Start HTTP server (and proxy calls to gRPC server endpoint)
return gwServer.ListenAndServe()
}
2024-02-26 10:12:30 +00:00
func main() {
2024-02-27 19:21:35 +00:00
flag.Parse()
2024-02-26 10:12:30 +00:00
// Adds gRPC internal logs. This is quite verbose, so adjust as desired!
log := grpclog.NewLoggerV2(os.Stdout, io.Discard, io.Discard)
grpclog.SetLoggerV2(log)
2024-02-27 14:42:25 +00:00
srv, err := server.NewServer(insecure.NewCredentials()) // TODO: Replace with your own certificate!
if err != nil {
log.Fatal("failed to initialize server: %v", err)
}
2024-02-26 10:12:30 +00:00
// Serve gRPC Server
2024-02-27 19:21:35 +00:00
log.Info("Serving gRPC on https://", *grpcServerEndpoint)
2024-02-26 10:12:30 +00:00
go func() {
2024-02-27 19:21:35 +00:00
if err := srv.Listen(*grpcServerEndpoint); err != nil && !errors.Is(err, net.ErrClosed) {
2024-02-26 10:12:30 +00:00
log.Fatalf("listen: %s\n", err)
}
}()
2024-02-27 19:21:35 +00:00
// Serve HTTP gateway
ctx, cancel := context.WithCancel(context.Background())
go func() {
if err := httpServer(ctx, *httpServerEndpoint); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("http server: %s\n", err)
}
}()
2024-02-26 10:12:30 +00:00
// Wait for interrupt signal to gracefully shut down the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can't be caught, so don't need to add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
2024-02-27 19:21:35 +00:00
cancel()
2024-02-26 10:12:30 +00:00
if err := srv.Close(); err != nil {
log.Fatal(err)
}
2024-02-26 13:57:19 +00:00
log.Info("graceful shut down succeeded")
2024-02-26 10:12:30 +00:00
}