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-06-30 00:57:05 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2018-07-07 05:02:47 +00:00
|
|
|
"errors"
|
2018-07-12 04:49:18 +00:00
|
|
|
"fmt"
|
2018-06-30 00:57:05 +00:00
|
|
|
"github.com/go-yaml/yaml"
|
2020-02-25 07:41:28 +00:00
|
|
|
"github.com/hunterlong/statping/database"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/types"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
2018-06-30 00:57:05 +00:00
|
|
|
)
|
|
|
|
|
2018-11-16 16:42:49 +00:00
|
|
|
// ErrorResponse is used for HTTP errors to show to User
|
2018-09-25 07:03:49 +00:00
|
|
|
type ErrorResponse struct {
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:53:13 +00:00
|
|
|
// LoadConfigFile will attempt to load the 'config.yml' file in a specific directory
|
2020-02-26 05:38:03 +00:00
|
|
|
func LoadConfigFile(directory string) (*DbConfig, error) {
|
|
|
|
var configs *DbConfig
|
|
|
|
|
|
|
|
dbConn := utils.Getenv("DB_CONN", "")
|
|
|
|
|
|
|
|
if dbConn != "" {
|
|
|
|
log.Infof("DB_CONN=%s environment variable was found, waiting for database...", dbConn)
|
2018-07-07 05:02:47 +00:00
|
|
|
return LoadUsingEnv()
|
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
log.Debugln("Attempting to read config file at: " + directory + "/config.yml")
|
2020-02-26 06:39:54 +00:00
|
|
|
file, err := utils.OpenFile(directory + "/config.yml")
|
2018-06-30 00:57:05 +00:00
|
|
|
if err != nil {
|
2020-01-28 12:15:48 +00:00
|
|
|
CoreApp.Setup = false
|
2018-09-05 10:54:57 +00:00
|
|
|
return nil, errors.New("config.yml file not found at " + directory + "/config.yml - starting in setup mode")
|
2018-07-07 05:02:47 +00:00
|
|
|
}
|
2020-02-26 06:39:54 +00:00
|
|
|
err = yaml.Unmarshal([]byte(file), &configs)
|
2018-07-28 01:50:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-30 08:08:51 +00:00
|
|
|
log.WithFields(utils.ToFields(configs)).Debugln("read config file: " + directory + "/config.yml")
|
2020-02-26 05:38:03 +00:00
|
|
|
CoreApp.Config = configs.DbConfig
|
2019-12-30 08:08:51 +00:00
|
|
|
return configs, err
|
2018-07-07 05:02:47 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:16:23 +00:00
|
|
|
// LoadUsingEnv will attempt to load database configs based on environment variables. If DB_CONN is set if will force this function.
|
2020-02-26 05:38:03 +00:00
|
|
|
func LoadUsingEnv() (*DbConfig, error) {
|
2019-02-11 21:10:17 +00:00
|
|
|
Configs, err := EnvToConfig()
|
|
|
|
if err != nil {
|
|
|
|
return Configs, err
|
2018-07-07 05:02:47 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
|
|
|
|
CoreApp.Name = utils.Getenv("NAME", "").(string)
|
|
|
|
CoreApp.Domain = utils.Getenv("DOMAIN", Configs.LocalIP).(string)
|
|
|
|
CoreApp.UseCdn = types.NewNullBool(utils.Getenv("USE_CDN", false).(bool))
|
2018-11-07 05:06:44 +00:00
|
|
|
|
2019-12-30 08:08:51 +00:00
|
|
|
err = CoreApp.Connect(true, utils.Directory)
|
2018-07-12 04:49:18 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2018-07-12 04:49:18 +00:00
|
|
|
return nil, err
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
if err := Configs.Save(); err != nil {
|
2020-02-25 07:41:28 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 10:54:57 +00:00
|
|
|
exists := DbSession.HasTable("core")
|
2018-07-12 04:49:18 +00:00
|
|
|
if !exists {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Infoln(fmt.Sprintf("Core database does not exist, creating now!"))
|
2020-02-26 05:38:03 +00:00
|
|
|
if err := CoreApp.DropDatabase(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := CoreApp.CreateDatabase(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
CoreApp, err = Configs.InsertCore()
|
2018-07-12 04:49:18 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2018-07-12 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 05:38:03 +00:00
|
|
|
username := utils.Getenv("ADMIN_USER", "admin").(string)
|
|
|
|
password := utils.Getenv("ADMIN_PASSWORD", "admin").(string)
|
2019-04-03 15:15:46 +00:00
|
|
|
|
2020-02-25 07:41:28 +00:00
|
|
|
admin := &types.User{
|
2019-04-03 15:15:46 +00:00
|
|
|
Username: username,
|
2020-02-26 05:38:03 +00:00
|
|
|
Password: utils.HashPassword(password),
|
2018-07-12 04:49:18 +00:00
|
|
|
Email: "info@admin.com",
|
2018-11-07 07:53:39 +00:00
|
|
|
Admin: types.NewNullBool(true),
|
2020-02-25 07:41:28 +00:00
|
|
|
}
|
|
|
|
if _, err := database.Create(admin); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := SampleData(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-12 04:49:18 +00:00
|
|
|
|
|
|
|
return Configs, err
|
2018-10-11 16:53:13 +00:00
|
|
|
}
|
|
|
|
return Configs, nil
|
|
|
|
}
|
2018-07-12 04:49:18 +00:00
|
|
|
|
2020-01-04 02:03:59 +00:00
|
|
|
// defaultPort accepts a database type and returns its default port
|
2020-02-26 06:59:56 +00:00
|
|
|
func defaultPort(db string) int {
|
2018-10-11 16:53:13 +00:00
|
|
|
switch db {
|
|
|
|
case "mysql":
|
|
|
|
return 3306
|
|
|
|
case "postgres":
|
|
|
|
return 5432
|
|
|
|
case "mssql":
|
|
|
|
return 1433
|
|
|
|
default:
|
|
|
|
return 0
|
2018-07-12 04:49:18 +00:00
|
|
|
}
|
2018-10-11 16:53:13 +00:00
|
|
|
}
|
2018-07-12 04:49:18 +00:00
|
|
|
|
2018-10-11 16:53:13 +00:00
|
|
|
// EnvToConfig converts environment variables to a DbConfig variable
|
2020-02-26 05:38:03 +00:00
|
|
|
func EnvToConfig() (*DbConfig, error) {
|
2019-02-11 21:10:17 +00:00
|
|
|
var err error
|
2020-02-26 05:38:03 +00:00
|
|
|
|
|
|
|
dbConn := utils.Getenv("DB_CONN", "").(string)
|
|
|
|
dbHost := utils.Getenv("DB_HOST", "").(string)
|
|
|
|
dbUser := utils.Getenv("DB_USER", "").(string)
|
|
|
|
dbPass := utils.Getenv("DB_PASS", "").(string)
|
|
|
|
dbData := utils.Getenv("DB_DATABASE", "").(string)
|
2020-02-26 06:59:56 +00:00
|
|
|
dbPort := utils.Getenv("DB_PORT", defaultPort(dbConn)).(int)
|
2020-02-26 05:38:03 +00:00
|
|
|
name := utils.Getenv("NAME", "Statping").(string)
|
|
|
|
desc := utils.Getenv("DESCRIPTION", "Statping Monitoring Sample Data").(string)
|
|
|
|
user := utils.Getenv("ADMIN_USER", "admin").(string)
|
|
|
|
password := utils.Getenv("ADMIN_PASS", "admin").(string)
|
|
|
|
domain := utils.Getenv("DOMAIN", "").(string)
|
|
|
|
sqlFile := utils.Getenv("SQL_FILE", "").(string)
|
|
|
|
|
|
|
|
if dbConn != "sqlite" {
|
|
|
|
if dbHost == "" {
|
2019-12-30 08:08:51 +00:00
|
|
|
return nil, errors.New("Missing DB_HOST environment variable")
|
2019-02-11 21:10:17 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
if dbUser == "" {
|
2019-12-30 08:08:51 +00:00
|
|
|
return nil, errors.New("Missing DB_USER environment variable")
|
2019-02-11 21:10:17 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
if dbPass == "" {
|
2019-12-30 08:08:51 +00:00
|
|
|
return nil, errors.New("Missing DB_PASS environment variable")
|
2019-02-11 21:10:17 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
if dbData == "" {
|
2019-12-30 08:08:51 +00:00
|
|
|
return nil, errors.New("Missing DB_DATABASE environment variable")
|
2019-02-11 21:10:17 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-21 08:45:55 +00:00
|
|
|
|
2020-02-26 05:38:03 +00:00
|
|
|
CoreApp.Config = &types.DbConfig{
|
|
|
|
DbConn: dbConn,
|
|
|
|
DbHost: dbHost,
|
|
|
|
DbUser: dbUser,
|
|
|
|
DbPass: dbPass,
|
|
|
|
DbData: dbData,
|
|
|
|
DbPort: dbPort,
|
2018-10-11 16:53:13 +00:00
|
|
|
Project: name,
|
2020-02-26 05:38:03 +00:00
|
|
|
Description: desc,
|
|
|
|
Domain: domain,
|
2018-10-11 16:53:13 +00:00
|
|
|
Email: "",
|
2020-02-26 05:38:03 +00:00
|
|
|
Username: user,
|
|
|
|
Password: password,
|
2018-10-11 16:53:13 +00:00
|
|
|
Error: nil,
|
|
|
|
Location: utils.Directory,
|
2020-02-26 05:38:03 +00:00
|
|
|
SqlFile: sqlFile,
|
2018-10-11 16:53:13 +00:00
|
|
|
}
|
2020-02-26 05:38:03 +00:00
|
|
|
|
|
|
|
return &DbConfig{CoreApp.Config}, err
|
2018-10-11 16:53:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 05:57:11 +00:00
|
|
|
// SampleData runs all the sample data for a new Statping installation
|
2018-10-11 16:53:13 +00:00
|
|
|
func SampleData() error {
|
|
|
|
if err := InsertSampleData(); err != nil {
|
2020-01-04 02:03:59 +00:00
|
|
|
log.Errorln(err)
|
2018-10-11 16:53:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := InsertSampleHits(); err != nil {
|
2020-01-04 02:03:59 +00:00
|
|
|
log.Errorln(err)
|
2018-10-11 16:53:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
2018-09-15 01:18:21 +00:00
|
|
|
|
|
|
|
// DeleteConfig will delete the 'config.yml' file
|
2018-10-11 16:53:13 +00:00
|
|
|
func DeleteConfig() error {
|
2019-12-30 08:08:51 +00:00
|
|
|
log.Debugln("deleting config yaml file", utils.Directory+"/config.yml")
|
|
|
|
err := utils.DeleteFile(utils.Directory + "/config.yml")
|
2018-09-15 01:18:21 +00:00
|
|
|
if err != nil {
|
2019-12-28 09:01:07 +00:00
|
|
|
log.Errorln(err)
|
2018-10-11 16:53:13 +00:00
|
|
|
return err
|
2018-09-15 01:18:21 +00:00
|
|
|
}
|
2018-10-11 16:53:13 +00:00
|
|
|
return nil
|
2018-09-15 01:18:21 +00:00
|
|
|
}
|