186 lines
5.8 KiB
Go
186 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/hunterlong/statup/core"
|
|
"github.com/hunterlong/statup/plugin"
|
|
"github.com/hunterlong/statup/utils"
|
|
"github.com/joho/godotenv"
|
|
"strings"
|
|
"time"
|
|
"upper.io/db.v3/sqlite"
|
|
)
|
|
|
|
func CatchCLI(args []string) {
|
|
switch args[1] {
|
|
case "version":
|
|
fmt.Printf("Statup v%v\n", VERSION)
|
|
case "assets":
|
|
RenderBoxes()
|
|
core.CreateAllAssets()
|
|
case "sass":
|
|
core.CompileSASS()
|
|
case "api":
|
|
HelpEcho()
|
|
case "test":
|
|
cmd := args[2]
|
|
switch cmd {
|
|
case "plugins":
|
|
LoadPlugins(true)
|
|
}
|
|
case "export":
|
|
var err error
|
|
fmt.Printf("Statup v%v Exporting Static 'index.html' page...\n", VERSION)
|
|
RenderBoxes()
|
|
core.Configs, err = core.LoadConfig()
|
|
if err != nil {
|
|
utils.Log(4, "config.yml file not found")
|
|
}
|
|
RunOnce()
|
|
indexSource := core.ExportIndexHTML()
|
|
err = core.SaveFile("./index.html", []byte(indexSource))
|
|
if err != nil {
|
|
utils.Log(4, err)
|
|
}
|
|
utils.Log(1, "Exported Statup index page: 'index.html'")
|
|
case "help":
|
|
HelpEcho()
|
|
case "update":
|
|
fmt.Println("Sorry updating isn't available yet!")
|
|
case "run":
|
|
utils.Log(1, "Running 1 time and saving to database...")
|
|
RunOnce()
|
|
fmt.Println("Check is complete.")
|
|
case "env":
|
|
fmt.Println("Statup Environment Variables")
|
|
envs, err := godotenv.Read(".env")
|
|
if err != nil {
|
|
utils.Log(4, "No .env file found in current directory.")
|
|
}
|
|
for k, e := range envs {
|
|
fmt.Printf("%v=%v\n", k, e)
|
|
}
|
|
default:
|
|
utils.Log(3, "Statup does not have the command you entered.")
|
|
}
|
|
}
|
|
|
|
func RunOnce() {
|
|
var err error
|
|
core.Configs, err = core.LoadConfig()
|
|
if err != nil {
|
|
utils.Log(4, "config.yml file not found")
|
|
}
|
|
err = core.DbConnection(core.Configs.Connection)
|
|
if err != nil {
|
|
utils.Log(4, err)
|
|
}
|
|
core.CoreApp, err = core.SelectCore()
|
|
if err != nil {
|
|
fmt.Println("Core database was not found, Statup is not setup yet.")
|
|
}
|
|
core.CoreApp.Services, err = core.SelectAllServices()
|
|
if err != nil {
|
|
utils.Log(4, err)
|
|
}
|
|
for _, s := range core.CoreApp.Services {
|
|
out := s.Check()
|
|
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online)
|
|
}
|
|
}
|
|
|
|
func HelpEcho() {
|
|
fmt.Printf("Statup v%v - Statup.io\n", VERSION)
|
|
fmt.Printf("A simple Application Status Monitor that is opensource and lightweight.\n")
|
|
fmt.Printf("Commands:\n")
|
|
fmt.Println(" statup - Main command to run Statup server")
|
|
fmt.Println(" statup version - Returns the current version of Statup")
|
|
fmt.Println(" statup run - Check all service 1 time and then quit")
|
|
fmt.Println(" statup test plugins - Test all plugins for required information")
|
|
fmt.Println(" statup assets - Export all assets used locally to be edited.")
|
|
fmt.Println(" statup env - Show all environment variables being used for Statup")
|
|
fmt.Println(" statup export - Exports the index page as a static HTML for pushing")
|
|
fmt.Println(" to Github Pages or your own FTP server. Export will")
|
|
fmt.Println(" create 'index.html' in the current directory.")
|
|
fmt.Println(" statup update - Attempts to update to the latest version")
|
|
fmt.Println(" statup help - Shows the user basic information about Statup")
|
|
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
|
|
}
|
|
|
|
func TestPlugin(plug plugin.PluginActions) {
|
|
RenderBoxes()
|
|
defer utils.DeleteFile("./.plugin_test.db")
|
|
core.CoreApp.AllPlugins = []plugin.PluginActions{plug}
|
|
info := plug.GetInfo()
|
|
utils.Log(1, "=======================================================================")
|
|
utils.Log(1, fmt.Sprintf(" Plugin Name: %v", info.Name))
|
|
utils.Log(1, fmt.Sprintf(" Plugin Description: %v", info.Description))
|
|
utils.Log(1, fmt.Sprintf(" Plugin Routes: %v", len(plug.Routes())))
|
|
for k, r := range plug.Routes() {
|
|
utils.Log(1, fmt.Sprintf(" - Route %v - (%v) /%v", k+1, r.Method, r.URL))
|
|
}
|
|
|
|
fakeSrv := &core.Service{
|
|
Id: 56,
|
|
Name: "Test Plugin Service",
|
|
Domain: "https://google.com",
|
|
}
|
|
|
|
fakeFailD := core.FailureData{
|
|
Issue: "No issue, just testing this plugin.",
|
|
}
|
|
|
|
fakeCore := &core.Core{
|
|
Name: "Plugin Test",
|
|
Description: "This is a fake Core for testing your plugin",
|
|
ApiSecret: "0x0x0x0x0",
|
|
ApiKey: "abcdefg12345",
|
|
Services: []*core.Service{fakeSrv},
|
|
}
|
|
|
|
fakeUser := &core.User{
|
|
Id: 6334,
|
|
Username: "Bulbasaur",
|
|
Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
|
|
Email: "info@testdomain.com",
|
|
Admin: true,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
utils.Log(1, fmt.Sprintf("Creating a SQLite database for testing, will be deleted automatically..."))
|
|
sqlFake := sqlite.ConnectionURL{
|
|
Database: "./.plugin_test.db",
|
|
}
|
|
fakeDb, err := sqlite.Open(sqlFake)
|
|
if err != nil {
|
|
utils.Log(3, err)
|
|
}
|
|
up, _ := core.SqlBox.String("sqlite_up.sql")
|
|
requests := strings.Split(up, ";")
|
|
for _, request := range requests {
|
|
_, err := fakeDb.Exec(request)
|
|
if err != nil {
|
|
utils.Log(2, err)
|
|
}
|
|
}
|
|
utils.Log(1, fmt.Sprintf("Finished creating Test SQLite database, sending events."))
|
|
|
|
utils.Log(1, "======> Sending 'OnLoad(sqlbuilder.Database)'")
|
|
core.OnLoad(fakeDb)
|
|
utils.Log(1, "======> Sending 'OnSuccess(Service)'")
|
|
core.OnSuccess(fakeSrv)
|
|
utils.Log(1, "======> Sending 'OnFailure(Service, FailureData)'")
|
|
core.OnFailure(fakeSrv, fakeFailD)
|
|
utils.Log(1, "======> Sending 'OnSettingsSaved(Core)'")
|
|
core.OnSettingsSaved(fakeCore)
|
|
utils.Log(1, "======> Sending 'OnNewService(Service)'")
|
|
core.OnNewService(fakeSrv)
|
|
utils.Log(1, "======> Sending 'OnNewUser(User)'")
|
|
core.OnNewUser(fakeUser)
|
|
utils.Log(1, "======> Sending 'OnUpdateService(Service)'")
|
|
core.OnUpdateService(fakeSrv)
|
|
utils.Log(1, "======> Sending 'OnDeletedService(Service)'")
|
|
core.OnDeletedService(fakeSrv)
|
|
|
|
}
|