feat: redirect to install

pull/8/head v1.0.0
1379 2 years ago committed by 1379Monitor
parent 58cc28a976
commit 68e30b7c39

@ -2,6 +2,7 @@ package listener
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
@ -85,7 +86,9 @@ func (s *StartListener) printStartInfo(ctx context.Context) error {
}
site := logger.BlueBold + "Sonic started at " + blogURL + logger.Reset
log.Info(site)
fmt.Println(site)
adminSite := logger.BlueBold + "Sonic admin started at " + blogURL + "/admin" + logger.Reset
log.Info(adminSite)
fmt.Println(adminSite)
return nil
}

@ -0,0 +1,45 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-sonic/sonic/model/property"
"github.com/go-sonic/sonic/service"
)
type InstallRedirectMiddleware struct {
optionService service.OptionService
}
func NewInstallRedirectMiddleware(optionService service.OptionService) *InstallRedirectMiddleware {
return &InstallRedirectMiddleware{
optionService: optionService,
}
}
func (i *InstallRedirectMiddleware) InstallRedirect() gin.HandlerFunc {
skipPath := map[string]struct{}{
"/api/admin/installations": {},
"/api/admin/is_installed": {},
"/api/admin/login/precheck": {},
}
return func(ctx *gin.Context) {
path := ctx.Request.URL.Path
if _, ok := skipPath[path]; ok {
return
}
isInstall, err := i.optionService.GetOrByDefaultWithErr(ctx, property.IsInstalled, false)
if err != nil {
abortWithStatusJSON(ctx, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
return
}
if !isInstall.(bool) {
ctx.Redirect(http.StatusFound, "/admin/#install")
ctx.Abort()
return
}
ctx.Next()
}
}

@ -45,7 +45,7 @@ func (s *Server) RegisterRouters() {
}
{
adminApiRouter := router.Group("/api/admin")
adminApiRouter.Use(s.LogMiddleware.LoggerWithConfig(middleware.GinLoggerConfig{}), s.RecoveryMiddleware.RecoveryWithLogger())
adminApiRouter.Use(s.LogMiddleware.LoggerWithConfig(middleware.GinLoggerConfig{}), s.RecoveryMiddleware.RecoveryWithLogger(), s.InstallRedirectMiddleware.InstallRedirect())
adminApiRouter.GET("/is_installed", s.wrapHandler(s.AdminHandler.IsInstalled))
adminApiRouter.POST("/login/precheck", s.wrapHandler(s.AdminHandler.AuthPreCheck))
adminApiRouter.POST("/login", s.wrapHandler(s.AdminHandler.Auth))
@ -278,7 +278,7 @@ func (s *Server) RegisterRouters() {
}
{
contentRouter := router.Group("")
contentRouter.Use(s.LogMiddleware.LoggerWithConfig(middleware.GinLoggerConfig{}), s.RecoveryMiddleware.RecoveryWithLogger())
contentRouter.Use(s.LogMiddleware.LoggerWithConfig(middleware.GinLoggerConfig{}), s.RecoveryMiddleware.RecoveryWithLogger(), s.InstallRedirectMiddleware.InstallRedirect())
contentRouter.POST("/content/:type/:slug/authentication", s.wrapHTMLHandler(s.ViewHandler.Authenticate))

@ -32,6 +32,7 @@ type Server struct {
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
@ -85,6 +86,7 @@ type ServerParams struct {
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
@ -148,6 +150,7 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
AuthMiddleware: param.AuthMiddleware,
LogMiddleware: param.LogMiddleware,
RecoveryMiddleware: param.RecoveryMiddleware,
InstallRedirectMiddleware: param.InstallRedirectMiddleware,
AdminHandler: param.AdminHandler,
AttachmentHandler: param.AttachmentHandler,
BackupHandler: param.BackupHandler,

@ -47,6 +47,7 @@ func InitApp() *fx.App {
middleware.NewAuthMiddleware,
middleware.NewGinLoggerMiddleware,
middleware.NewRecoveryMiddleware,
middleware.NewInstallRedirectMiddleware,
),
fx.Populate(&dal.DB),
fx.Populate(&eventBus),

Loading…
Cancel
Save