You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.8 KiB

2 years ago
package service
import (
"fmt"
"net/http"
1 year ago
"strings"
"time"
2 years ago
"moredoc/conf"
"moredoc/middleware/auth"
2 years ago
"moredoc/middleware/jsonpb"
"moredoc/model"
2 years ago
"moredoc/service/serve"
2 years ago
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"go.uber.org/zap"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"google.golang.org/grpc"
_ "google.golang.org/grpc/encoding/gzip" // grpc gzip
)
// Run start server
func Run(cfg *conf.Config, logger *zap.Logger) {
2 years ago
size := 100 * 1024 * 1024 // 100MB
dialOpts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(size)),
grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")),
}
auth := auth.NewAuth(&cfg.JWT)
2 years ago
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
auth.AuthUnaryServerInterceptor(),
2 years ago
grpc_recovery.UnaryServerInterceptor(),
)),
)
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(
runtime.MIMEWildcard,
&jsonpb.JSONPb{OrigName: true, EmitDefaults: true, EnumsAsInts: true},
),
)
dbModel, err := model.NewDBModel(&cfg.Database, logger)
2 years ago
if err != nil {
logger.Fatal("NewDBModel", zap.Error(err))
return
}
7 months ago
// 每次启动时都对dist中的title进行一次处理以替换掉关键字 moredoc
go dbModel.InitSEO()
7 months ago
dbModel.RunTasks()
7 months ago
2 years ago
if cfg.Level != "debug" {
gin.SetMode(gin.ReleaseMode)
}
app := gin.New()
app.Use(
gzip.Gzip(gzip.BestCompression, gzip.WithExcludedExtensions([]string{".svg", ".png", ".gif", ".jpeg", ".jpg", ".ico"})), // gzip
gin.Recovery(), // recovery
// cors.Default(), // allows all origins
cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"},
AllowHeaders: []string{"*"},
AllowOrigins: []string{"*"}, // Referrer Policy: strict-origin-when-cross-origin
AllowCredentials: false,
MaxAge: 12 * time.Hour,
}),
2 years ago
)
2 years ago
endpoint := fmt.Sprintf("localhost:%v", cfg.Port)
err = serve.RegisterGRPCService(dbModel, logger, endpoint, auth, grpcServer, gwmux, dialOpts...)
if err != nil {
logger.Fatal("registerAPIService", zap.Error(err))
return
}
2 years ago
serve.RegisterGinRouter(app, dbModel, logger, auth)
2 years ago
// 根目录访问静态文件,要放在 grpc 服务的前面
// 可以在 dist 目录下创建一个 index.html 文件并添加内容,然后访问 http://ip:port
app.Use(static.Serve("/uploads", static.LocalFile("./uploads", true)))
app.Use(static.Serve("/sitemap", static.LocalFile("./sitemap", true)))
2 years ago
app.Use(static.Serve("/", static.LocalFile("./dist", true)))
app.NoRoute(wrapH(grpcHandlerFunc(grpcServer, gwmux))) // grpcServer and grpcGatewayServer
2 years ago
addr := fmt.Sprintf(":%v", cfg.Port)
logger.Info("server start", zap.Int("port", cfg.Port))
err = app.Run(addr)
if err != nil {
logger.Fatal(err.Error())
}
}
// See: https://github.com/philips/grpc-gateway-example/issues/22
func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
1 year ago
if r.ProtoMajor == 2 { // grpc 请求
2 years ago
grpcServer.ServeHTTP(w, r)
1 year ago
} else if !strings.HasPrefix(r.URL.Path, "/api/") {
http.ServeFile(w, r, "./dist/index.html")
2 years ago
} else {
1 year ago
// 如 /api/v1/xxxx /api/v2/xxxx
2 years ago
otherHandler.ServeHTTP(w, r)
}
}), &http2.Server{})
}
// wrapH overwrite gin.WrapH
func wrapH(h http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
c.Status(http.StatusOK) // reset 404
h.ServeHTTP(c.Writer, c.Request)
}
}