statping/setup.go

116 lines
2.1 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
import (
2018-06-10 01:47:57 +00:00
"github.com/go-yaml/yaml"
2018-06-15 04:30:10 +00:00
"github.com/hunterlong/statup/plugin"
2018-06-10 01:31:13 +00:00
"net/http"
"os"
2018-06-10 01:47:57 +00:00
"strconv"
2018-06-10 01:31:13 +00:00
"time"
)
type DbConfig struct {
2018-06-15 04:30:10 +00:00
DbConn string `yaml:"connection"`
DbHost string `yaml:"host"`
DbUser string `yaml:"user"`
DbPass string `yaml:"password"`
DbData string `yaml:"database"`
DbPort int `yaml:"port"`
Project string `yaml:"-"`
Description string `yaml:"-"`
Username string `yaml:"-"`
Password string `yaml:"-"`
2018-06-10 01:31:13 +00:00
}
func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
dbHost := r.PostForm.Get("db_host")
dbUser := r.PostForm.Get("db_user")
dbPass := r.PostForm.Get("db_password")
dbDatabase := r.PostForm.Get("db_database")
dbConn := r.PostForm.Get("db_connection")
dbPort, _ := strconv.Atoi(r.PostForm.Get("db_port"))
project := r.PostForm.Get("project")
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
2018-06-15 04:30:10 +00:00
sample := r.PostForm.Get("sample_data")
description := r.PostForm.Get("description")
2018-06-10 01:31:13 +00:00
config := &DbConfig{
dbConn,
dbHost,
dbUser,
dbPass,
dbDatabase,
dbPort,
project,
2018-06-15 04:30:10 +00:00
description,
2018-06-10 01:31:13 +00:00
username,
password,
}
err := config.Save()
if err != nil {
2018-06-15 04:30:10 +00:00
throw(err)
}
configs, err = LoadConfig()
if err != nil {
throw(err)
}
err = DbConnection(configs.Connection)
if err != nil {
throw(err)
}
if sample == "on" {
LoadSampleData()
2018-06-10 01:31:13 +00:00
}
http.Redirect(w, r, "/", http.StatusSeeOther)
time.Sleep(2 * time.Second)
mainProcess()
}
func (c *DbConfig) Save() error {
var err error
config, err := os.Create("config.yml")
if err != nil {
return err
}
data, err := yaml.Marshal(c)
if err != nil {
return err
}
config.WriteString(string(data))
config.Close()
2018-06-14 06:50:47 +00:00
configs, err = LoadConfig()
if err != nil {
return err
}
2018-06-14 15:24:58 +00:00
err = DbConnection(configs.Connection)
2018-06-14 06:50:47 +00:00
if err != nil {
return err
}
2018-06-10 01:31:13 +00:00
DropDatabase()
CreateDatabase()
2018-06-15 04:30:10 +00:00
newCore := Core{
c.Project,
c.Description,
"config.yml",
NewSHA1Hash(5),
NewSHA1Hash(10),
VERSION,
[]plugin.Info{},
[]PluginJSON{},
[]PluginSelect{},
}
col := dbSession.Collection("core")
_, err = col.Insert(newCore)
2018-06-10 01:31:13 +00:00
return err
}