statping/handlers/function.go

90 lines
1.6 KiB
Go
Raw Normal View History

package handlers
import (
2020-02-25 07:41:28 +00:00
"encoding/json"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/types/core"
"github.com/hunterlong/statping/utils"
"html/template"
"net/http"
2020-02-24 05:53:15 +00:00
"net/url"
)
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
}
var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap {
return template.FuncMap{
"VERSION": func() string {
2020-03-04 10:29:00 +00:00
return core.App.Version
},
2020-02-19 08:13:09 +00:00
"CoreApp": func() core.Core {
2020-03-04 10:29:00 +00:00
c := *core.App
2020-02-19 08:13:09 +00:00
if c.Name == "" {
c.Name = "Statping"
}
return c
},
"USE_CDN": func() bool {
2020-03-04 10:29:00 +00:00
return core.App.UseCdn.Bool
},
2020-02-20 05:28:39 +00:00
"USING_ASSETS": func() bool {
2020-03-04 10:29:00 +00:00
return source.UsingAssets(utils.Directory)
2020-02-20 05:28:39 +00:00
},
2020-01-13 07:11:53 +00:00
"BasePath": func() string {
return basePath
},
}
}