fusen-gateway/server/main.go

58 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-11-02 10:40:22 +00:00
package main
import (
"context"
2023-11-21 10:30:26 +00:00
"log"
2023-11-07 03:16:29 +00:00
"fusen-basic/env"
2023-11-02 10:40:22 +00:00
"fusen-gateway/gen/go/service"
2023-11-07 03:16:29 +00:00
2023-11-02 10:40:22 +00:00
"fusen-gateway/server/config"
"fusen-gateway/server/logic"
2023-11-03 05:13:28 +00:00
2023-11-02 10:40:22 +00:00
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
2023-11-07 03:16:29 +00:00
fusen := env.NewFusen[config.Config]()
2023-11-02 10:40:22 +00:00
fusen.StartNacos(nil)
service.AutoGrpcInit(fusen)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(
2023-11-21 10:30:26 +00:00
runtime.WithErrorHandler(func(ctx context.Context, sm *runtime.ServeMux, m runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
log.Println(err)
2023-11-24 08:38:11 +00:00
w.Write([]byte("gateway: " + err.Error()))
2023-11-21 10:30:26 +00:00
}),
2023-11-16 08:24:52 +00:00
runtime.WithMetadata(logic.PassMetadata),
2023-11-02 10:40:22 +00:00
runtime.WithForwardResponseOption(logic.ResponseHeaderMatcher),
runtime.WithMarshalerOption("*", &logic.EmptyMarshaler{}),
)
opts := []grpc.DialOption{
2023-11-21 10:30:26 +00:00
grpc.WithUnaryInterceptor(func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
log.Println(method)
return invoker(ctx, method, req, reply, cc, opts...)
}),
2023-11-02 10:40:22 +00:00
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
err := logic.AutoRegisterHandler(ctx, mux, opts...)
if err != nil {
panic(err)
}
2023-11-06 03:58:57 +00:00
err = http.ListenAndServe(":9900", mux)
2023-11-02 10:40:22 +00:00
if err != nil {
panic(err)
}
}