Allow customization of admin path

pull/755/head
johnniang 2019-11-29 23:32:54 +08:00
parent f7057db030
commit 8463a3f31e
3 changed files with 33 additions and 7 deletions

View File

@ -3,7 +3,6 @@ package run.halo.app.controller.content;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import run.halo.app.config.properties.HaloProperties; import run.halo.app.config.properties.HaloProperties;
import run.halo.app.exception.ServiceException; import run.halo.app.exception.ServiceException;
@ -13,6 +12,7 @@ import run.halo.app.model.support.HaloConst;
import run.halo.app.service.OptionService; import run.halo.app.service.OptionService;
import run.halo.app.service.UserService; import run.halo.app.service.UserService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
@ -25,6 +25,16 @@ import java.io.IOException;
@Controller @Controller
public class MainController { public class MainController {
/**
* Index redirect uri.
*/
private final static String INDEX_REDIRECT_URI = "index.html";
/**
* Install redirect uri.
*/
private final static String INSTALL_REDIRECT_URI = INDEX_REDIRECT_URI + "#install";
private final UserService userService; private final UserService userService;
private final OptionService optionService; private final OptionService optionService;
@ -37,9 +47,10 @@ public class MainController {
this.haloProperties = haloProperties; this.haloProperties = haloProperties;
} }
@GetMapping("{permlink}") @GetMapping("${halo.admin-path:admin}")
public String admin(@PathVariable(name = "permlink") String permlink) { public void admin(HttpServletRequest request, HttpServletResponse response) throws IOException {
return "redirect:/" + permlink + "/index.html"; String adminIndexRedirectUri = StringUtils.appendIfMissing(this.haloProperties.getAdminPath(), "/") + INDEX_REDIRECT_URI;
response.sendRedirect(adminIndexRedirectUri);
} }
@GetMapping("version") @GetMapping("version")
@ -49,8 +60,9 @@ public class MainController {
} }
@GetMapping("install") @GetMapping("install")
public String installation() { public void installation(HttpServletResponse response) throws IOException {
return "redirect:" + haloProperties.getAdminPath() + "/index.html#install"; String installRedirectUri = StringUtils.appendIfMissing(this.haloProperties.getAdminPath(), "/") + INSTALL_REDIRECT_URI;
response.sendRedirect(installRedirectUri);
} }
@GetMapping("avatar") @GetMapping("avatar")
@ -76,4 +88,5 @@ public class MainController {
response.sendRedirect(favicon); response.sendRedirect(favicon);
} }
} }
} }

View File

@ -1,5 +1,6 @@
package run.halo.app.controller.core; package run.halo.app.controller.core;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties; import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
@ -68,7 +69,11 @@ public class CommonController extends AbstractErrorController {
public String handleError(HttpServletRequest request, HttpServletResponse response, Model model) { public String handleError(HttpServletRequest request, HttpServletResponse response, Model model) {
HttpStatus status = getStatus(request); HttpStatus status = getStatus(request);
log.error("Error path: [{}], status: [{}]", getErrorPath(), status); log.error("Request URL: [{}], URI: [{}], Request Method: [{}], IP: [{}]",
request.getRequestURL(),
request.getRequestURI(),
request.getMethod(),
ServletUtil.getClientIP(request));
handleCustomException(request); handleCustomException(request);

View File

@ -16,6 +16,11 @@ import java.util.List;
@Component @Component
public class RandomMethod implements TemplateMethodModelEx { public class RandomMethod implements TemplateMethodModelEx {
/**
* Constructor.
*
* @param configuration injected by spring.
*/
public RandomMethod(Configuration configuration) { public RandomMethod(Configuration configuration) {
configuration.setSharedVariable("randomMethod", this); configuration.setSharedVariable("randomMethod", this);
} }
@ -29,6 +34,9 @@ public class RandomMethod implements TemplateMethodModelEx {
*/ */
@Override @Override
public Object exec(List arguments) throws TemplateModelException { public Object exec(List arguments) throws TemplateModelException {
if (arguments.size() != 2) {
throw new TemplateModelException("Wrong arguments! 2 arguments are needed");
}
SimpleNumber argOne = (SimpleNumber) arguments.get(0); SimpleNumber argOne = (SimpleNumber) arguments.get(0);
SimpleNumber argTwo = (SimpleNumber) arguments.get(1); SimpleNumber argTwo = (SimpleNumber) arguments.get(1);
int start = argOne.getAsNumber().intValue(); int start = argOne.getAsNumber().intValue();