statping/source/source.go

236 lines
7.0 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2020-03-09 18:17:55 +00:00
// https://github.com/statping/statping
2018-08-16 06:22:20 +00:00
//
// 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-08-10 04:38:54 +00:00
package source
2018-12-20 00:53:14 +00:00
//go:generate go run generate_wiki.go
2018-08-11 16:24:32 +00:00
import (
"fmt"
"github.com/GeertJohan/go.rice"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2020-03-02 07:53:46 +00:00
"github.com/pkg/errors"
2019-11-22 18:48:34 +00:00
"github.com/russross/blackfriday/v2"
2018-08-11 16:24:32 +00:00
"os"
2020-02-06 05:00:12 +00:00
"path/filepath"
2020-03-02 07:53:46 +00:00
"strings"
2018-08-11 16:24:32 +00:00
)
2018-08-10 04:38:54 +00:00
var (
2020-03-02 07:53:46 +00:00
log = utils.Log.WithField("type", "source")
TmplBox *rice.Box // HTML and other small files from the 'source/tmpl' directory, this will be loaded into '/assets'
DefaultScss = []string{"scss/base.scss", "scss/mobile.scss"}
2018-08-10 04:38:54 +00:00
)
// Assets will load the Rice boxes containing the CSS, SCSS, JS, and HTML files.
2019-12-30 04:51:28 +00:00
func Assets() error {
2020-03-05 21:19:28 +00:00
var err error
TmplBox, err = rice.FindBox("dist")
return err
2018-08-10 04:38:54 +00:00
}
2018-08-11 16:24:32 +00:00
// HelpMarkdown will return the Markdown of help.md into HTML
func HelpMarkdown() string {
2018-12-20 00:53:14 +00:00
output := blackfriday.Run(CompiledWiki)
return string(output)
}
2020-03-02 07:53:46 +00:00
func scssRendered(name string) string {
spl := strings.Split(name, "/")
path := spl[:len(spl)-2]
file := spl[len(spl)-1]
splFile := strings.Split(file, ".")
return fmt.Sprintf("%s/css/%s.css", strings.Join(path, "/"), splFile[len(splFile)-2])
}
// CompileSASS will attempt to compile the SASS files into CSS
2020-03-02 07:53:46 +00:00
func CompileSASS(files ...string) error {
sassBin := utils.Getenv("SASS", "sass").(string)
2018-08-11 16:24:32 +00:00
2020-03-02 07:53:46 +00:00
for _, file := range files {
scssFile := fmt.Sprintf("%v/assets/%v", utils.Directory, file)
2018-08-11 16:24:32 +00:00
2020-03-02 07:53:46 +00:00
log.Infoln(fmt.Sprintf("Compiling SASS %v into %v", scssFile, scssRendered(scssFile)))
2020-03-02 07:53:46 +00:00
stdout, stderr, err := utils.Command(sassBin, scssFile, scssRendered(scssFile))
2020-03-02 07:53:46 +00:00
if err != nil {
log.Errorln(fmt.Sprintf("Failed to compile assets with SASS %v", err))
log.Errorln(fmt.Sprintf("%s %s %s", sassBin, scssFile, scssRendered(scssFile)))
return errors.Wrapf(err, "failed to compile assets, %s %s %s", err, stdout, stderr)
}
2020-02-06 05:00:12 +00:00
2020-03-02 07:53:46 +00:00
if stdout != "" || stderr != "" {
log.Errorln(fmt.Sprintf("Failed to compile assets with SASS %v %v %v", err, stdout, stderr))
return errors.Wrap(err, "failed to capture stdout or stderr")
}
2020-03-02 07:53:46 +00:00
log.Infoln(fmt.Sprintf("out: %v | error: %v", stdout, stderr))
}
log.Infoln("SASS Compiling is complete!")
2018-11-07 17:19:56 +00:00
return nil
2018-08-11 16:24:32 +00:00
}
// UsingAssets returns true if the '/assets' folder is found in the directory
2018-08-16 20:55:30 +00:00
func UsingAssets(folder string) bool {
2018-08-11 16:24:32 +00:00
if _, err := os.Stat(folder + "/assets"); err == nil {
return true
} else {
2020-02-26 05:38:03 +00:00
useAssets := utils.Getenv("USE_ASSETS", false).(bool)
if useAssets {
log.Infoln("Environment variable USE_ASSETS was found.")
2020-02-19 04:07:22 +00:00
if err := CreateAllAssets(folder); err != nil {
log.Warnln(err)
}
2020-03-02 07:53:46 +00:00
err := CompileSASS(DefaultScss...)
2018-08-11 16:24:32 +00:00
if err != nil {
2020-01-26 21:01:43 +00:00
//CopyToPublic(CssBox, folder+"/css", "base.css")
log.Warnln("Default 'base.css' was insert because SASS did not work.")
2018-08-11 16:24:32 +00:00
return true
}
return true
}
}
return false
}
// SaveAsset will save an asset to the '/assets/' folder.
2020-02-19 04:07:22 +00:00
func SaveAsset(data []byte, path string) error {
path = fmt.Sprintf("%s/assets/%s", utils.Directory, path)
err := utils.SaveFile(path, data)
2018-08-11 16:24:32 +00:00
if err != nil {
2020-02-19 04:07:22 +00:00
log.Errorln(fmt.Sprintf("Failed to save %v, %v", path, err))
2018-08-16 02:22:10 +00:00
return err
2018-08-11 16:24:32 +00:00
}
2018-08-16 02:22:10 +00:00
return nil
2018-08-11 16:24:32 +00:00
}
// OpenAsset returns a file's contents as a string
2020-02-19 04:07:22 +00:00
func OpenAsset(path string) string {
path = fmt.Sprintf("%s/assets/%s", utils.Directory, path)
data, err := utils.OpenFile(path)
2018-08-11 16:24:32 +00:00
if err != nil {
2020-02-19 04:07:22 +00:00
log.Errorln(fmt.Sprintf("Failed to open %v, %v", path, err))
2018-08-11 16:24:32 +00:00
return ""
}
2020-02-19 04:07:22 +00:00
return data
2018-08-11 16:24:32 +00:00
}
// CreateAllAssets will dump HTML, CSS, SCSS, and JS assets into the '/assets' directory
2018-08-11 16:24:32 +00:00
func CreateAllAssets(folder string) error {
log.Infoln(fmt.Sprintf("Dump Statping assets into %v/assets", folder))
2020-02-06 05:00:12 +00:00
fp := filepath.Join
MakePublicFolder(fp(folder, "/assets"))
MakePublicFolder(fp(folder, "assets", "js"))
MakePublicFolder(fp(folder, "assets", "css"))
MakePublicFolder(fp(folder, "assets", "scss"))
MakePublicFolder(fp(folder, "assets", "font"))
MakePublicFolder(fp(folder, "assets", "files"))
log.Infoln("Inserting scss, css, and javascript files into assets folder")
2020-02-06 05:00:12 +00:00
2020-02-19 04:07:22 +00:00
if err := CopyAllToPublic(TmplBox); err != nil {
2020-02-06 05:00:12 +00:00
log.Errorln(err)
2020-03-02 07:53:46 +00:00
return errors.Wrap(err, "copying all to public")
2020-02-06 05:00:12 +00:00
}
2020-02-19 04:07:22 +00:00
CopyToPublic(TmplBox, "", "robots.txt")
CopyToPublic(TmplBox, "", "banner.png")
CopyToPublic(TmplBox, "", "favicon.ico")
CopyToPublic(TmplBox, "files", "swagger.json")
CopyToPublic(TmplBox, "files", "postman.json")
CopyToPublic(TmplBox, "files", "grafana.json")
log.Infoln("Compiling CSS from SCSS style...")
2020-03-02 07:53:46 +00:00
err := CompileSASS(DefaultScss...)
log.Infoln("Statping assets have been inserted")
2018-08-11 16:24:32 +00:00
return err
}
// DeleteAllAssets will delete the '/assets' folder
2018-08-11 16:24:32 +00:00
func DeleteAllAssets(folder string) error {
2019-12-30 03:34:49 +00:00
err := utils.DeleteDirectory(folder + "/assets")
2018-08-11 16:24:32 +00:00
if err != nil {
log.Infoln(fmt.Sprintf("There was an issue deleting Statping Assets, %v", err))
2018-08-11 16:24:32 +00:00
return err
}
log.Infoln("Statping assets have been deleted")
2018-08-11 16:24:32 +00:00
return err
}
2018-11-06 09:03:21 +00:00
// CopyAllToPublic will copy all the files in a rice box into a local folder
2020-02-19 04:07:22 +00:00
func CopyAllToPublic(box *rice.Box) error {
2020-02-06 05:00:12 +00:00
exclude := map[string]bool{
"base.gohtml": true,
"index.html": true,
"swagger.json": true,
"postman.json": true,
"grafana.json": true,
}
2018-11-06 07:15:55 +00:00
err := box.Walk("/", func(path string, info os.FileInfo, err error) error {
if info.Name() == "" {
return nil
}
2020-02-06 05:00:12 +00:00
if exclude[info.Name()] {
return nil
}
2018-11-06 07:15:55 +00:00
if info.IsDir() {
2020-02-06 05:00:12 +00:00
return nil
2018-11-06 07:15:55 +00:00
}
2020-02-06 05:00:12 +00:00
utils.Log.Infoln(path)
2018-11-06 07:15:55 +00:00
file, err := box.Bytes(path)
if err != nil {
2019-02-06 21:08:01 +00:00
return err
2018-11-06 07:15:55 +00:00
}
2020-02-19 04:07:22 +00:00
return SaveAsset(file, path)
2018-11-06 07:15:55 +00:00
})
return err
}
// CopyToPublic will create a file from a rice Box to the '/assets' directory
2020-02-19 04:07:22 +00:00
func CopyToPublic(box *rice.Box, path, file string) error {
assetPath := fmt.Sprintf("%v/assets/%v/%v", utils.Directory, path, file)
if path == "" {
assetPath = fmt.Sprintf("%v/assets/%v", utils.Directory, file)
}
log.Infoln(fmt.Sprintf("Copying %v to %v", file, assetPath))
2018-08-11 16:24:32 +00:00
base, err := box.String(file)
if err != nil {
2020-02-19 04:07:22 +00:00
log.Errorln(fmt.Sprintf("Failed to copy %v to %v, %v.", file, assetPath, err))
2018-08-16 02:22:10 +00:00
return err
2018-08-11 16:24:32 +00:00
}
2020-02-19 04:07:22 +00:00
err = utils.SaveFile(assetPath, []byte(base))
2018-08-11 16:24:32 +00:00
if err != nil {
2020-02-19 04:07:22 +00:00
log.Errorln(fmt.Sprintf("Failed to write file %v to %v, %v.", file, assetPath, err))
2018-08-16 02:22:10 +00:00
return err
2018-08-11 16:24:32 +00:00
}
2018-08-16 02:22:10 +00:00
return nil
2018-08-11 16:24:32 +00:00
}
// MakePublicFolder will create a new folder
2018-08-16 02:22:10 +00:00
func MakePublicFolder(folder string) error {
log.Infoln(fmt.Sprintf("Creating folder '%v'", folder))
2019-12-30 03:34:49 +00:00
if !utils.FolderExists(folder) {
err := utils.CreateDirectory(folder)
2018-08-11 16:24:32 +00:00
if err != nil {
log.Errorln(fmt.Sprintf("Failed to created %v directory, %v", folder, err))
2018-08-16 02:22:10 +00:00
return err
2018-08-11 16:24:32 +00:00
}
}
2018-08-16 02:22:10 +00:00
return nil
2018-08-11 16:24:32 +00:00
}