Exemplo n.º 1
0
// @api put /admin/api/themes/current 更改当前的主题
// @apiGroup admin
//
// @apiRequest json
// @apiHeader Authorization xxx
// @apiParam value string 新值
//
// @apiSuccess 200 OK
func adminPutCurrentTheme(w http.ResponseWriter, r *http.Request) {
	v := &struct {
		Value string `json:"value"`
	}{}
	if !util.ReadJSON(w, r, v) {
		return
	}

	if len(v.Value) == 0 {
		util.RenderJSON(w, http.StatusBadRequest, &util.ErrorResult{Message: "必须指定一个值!"}, nil)
		return
	}

	if err := app.SetOption("theme", v.Value, false); err != nil {
		logs.Error("adminPutTheme:", err)
		util.RenderJSON(w, http.StatusInternalServerError, nil, nil)
		return
	}

	if err := front.Switch(v.Value); err != nil {
		logs.Error("adminPutTheme:", err)
		util.RenderJSON(w, http.StatusInternalServerError, nil, nil)
		return
	}
	lastUpdated()
	util.RenderJSON(w, http.StatusNoContent, nil, nil)
}
Exemplo n.º 2
0
// @api patch /admin/api/options/{key} 修改设置项的值
// @apiParam key string 需要修改项的key
// @apiGroup admin
//
// @apiRequest json
// @apiHeader Authorization xxx
// @apiParam value string 新值
// @apiExample json
// { "value": "abcdef" }
// @apiSuccess 204 no content
func adminPatchOption(w http.ResponseWriter, r *http.Request) {
	key, ok := util.ParamString(w, r, "key")
	if !ok {
		return
	}

	if _, found := app.GetOption(key); !found {
		util.RenderJSON(w, http.StatusNotFound, nil, nil)
		return
	}

	data := &struct {
		Value string `json:"value"`
	}{}
	if !util.ReadJSON(w, r, data) {
		return
	}

	if err := app.SetOption(key, data.Value, false); err != nil {
		logs.Error("adminPatchOption:", err)
		util.RenderJSON(w, http.StatusInternalServerError, nil, nil)
		return
	}

	lastUpdated()
	util.RenderJSON(w, http.StatusNoContent, nil, nil)
}
Exemplo n.º 3
0
func writeLastLogs(r *http.Request) error {
	ls := make([]*login, 0, maxLoginLogs)
	if err := json.Unmarshal([]byte(opt.Last), &ls); err != nil {
		return err
	}

	l := &login{
		IP:    r.RemoteAddr,
		Agent: r.UserAgent(),
		Time:  time.Now().Format("2006-01-02 15:04:05"),
	}

	if len(ls) >= maxLoginLogs { // 去掉最后一条记录
		ls = ls[:maxLoginLogs-1]
	}
	//lss := make([]*login, 0, maxLoginLogs)
	//lss = append(lss, l)
	lss := []*login{l}
	lss = append(lss, ls...)
	bs, err := json.Marshal(lss)
	if err != nil {
		return err
	}

	if err := app.SetOption("last", string(bs), true); err != nil {
		return err
	}
	logs.Infof("登录信息:IP:%v;Agent:%v;Time:%v\n", l.IP, l.Agent, l.Time)

	return nil
}
Exemplo n.º 4
0
// 更新数据库中的lastUpdated变量
func lastUpdated() {
	if err := app.SetOption("lastUpdated", time.Now().Unix(), true); err != nil {
		logs.Error("admin.lastUpdated:", err)
	}
}