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

import (
	"fmt"
	"net/http"

黄孟柱 已提交
7 8 9
	"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 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
)

type LogHandler struct {
	name string
}

func (h *LogHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	_, err := controller.CheckLogin(w, r, controller.OperationGatewayConfig, controller.OperationEDIT)
	if err != nil {
		return
	}

	switch r.Method {
	case http.MethodGet:
		{
			h.get(w, r)

		}

	case http.MethodPut:
		{
			h.set(w, r)

		}
	default:
		w.WriteHeader(404)
	}
}
func (h *LogHandler) get(w http.ResponseWriter, r *http.Request) {
	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)

}

func (h *LogHandler) set(w http.ResponseWriter, r *http.Request) {

	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 {
		controller.WriteError(w, "260000", "data", fmt.Sprintf("[mysql_error] %s", err.Error()), err)
		return
	}
	controller.WriteResultInfo(w,
		"data",
		"",
		nil)
}