2019-02-01 04:48:18 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2020-02-25 07:41:28 +00:00
|
|
|
"encoding/json"
|
2019-02-01 04:48:18 +00:00
|
|
|
"github.com/hunterlong/statping/core"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2020-02-24 05:53:15 +00:00
|
|
|
"net/url"
|
2019-02-01 04:48:18 +00:00
|
|
|
)
|
|
|
|
|
2020-01-13 07:11:53 +00:00
|
|
|
var (
|
|
|
|
basePath = "/"
|
|
|
|
)
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
type CustomResponseWriter struct {
|
|
|
|
body []byte
|
|
|
|
statusCode int
|
|
|
|
header http.Header
|
2020-02-24 16:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func NewCustomResponseWriter() *CustomResponseWriter {
|
|
|
|
return &CustomResponseWriter{
|
|
|
|
header: http.Header{},
|
|
|
|
}
|
2020-02-24 16:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func (w *CustomResponseWriter) Header() http.Header {
|
|
|
|
return w.header
|
2020-02-24 16:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func (w *CustomResponseWriter) Write(b []byte) (int, error) {
|
|
|
|
w.body = b
|
|
|
|
// implement it as per your requirement
|
|
|
|
return 0, nil
|
2020-02-24 16:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func (w *CustomResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
w.statusCode = statusCode
|
2020-02-24 16:26:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-24 05:53:15 +00:00
|
|
|
func parseForm(r *http.Request) url.Values {
|
|
|
|
r.ParseForm()
|
|
|
|
return r.PostForm
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGet(r *http.Request) url.Values {
|
|
|
|
r.ParseForm()
|
|
|
|
return r.Form
|
|
|
|
}
|
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
func decodeRequest(r *http.Request, object interface{}) error {
|
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
defer r.Body.Close()
|
|
|
|
return decoder.Decode(&object)
|
|
|
|
}
|
|
|
|
|
|
|
|
type parsedObject struct {
|
|
|
|
Error Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceFromID(r *http.Request, object interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 04:48:18 +00:00
|
|
|
var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
|
|
|
"VERSION": func() string {
|
|
|
|
return core.VERSION
|
|
|
|
},
|
2020-02-19 08:13:09 +00:00
|
|
|
"CoreApp": func() core.Core {
|
|
|
|
c := *core.CoreApp
|
|
|
|
if c.Name == "" {
|
|
|
|
c.Name = "Statping"
|
|
|
|
}
|
|
|
|
return c
|
2019-02-01 04:48:18 +00:00
|
|
|
},
|
|
|
|
"USE_CDN": func() bool {
|
|
|
|
return core.CoreApp.UseCdn.Bool
|
|
|
|
},
|
2020-02-20 05:28:39 +00:00
|
|
|
"USING_ASSETS": func() bool {
|
|
|
|
return core.CoreApp.UsingAssets()
|
|
|
|
},
|
2020-01-13 07:11:53 +00:00
|
|
|
"BasePath": func() string {
|
|
|
|
return basePath
|
|
|
|
},
|
2019-02-01 04:48:18 +00:00
|
|
|
}
|
|
|
|
}
|