statping/handlers/handlers.go

251 lines
6.3 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package handlers
import (
2018-09-08 22:31:05 +00:00
"encoding/json"
2018-07-04 09:00:16 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/gorilla/sessions"
"github.com/hunterlong/statup/core"
2018-08-10 04:38:54 +00:00
"github.com/hunterlong/statup/source"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"html/template"
"net/http"
"os"
"reflect"
2018-06-30 00:57:05 +00:00
"time"
)
const (
2018-10-06 05:56:08 +00:00
cookieKey = "statup_auth"
2018-06-30 00:57:05 +00:00
)
var (
2018-10-06 05:56:08 +00:00
sessionStore *sessions.CookieStore
httpServer *http.Server
2018-06-30 00:57:05 +00:00
)
2018-09-25 07:03:49 +00:00
// RunHTTPServer will start a HTTP server on a specific IP and port
2018-08-16 02:22:10 +00:00
func RunHTTPServer(ip string, port int) error {
host := fmt.Sprintf("%v:%v", ip, port)
utils.Log(1, "Statup HTTP Server running on http://"+host)
2018-08-16 06:22:20 +00:00
//for _, p := range core.CoreApp.AllPlugins {
// info := p.GetInfo()
// for _, route := range p.Routes() {
// path := fmt.Sprintf("%v", route.URL)
2018-08-16 20:55:30 +00:00
// router.Handle(path, http.HandlerFunc(route.Handler)).Methods(route.Method)
2018-08-16 06:22:20 +00:00
// utils.Log(1, fmt.Sprintf("Added Route %v for plugin %v\n", path, info.Name))
// }
//}
2018-08-16 20:55:30 +00:00
router = Router()
httpServer = &http.Server{
2018-08-16 02:22:10 +00:00
Addr: host,
WriteTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
2018-06-30 00:57:05 +00:00
IdleTimeout: time.Second * 60,
2018-08-16 20:55:30 +00:00
Handler: router,
2018-06-30 00:57:05 +00:00
}
2018-08-16 20:55:30 +00:00
resetCookies()
return httpServer.ListenAndServe()
2018-06-30 00:57:05 +00:00
}
2018-09-25 07:03:49 +00:00
// IsAuthenticated returns true if the HTTP request is authenticated
2018-06-30 00:57:05 +00:00
func IsAuthenticated(r *http.Request) bool {
if os.Getenv("GO_ENV") == "test" {
return true
}
if core.CoreApp == nil {
return false
}
2018-10-06 05:56:08 +00:00
if sessionStore == nil {
2018-06-30 00:57:05 +00:00
return false
}
2018-10-06 05:56:08 +00:00
session, err := sessionStore.Get(r, cookieKey)
2018-06-30 00:57:05 +00:00
if err != nil {
return false
}
if session.Values["authenticated"] == nil {
return false
}
return session.Values["authenticated"].(bool)
}
var handlerFuncs = func(w http.ResponseWriter, r *http.Request) template.FuncMap {
return template.FuncMap{
"js": func(html interface{}) template.JS {
return template.JS(utils.ToString(html))
2018-06-30 00:57:05 +00:00
},
2018-10-03 08:17:25 +00:00
"safe": func(html string) template.HTML {
return template.HTML(html)
2018-06-30 00:57:05 +00:00
},
"Auth": func() bool {
return IsAuthenticated(r)
},
"VERSION": func() string {
2018-06-30 07:31:42 +00:00
return core.VERSION
2018-06-30 00:57:05 +00:00
},
"CoreApp": func() *core.Core {
return core.CoreApp
},
"Services": func() []types.ServiceInterface {
return core.CoreApp.Services
},
"USE_CDN": func() bool {
return core.CoreApp.UseCdn
},
"Type": func(g interface{}) []string {
fooType := reflect.TypeOf(g)
var methods []string
methods = append(methods, fooType.String())
for i := 0; i < fooType.NumMethod(); i++ {
method := fooType.Method(i)
fmt.Println(method.Name)
methods = append(methods, method.Name)
}
return methods
},
"ToJSON": func(g interface{}) template.HTML {
data, _ := json.Marshal(g)
return template.HTML(string(data))
},
2018-06-30 00:57:05 +00:00
"underscore": func(html string) string {
return utils.UnderScoreString(html)
},
"URL": func() string {
return r.URL.String()
},
"CHART_DATA": func() string {
return ""
},
2018-09-15 05:07:17 +00:00
"Error": func() string {
return ""
},
"ToString": func(v interface{}) string {
return utils.ToString(v)
},
2018-10-04 08:18:55 +00:00
"Ago": func(t time.Time) string {
return utils.Timestamp(t).Ago()
},
"Duration": func(t time.Duration) string {
duration, _ := time.ParseDuration(fmt.Sprintf("%vs", t.Seconds()))
2018-10-05 04:28:38 +00:00
return utils.FormatDuration(duration)
2018-10-04 08:18:55 +00:00
},
2018-09-18 22:02:27 +00:00
"ToUnix": func(t time.Time) int64 {
return t.UTC().Unix()
},
"FromUnix": func(t int64) string {
return utils.Timezoner(time.Unix(t, 0), core.CoreApp.Timezone).Format("Monday, January 02")
},
2018-10-02 06:21:14 +00:00
"NewService": func() *types.Service {
return new(types.Service)
},
"NewUser": func() *types.User {
return new(types.User)
},
}
}
2018-10-02 06:21:14 +00:00
var mainTmpl = `{{define "main" }} {{ template "base" . }} {{ end }}`
// executeResponse will render a HTTP response for the front end user
func executeResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}, redirect interface{}) {
utils.Http(r)
if url, ok := redirect.(string); ok {
http.Redirect(w, r, url, http.StatusSeeOther)
return
}
2018-10-02 06:21:14 +00:00
2018-10-03 10:47:32 +00:00
templates := []string{"base.html", "head.html", "nav.html", "footer.html", "scripts.html", "form_service.html", "form_notifier.html", "form_user.html", "form_checkin.html"}
2018-10-02 06:21:14 +00:00
javascripts := []string{"charts.js", "chart_index.js"}
2018-10-02 06:21:14 +00:00
render, err := source.TmplBox.String(file)
if err != nil {
utils.Log(4, err)
}
2018-10-02 06:21:14 +00:00
// setup the main template and handler funcs
t := template.New("main")
t.Funcs(handlerFuncs(w, r))
2018-10-02 06:21:14 +00:00
t, err = t.Parse(mainTmpl)
if err != nil {
utils.Log(4, err)
}
2018-10-02 06:21:14 +00:00
// render all templates
for _, temp := range templates {
tmp, _ := source.TmplBox.String(temp)
t, err = t.Parse(tmp)
if err != nil {
utils.Log(4, err)
}
}
2018-10-02 06:21:14 +00:00
// render all javascript files
for _, temp := range javascripts {
tmp, _ := source.JsBox.String(temp)
t, err = t.Parse(tmp)
if err != nil {
utils.Log(4, err)
}
}
2018-10-02 06:21:14 +00:00
// render the page requested
_, err = t.Parse(render)
if err != nil {
utils.Log(4, err)
}
2018-10-02 06:21:14 +00:00
// execute the template
err = t.Execute(w, data)
if err != nil {
utils.Log(4, err)
}
2018-06-30 00:57:05 +00:00
}
2018-09-25 07:03:49 +00:00
// executeJSResponse will render a Javascript response
func executeJSResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}) {
2018-08-10 04:38:54 +00:00
render, err := source.JsBox.String(file)
2018-07-04 01:19:18 +00:00
if err != nil {
utils.Log(4, err)
}
t := template.New("charts")
t.Funcs(template.FuncMap{
"safe": func(html string) template.HTML {
return template.HTML(html)
},
"Services": func() []types.ServiceInterface {
return core.CoreApp.Services
},
2018-07-04 01:19:18 +00:00
})
_, err = t.Parse(render)
if err != nil {
utils.Log(4, err)
}
err = t.Execute(w, data)
if err != nil {
utils.Log(4, err)
}
2018-07-04 01:19:18 +00:00
}
2018-09-25 07:03:49 +00:00
// error404Handler is a HTTP handler for 404 error pages
func error404Handler(w http.ResponseWriter, r *http.Request) {
2018-08-16 02:22:10 +00:00
w.WriteHeader(http.StatusNotFound)
executeResponse(w, r, "error_404.html", nil, nil)
2018-08-16 02:22:10 +00:00
}