statping/source/source.go

213 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
//
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 (
"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
"io/ioutil"
"os"
)
2018-08-10 04:38:54 +00:00
var (
CssBox *rice.Box // CSS files from the 'source/css' directory, this will be loaded into '/assets/css'
ScssBox *rice.Box // SCSS files from the 'source/scss' directory, this will be loaded into '/assets/scss'
JsBox *rice.Box // JS files from the 'source/js' directory, this will be loaded into '/assets/js'
TmplBox *rice.Box // HTML and other small files from the 'source/tmpl' directory, this will be loaded into '/assets'
2018-10-21 16:22:26 +00:00
FontBox *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
)
// Assets will load the Rice boxes containing the CSS, SCSS, JS, and HTML files.
2018-08-10 04:38:54 +00:00
func Assets() {
CssBox = rice.MustFindBox("css")
ScssBox = rice.MustFindBox("scss")
JsBox = rice.MustFindBox("js")
TmplBox = rice.MustFindBox("tmpl")
2018-10-21 16:22:26 +00:00
FontBox = rice.MustFindBox("font")
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)
}
// CompileSASS will attempt to compile the SASS files into CSS
2018-08-11 16:24:32 +00:00
func CompileSASS(folder string) error {
sassBin := os.Getenv("SASS")
if sassBin == "" {
2018-08-16 01:15:14 +00:00
sassBin = "sass"
}
2018-08-11 16:24:32 +00:00
scssFile := fmt.Sprintf("%v/%v", folder, "assets/scss/base.scss")
baseFile := fmt.Sprintf("%v/%v", folder, "assets/css/base.css")
utils.Log(1, fmt.Sprintf("Compiling SASS %v into %v", scssFile, baseFile))
command := fmt.Sprintf("%v %v %v", sassBin, scssFile, baseFile)
stdout, stderr, err := utils.Command(command)
if stdout != "" || stderr != "" {
utils.Log(3, fmt.Sprintf("Failed to compile assets with SASS %v", err))
return errors.New("failed to capture stdout or stderr")
}
2018-08-11 16:24:32 +00:00
if err != nil {
utils.Log(3, fmt.Sprintf("Failed to compile assets with SASS %v", err))
2018-12-20 05:19:11 +00:00
utils.Log(3, fmt.Sprintf("sh -c %v", command))
2018-08-11 16:24:32 +00:00
return err
}
utils.Log(1, fmt.Sprintf("out: %v | error: %v", stdout, stderr))
2018-08-11 16:24:32 +00:00
utils.Log(1, "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 {
2018-08-16 20:55:30 +00:00
if os.Getenv("USE_ASSETS") == "true" {
2018-08-11 16:24:32 +00:00
utils.Log(1, "Environment variable USE_ASSETS was found.")
CreateAllAssets(folder)
err := CompileSASS(folder)
if err != nil {
CopyToPublic(CssBox, folder+"/css", "base.css")
utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
return true
}
return true
}
}
return false
}
// SaveAsset will save an asset to the '/assets/' folder.
2018-08-16 02:22:10 +00:00
func SaveAsset(data []byte, folder, file string) error {
2019-02-06 21:08:01 +00:00
location := folder + "/assets/" + file
utils.Log(1, fmt.Sprintf("Saving %v", location))
err := utils.SaveFile(location, data)
2018-08-11 16:24:32 +00:00
if err != nil {
2019-02-06 21:08:01 +00:00
utils.Log(3, fmt.Sprintf("Failed to save %v, %v", location, 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
2018-08-11 16:24:32 +00:00
func OpenAsset(folder, file string) string {
dat, err := ioutil.ReadFile(folder + "/assets/" + file)
if err != nil {
utils.Log(3, fmt.Sprintf("Failed to open %v, %v", file, err))
return ""
}
return string(dat)
}
// 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 {
2018-12-04 05:57:11 +00:00
utils.Log(1, fmt.Sprintf("Dump Statping assets into %v/assets", folder))
2018-08-11 16:24:32 +00:00
MakePublicFolder(folder + "/assets")
MakePublicFolder(folder + "/assets/js")
MakePublicFolder(folder + "/assets/css")
MakePublicFolder(folder + "/assets/scss")
2018-11-06 07:15:55 +00:00
MakePublicFolder(folder + "/assets/font")
2018-11-16 02:52:51 +00:00
MakePublicFolder(folder + "/assets/files")
2018-08-11 16:24:32 +00:00
utils.Log(1, "Inserting scss, css, and javascript files into assets folder")
2019-01-17 04:59:07 +00:00
CopyAllToPublic(ScssBox, "scss")
CopyAllToPublic(FontBox, "font")
CopyAllToPublic(CssBox, "css")
CopyAllToPublic(JsBox, "js")
CopyToPublic(FontBox, folder+"/assets/font", "all.css")
2018-08-11 16:24:32 +00:00
CopyToPublic(TmplBox, folder+"/assets", "robots.txt")
2018-12-04 19:13:24 +00:00
CopyToPublic(TmplBox, folder+"/assets", "banner.png")
2018-11-06 07:15:55 +00:00
CopyToPublic(TmplBox, folder+"/assets", "favicon.ico")
2018-11-16 02:52:51 +00:00
CopyToPublic(TmplBox, folder+"/assets/files", "swagger.json")
CopyToPublic(TmplBox, folder+"/assets/files", "postman.json")
CopyToPublic(TmplBox, folder+"/assets/files", "grafana.json")
2018-08-11 16:24:32 +00:00
utils.Log(1, "Compiling CSS from SCSS style...")
2018-11-06 07:15:55 +00:00
err := CompileSASS(utils.Directory)
2018-12-04 05:57:11 +00:00
utils.Log(1, "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 {
err := os.RemoveAll(folder + "/assets")
if err != nil {
2018-12-04 05:57:11 +00:00
utils.Log(1, fmt.Sprintf("There was an issue deleting Statping Assets, %v", err))
2018-08-11 16:24:32 +00:00
return err
}
2018-12-04 05:57:11 +00:00
utils.Log(1, "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
2018-11-06 07:15:55 +00:00
func CopyAllToPublic(box *rice.Box, folder string) error {
err := box.Walk("/", func(path string, info os.FileInfo, err error) error {
if info.Name() == "" {
return nil
}
if info.IsDir() {
folder := fmt.Sprintf("%v/assets/%v/%v", utils.Directory, folder, info.Name())
2019-02-06 21:08:01 +00:00
return MakePublicFolder(folder)
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
}
2019-02-06 21:08:01 +00:00
filePath := fmt.Sprintf("%v/%v", folder, path)
return SaveAsset(file, utils.Directory, filePath)
2018-11-06 07:15:55 +00:00
})
return err
}
// CopyToPublic will create a file from a rice Box to the '/assets' directory
2018-08-16 02:22:10 +00:00
func CopyToPublic(box *rice.Box, folder, file string) error {
2018-08-11 16:24:32 +00:00
assetFolder := fmt.Sprintf("%v/%v", folder, file)
utils.Log(1, fmt.Sprintf("Copying %v to %v", file, assetFolder))
base, err := box.String(file)
if err != nil {
utils.Log(3, fmt.Sprintf("Failed to copy %v to %v, %v.", file, assetFolder, err))
2018-08-16 02:22:10 +00:00
return err
2018-08-11 16:24:32 +00:00
}
2018-08-17 04:39:25 +00:00
err = ioutil.WriteFile(assetFolder, []byte(base), 0744)
2018-08-11 16:24:32 +00:00
if err != nil {
utils.Log(3, fmt.Sprintf("Failed to write file %v to %v, %v.", file, assetFolder, 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 {
2018-08-11 16:24:32 +00:00
utils.Log(1, fmt.Sprintf("Creating folder '%v'", folder))
if _, err := os.Stat(folder); os.IsNotExist(err) {
2018-08-17 04:39:25 +00:00
err = os.MkdirAll(folder, 0777)
2018-08-11 16:24:32 +00:00
if err != nil {
utils.Log(3, 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
}