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
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/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 (
|
2018-08-16 01:07:02 +00:00
|
|
|
"errors"
|
2018-08-11 16:24:32 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/GeertJohan/go.rice"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/utils"
|
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"
|
2018-08-11 16:24:32 +00:00
|
|
|
)
|
2018-08-10 04:38:54 +00:00
|
|
|
|
|
|
|
var (
|
2019-12-30 03:34:49 +00:00
|
|
|
log = utils.Log.WithField("type", "source")
|
2018-09-10 22:16:23 +00:00
|
|
|
TmplBox *rice.Box // HTML and other small files from the 'source/tmpl' directory, this will be loaded into '/assets'
|
2018-08-10 04:38:54 +00:00
|
|
|
)
|
|
|
|
|
2018-09-10 22:16:23 +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-01-26 21:01:43 +00:00
|
|
|
TmplBox = rice.MustFindBox("dist")
|
2019-12-30 04:51:28 +00:00
|
|
|
return nil
|
2018-08-10 04:38:54 +00:00
|
|
|
}
|
2018-08-11 16:24:32 +00:00
|
|
|
|
2018-10-06 04:31:51 +00:00
|
|
|
// HelpMarkdown will return the Markdown of help.md into HTML
|
2018-09-15 01:18:21 +00:00
|
|
|
func HelpMarkdown() string {
|
2018-12-20 00:53:14 +00:00
|
|
|
output := blackfriday.Run(CompiledWiki)
|
2018-09-15 01:18:21 +00:00
|
|
|
return string(output)
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// CompileSASS will attempt to compile the SASS files into CSS
|
2020-02-19 04:07:22 +00:00
|
|
|
func CompileSASS() error {
|
2018-08-11 16:24:32 +00:00
|
|
|
sassBin := os.Getenv("SASS")
|
2018-08-16 01:07:02 +00:00
|
|
|
if sassBin == "" {
|
2018-08-16 01:15:14 +00:00
|
|
|
sassBin = "sass"
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
2018-08-11 16:24:32 +00:00
|
|
|
|
2020-02-19 04:07:22 +00:00
|
|
|
scssFile := fmt.Sprintf("%v/assets/%v", utils.Directory, "scss/base.scss")
|
|
|
|
baseFile := fmt.Sprintf("%v/assets/%v", utils.Directory, "css/base.css")
|
2018-08-11 16:24:32 +00:00
|
|
|
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Infoln(fmt.Sprintf("Compiling SASS %v into %v", scssFile, baseFile))
|
2018-08-11 16:24:32 +00:00
|
|
|
command := fmt.Sprintf("%v %v %v", sassBin, scssFile, baseFile)
|
2018-08-16 01:07:02 +00:00
|
|
|
|
2018-11-19 07:13:53 +00:00
|
|
|
stdout, stderr, err := utils.Command(command)
|
2018-08-16 01:07:02 +00:00
|
|
|
|
2018-08-11 16:24:32 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(fmt.Sprintf("Failed to compile assets with SASS %v", err))
|
|
|
|
log.Errorln(fmt.Sprintf("sh -c %v", command))
|
2020-02-06 05:00:12 +00:00
|
|
|
return fmt.Errorf("failed to compile assets with SASS: %v %v \n%v", err, stdout, stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdout != "" || stderr != "" {
|
|
|
|
log.Errorln(fmt.Sprintf("Failed to compile assets with SASS %v %v %v", err, stdout, stderr))
|
|
|
|
return errors.New("failed to capture stdout or stderr")
|
2018-08-11 16:24:32 +00:00
|
|
|
}
|
2018-08-16 01:07:02 +00:00
|
|
|
|
2019-12-28 09:01:07 +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
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +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 {
|
2018-08-16 20:55:30 +00:00
|
|
|
if os.Getenv("USE_ASSETS") == "true" {
|
2019-12-28 09:01:07 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
err := CompileSASS()
|
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")
|
2019-12-28 09:01:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// 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)
|
|
|
|
log.Infoln(fmt.Sprintf("Saving %v", 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
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +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
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
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"))
|
2019-12-28 09:01:07 +00:00
|
|
|
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-02-19 04:07:22 +00:00
|
|
|
return err
|
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")
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Infoln("Compiling CSS from SCSS style...")
|
2020-02-19 04:07:22 +00:00
|
|
|
err := CompileSASS()
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Infoln("Statping assets have been inserted")
|
2018-08-11 16:24:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// 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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Infoln(fmt.Sprintf("There was an issue deleting Statping Assets, %v", err))
|
2018-08-11 16:24:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-28 09:01:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// MakePublicFolder will create a new folder
|
2018-08-16 02:22:10 +00:00
|
|
|
func MakePublicFolder(folder string) error {
|
2019-12-28 09:01:07 +00:00
|
|
|
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 {
|
2019-12-28 09:01:07 +00:00
|
|
|
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
|
|
|
}
|