package configui import ( "bytes" "log" "net/http" "strconv" ) type CuiResponseWriter struct { http.ResponseWriter StatueCode int } func (w *CuiResponseWriter) WriteHeader(statusCode int) { if w.StatueCode != 0 { return } w.StatueCode = statusCode w.ResponseWriter.WriteHeader(statusCode) } func (w *CuiResponseWriter) Write(body []byte) (int, error) { if w.StatueCode == 0 { w.WriteHeader(200) } return w.ResponseWriter.Write(body) } func response(w http.ResponseWriter, status int, body []byte) (int, error) { w.WriteHeader(status) return w.Write(body) } func abort(w http.ResponseWriter, err interface{}) (int, error) { log.Println(err) 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))) } } func catch(rw *CuiResponseWriter, r *http.Request) { ex := recover() if ex != nil { abort(rw, ex) log.Printf("%s %s %d %s %s\n", GetIP(r), r.Method, rw.StatueCode, r.URL, r.Header.Get("User-Agent")) } } func (cui *ConfigUI) Parse(w http.ResponseWriter, name string, data interface{}) error { buf := &bytes.Buffer{} err := cui.tmpl.ExecuteTemplate(buf, "home", data) if err != nil { panic(err) } _, err = w.Write(buf.Bytes()) return err }