log.go 2.3 KB
Newer Older
Y
Your Name 已提交
1
package config_log
E
eoLinker API Management 已提交
2 3 4 5 6

import (
	"fmt"
	"net/http"

Y
Your Name 已提交
7 8
	goku_handler "github.com/eolinker/goku-api-gateway/goku-handler"

黄孟柱 已提交
9 10 11
	"github.com/eolinker/goku-api-gateway/common/auto-form"
	"github.com/eolinker/goku-api-gateway/console/controller"
	module "github.com/eolinker/goku-api-gateway/console/module/config-log"
E
eoLinker API Management 已提交
12 13
)

Y
Your Name 已提交
14 15
//LogHandlerGet 日志配置获取处理器
type LogHandlerGet struct {
E
eoLinker API Management 已提交
16 17 18
	name string
}

Y
Your Name 已提交
19
func (h *LogHandlerGet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
E
eoLinker API Management 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33

	config, e := module.Get(h.name)
	if e = r.ParseForm(); e != nil {
		controller.WriteError(w, "270000", "data", "[Get]未知错误:"+e.Error(), e)
		return
	}

	controller.WriteResultInfo(w,
		"data",
		"data",
		config)

}

Y
Your Name 已提交
34 35 36 37 38 39
//LogHandlerSet 设置日志处理器
type LogHandlerSet struct {
	name string
}

func (h *LogHandlerSet) ServeHTTP(w http.ResponseWriter, r *http.Request) {
E
eoLinker API Management 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	err := r.ParseForm()
	if err = r.ParseForm(); err != nil {
		controller.WriteError(w, "260000", "data", "[param_check] Parse form body error | 解析form表单参数错误", err)
		return
	}

	param := new(module.PutParam)
	err = auto.SetValues(r.Form, param)
	if err != nil {
		controller.WriteError(w, "260000", "data", fmt.Sprintf("[param_check] %s", err.Error()), err)
		return
	}
	if param.Dir == "" {
		controller.WriteError(w, "260000", "data", "[param_check] inval dir", err)
		return
	}
	if param.File == "" {
		controller.WriteError(w, "260000", "data", "[param_check] inval file name", err)
		return
	}
	if param.Expire < module.ExpireDefault {
		controller.WriteError(w, "260000", "data", "[param_check] inval expire", nil)
		return
	}
	paramBase, err := param.Format()
	if err != nil {
		controller.WriteError(w, "260000", "data", fmt.Sprintf("[param_check] %s", err.Error()), err)
		return
	}
	err = module.Set(h.name, paramBase)
	if err != nil {
Y
Your Name 已提交
72
		controller.WriteError(w, "260000", "data", fmt.Sprintf("[db_error] %s", err.Error()), err)
E
eoLinker API Management 已提交
73 74 75 76 77 78 79
		return
	}
	controller.WriteResultInfo(w,
		"data",
		"",
		nil)
}
Y
Your Name 已提交
80 81 82 83 84 85 86 87

//NewLogHandler 日志handler
func NewLogHandler(name string, factory *goku_handler.AccountHandlerFactory) http.Handler {
	return &LogHandler{
		getHandler: factory.NewAccountHandler(operationLog, false, &LogHandlerGet{name: name}),
		setHandler: factory.NewAccountHandler(operationLog, true, &LogHandlerSet{name: name}),
	}
}