statping/core/export.go

110 lines
2.6 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 core
2018-06-23 05:59:29 +00:00
import (
"bytes"
"fmt"
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/utils"
2018-06-23 05:59:29 +00:00
"html/template"
2018-06-24 06:44:15 +00:00
"io/ioutil"
2018-06-23 05:59:29 +00:00
)
func injectDatabase() {
Configs.Connect(false, utils.Directory)
}
2018-06-23 08:42:50 +00:00
func ExportIndexHTML() string {
source.Assets()
injectDatabase()
CoreApp.SelectAllServices()
CoreApp.UseCdn = true
for _, srv := range CoreApp.Services {
2018-09-12 04:14:22 +00:00
service := srv.(*Service)
service.Check(true)
fmt.Println(service.Name, service.Online, service.Latency)
}
2018-08-10 04:38:54 +00:00
nav, _ := source.TmplBox.String("nav.html")
footer, _ := source.TmplBox.String("footer.html")
render, err := source.TmplBox.String("index.html")
2018-06-23 08:42:50 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(3, err)
2018-06-23 08:42:50 +00:00
}
2018-06-23 08:42:50 +00:00
t := template.New("message")
t.Funcs(template.FuncMap{
2018-06-23 05:59:29 +00:00
"js": func(html string) template.JS {
return template.JS(html)
},
"safe": func(html string) template.HTML {
return template.HTML(html)
},
"VERSION": func() string {
2018-06-30 07:31:42 +00:00
return VERSION
2018-06-23 05:59:29 +00:00
},
"CoreApp": func() *Core {
return CoreApp
},
"USE_CDN": func() bool {
return CoreApp.UseCdn
},
2018-06-23 05:59:29 +00:00
"underscore": func(html string) string {
2018-06-30 00:57:05 +00:00
return utils.UnderScoreString(html)
2018-06-23 05:59:29 +00:00
},
"URL": func() string {
return "/"
},
"CHART_DATA": func() string {
return ExportChartsJs()
},
2018-06-23 08:42:50 +00:00
})
2018-06-23 05:59:29 +00:00
t, _ = t.Parse(nav)
t, _ = t.Parse(footer)
t.Parse(render)
var tpl bytes.Buffer
if err := t.Execute(&tpl, CoreApp); err != nil {
utils.Log(3, err)
}
result := tpl.String()
return result
}
func ExportChartsJs() string {
render, err := source.JsBox.String("charts.js")
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)
},
})
t.Parse(render)
var tpl bytes.Buffer
if err := t.Execute(&tpl, CoreApp.Services); err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(3, err)
2018-06-23 05:59:29 +00:00
}
result := tpl.String()
return result
}
2018-06-24 06:44:15 +00:00
func SaveFile(filename string, data []byte) error {
err := ioutil.WriteFile(filename, data, 0644)
return err
}