| 34 | ) |
| 35 | |
| 36 | func Serve(ctx context.Context) error { |
| 37 | core.Start(ctx) |
| 38 | defer core.Stop(ctx) |
| 39 | |
| 40 | log.Info("Start user system service") |
| 41 | |
| 42 | engine := rest.NewEngine() |
| 43 | server := &http.Server{Addr: ":80", Handler: engine} |
| 44 | |
| 45 | grpcServer := grpc.NewServer() |
| 46 | |
| 47 | stopChan := make(chan struct{}) |
| 48 | go func() { |
| 49 | defer close(stopChan) |
| 50 | |
| 51 | <-ctx.Done() |
| 52 | |
| 53 | ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second) |
| 54 | defer cancel() |
| 55 | |
| 56 | if err := server.Shutdown(ctx); err != nil { |
| 57 | log.Errorf("Failed to stop server, error: %s", err) |
| 58 | } |
| 59 | |
| 60 | grpcServer.GracefulStop() |
| 61 | }() |
| 62 | |
| 63 | listen, err := net.Listen("tcp", ":8080") |
| 64 | if err != nil { |
| 65 | log.Fatalf("failed to start grpc server listener, error: %s", err) |
| 66 | } |
| 67 | |
| 68 | ext_authz_v3.RegisterAuthorizationServer(grpcServer, &userGrpcServer.AuthServer{}) |
| 69 | |
| 70 | go func() { |
| 71 | log.Infof("grpc server stating on port: %d.....", 8080) |
| 72 | if err := grpcServer.Serve(listen); err != nil { |
| 73 | log.Fatalf("failed to start grpc server, error: %s", err) |
| 74 | } |
| 75 | }() |
| 76 | |
| 77 | // pprof service, you can access it by {your_ip}:8888/debug/pprof |
| 78 | go func() { |
| 79 | router := mux.NewRouter() |
| 80 | router.Handle("/debug/pprof", http.HandlerFunc(pprof.Index)) |
| 81 | router.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline)) |
| 82 | router.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile)) |
| 83 | router.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol)) |
| 84 | router.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace)) |
| 85 | router.Handle("/debug/pprof/{cmd}", http.HandlerFunc(pprof.Index)) |
| 86 | err := http.ListenAndServe("0.0.0.0:8888", router) |
| 87 | if err != nil { |
| 88 | log.Fatal(err) |
| 89 | } |
| 90 | }() |
| 91 | |
| 92 | if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 93 | log.Errorf("Failed to start http server, error: %s", err) |