2021-10-23 04:56:24 +00:00
|
|
|
package configui
|
2021-10-19 04:34:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
type CuiResponseWriter struct {
|
2021-10-19 04:34:01 +00:00
|
|
|
http.ResponseWriter
|
|
|
|
StatueCode int
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func (w *CuiResponseWriter) WriteHeader(statusCode int) {
|
2021-10-19 04:34:01 +00:00
|
|
|
if w.StatueCode != 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.StatueCode = statusCode
|
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func (w *CuiResponseWriter) Write(body []byte) (int, error) {
|
2021-10-19 04:34:01 +00:00
|
|
|
if w.StatueCode == 0 {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
return w.ResponseWriter.Write(body)
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func response(w http.ResponseWriter, status int, body []byte) (int, error) {
|
2021-10-19 04:34:01 +00:00
|
|
|
w.WriteHeader(status)
|
|
|
|
return w.Write(body)
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func abort(w http.ResponseWriter, err interface{}) (int, error) {
|
2021-10-19 04:34:01 +00:00
|
|
|
switch v := err.(type) {
|
|
|
|
case int:
|
|
|
|
w.WriteHeader(v)
|
|
|
|
return w.Write([]byte(strconv.Itoa(v)))
|
|
|
|
case string:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return w.Write([]byte(v))
|
|
|
|
case error:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return w.Write([]byte(v.Error()))
|
|
|
|
default:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return w.Write([]byte(strconv.Itoa(500)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func catch(rw *CuiResponseWriter, r *http.Request) {
|
2021-10-19 04:34:01 +00:00
|
|
|
ex := recover()
|
|
|
|
if ex != nil {
|
2021-10-23 04:56:24 +00:00
|
|
|
abort(rw, ex)
|
2021-10-19 04:34:01 +00:00
|
|
|
log.Printf("%s %s %d %s %s\n", GetIP(r), r.Method, rw.StatueCode, r.URL, r.Header.Get("User-Agent"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 04:56:24 +00:00
|
|
|
func (cui *ConfigUI) Parse(w http.ResponseWriter, name string, data interface{}) error {
|
2021-10-19 04:34:01 +00:00
|
|
|
buf := &bytes.Buffer{}
|
2021-10-23 04:56:24 +00:00
|
|
|
err := cui.tmpl.ExecuteTemplate(buf, "home", data)
|
2021-10-19 04:34:01 +00:00
|
|
|
if err != nil {
|
2021-10-20 17:19:13 +00:00
|
|
|
panic(err)
|
2021-10-19 04:34:01 +00:00
|
|
|
}
|
|
|
|
_, err = w.Write(buf.Bytes())
|
|
|
|
return err
|
|
|
|
}
|