|
@@ -1,26 +1,26 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
|
+ "app.yhyue.com/moapp/jybase/go-xweb/xweb"
|
|
|
"context"
|
|
|
"fmt"
|
|
|
+ "google.golang.org/grpc"
|
|
|
+ "google.golang.org/grpc/health"
|
|
|
+ "google.golang.org/grpc/health/grpc_health_v1"
|
|
|
+ "google.golang.org/grpc/keepalive"
|
|
|
"log"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"os/signal"
|
|
|
+ . "rpc/chat"
|
|
|
"rpc/config"
|
|
|
+ _ "rpc/filter"
|
|
|
"rpc/service"
|
|
|
"sync"
|
|
|
"syscall"
|
|
|
"time"
|
|
|
-
|
|
|
- "google.golang.org/grpc"
|
|
|
- "google.golang.org/grpc/health"
|
|
|
- "google.golang.org/grpc/health/grpc_health_v1"
|
|
|
- "google.golang.org/grpc/keepalive"
|
|
|
- _ "rpc/a"
|
|
|
- . "rpc/chat"
|
|
|
- _ "rpc/filter"
|
|
|
)
|
|
|
|
|
|
type server struct {
|
|
@@ -47,38 +47,46 @@ func main() {
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
defer cancel()
|
|
|
|
|
|
+ go func() {
|
|
|
+ srv.initGRPCServer()
|
|
|
+ // 启动服务
|
|
|
+ srv.startServices(ctx)
|
|
|
+ // 设置定时任务
|
|
|
+ srv.setupTimedTasks(ctx)
|
|
|
+ // 等待终止信号
|
|
|
+ srv.waitForShutdown()
|
|
|
+
|
|
|
+ // 优雅关闭
|
|
|
+ srv.gracefulShutdown()
|
|
|
+ }()
|
|
|
+
|
|
|
// 初始化服务
|
|
|
srv.initHTTPServer()
|
|
|
- srv.initGRPCServer()
|
|
|
-
|
|
|
- // 启动服务
|
|
|
- srv.startServices(ctx)
|
|
|
-
|
|
|
- // 设置定时任务
|
|
|
- srv.setupTimedTasks(ctx)
|
|
|
|
|
|
- // 等待终止信号
|
|
|
- srv.waitForShutdown()
|
|
|
-
|
|
|
- // 优雅关闭
|
|
|
- srv.gracefulShutdown()
|
|
|
}
|
|
|
|
|
|
func (s *server) initHTTPServer() {
|
|
|
- mux := http.NewServeMux()
|
|
|
- // 可以在这里添加HTTP路由
|
|
|
- // mux.HandleFunc("/health", healthHandler)
|
|
|
-
|
|
|
- s.httpServer = &http.Server{
|
|
|
- Addr: ":" + config.DbConf.WebPort,
|
|
|
- Handler: mux,
|
|
|
- }
|
|
|
+ // 1. 初始化xweb配置
|
|
|
+ httpsession.IsRedisSessionStore = true
|
|
|
+ httpsession.RedisNotLoginKey = "userId"
|
|
|
+ xweb.Config.Profiler = true
|
|
|
+ xweb.RootApp().BasePath = "/wxRobot"
|
|
|
+ xweb.RootApp().AppConfig.StaticFileVersion = false
|
|
|
+ xweb.RootApp().AppConfig.CheckXsrf = false
|
|
|
+ xweb.RootApp().AppConfig.EnableHttpCache = false
|
|
|
+ xweb.RootApp().AppConfig.Mode = xweb.Product
|
|
|
+ xweb.RootApp().AppConfig.ReloadTemplates = true
|
|
|
+ xweb.RootApp().AppConfig.SessionTimeout = 7 * 24 * time.Hour
|
|
|
+ xweb.RootApp().Logger.SetOutputLevel(1)
|
|
|
+
|
|
|
+ // 2. 注册控制器
|
|
|
+ xweb.AddAction(&service.WXroBot{})
|
|
|
+ mux1 := http.NewServeMux()
|
|
|
+ xweb.RunBase(":"+config.DbConf.WebPort, mux1)
|
|
|
}
|
|
|
-
|
|
|
func (s *server) initGRPCServer() {
|
|
|
// 创建健康检查服务
|
|
|
s.healthServer = health.NewServer()
|
|
|
-
|
|
|
s.grpcServer = grpc.NewServer(
|
|
|
grpc.KeepaliveParams(keepalive.ServerParameters{
|
|
|
Time: 60 * time.Second,
|
|
@@ -100,14 +108,6 @@ func (s *server) initGRPCServer() {
|
|
|
}
|
|
|
|
|
|
func (s *server) startServices(ctx context.Context) {
|
|
|
- // 启动HTTP服务
|
|
|
- go func() {
|
|
|
- log.Printf("HTTP服务监听 :%s", config.DbConf.WebPort)
|
|
|
- if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
- log.Fatalf("HTTP服务启动失败: %v", err)
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
// 启动gRPC服务
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%s", config.DbConf.GrpcWebPort))
|
|
|
if err != nil {
|
|
@@ -130,7 +130,7 @@ func (s *server) setupTimedTasks(ctx context.Context) {
|
|
|
}{
|
|
|
{1 * time.Minute, "sendTalk"},
|
|
|
{1 * time.Hour, "getContacts"},
|
|
|
- {120 * time.Second, "heartbeat"},
|
|
|
+ {10 * time.Second, "heartbeat"},
|
|
|
}
|
|
|
|
|
|
for _, task := range tasks {
|