diff --git a/checker.go b/checker.go index b3841b6a..e5040bf3 100644 --- a/checker.go +++ b/checker.go @@ -10,6 +10,8 @@ import ( func CheckServices() { services, _ = SelectAllServices() + core.Communications, _ = SelectAllCommunications() + LoadDefaultCommunications() for _, v := range services { obj := v go obj.StartCheckins() diff --git a/comms/emailer.go b/comms/emailer.go index b238a6ee..fd7aaa49 100644 --- a/comms/emailer.go +++ b/comms/emailer.go @@ -4,50 +4,74 @@ import ( "bytes" "crypto/tls" "fmt" + "github.com/hunterlong/statup/types" "gopkg.in/gomail.v2" "html/template" "log" - "os" + "time" ) var ( - mailer *gomail.Dialer + Emailer *gomail.Dialer + Outgoing []*types.Email ) -func NewMailer() { - mailer = gomail.NewDialer(os.Getenv("HOST"), 587, os.Getenv("USER"), os.Getenv("PASS")) - mailer.TLSConfig = &tls.Config{InsecureSkipVerify: true} +func AddEmail(email *types.Email) { + Outgoing = append(Outgoing, email) +} - source := EmailTemplate("comms/templates/error.html", "this is coooool") +func EmailerQueue() { + defer EmailerQueue() + for _, out := range Outgoing { + fmt.Printf("sending email to: %v \n", out.To) + Send(out) + } + Outgoing = nil + fmt.Println("running emailer queue") + time.Sleep(10 * time.Second) +} - fmt.Println("source: ", source) +func Send(em *types.Email) { + source := EmailTemplate("comms/templates/error.html", nil) + m := gomail.NewMessage() + m.SetHeader("From", "info@betatude.com") + m.SetHeader("To", em.To) + m.SetHeader("Subject", em.Subject) + m.SetBody("text/html", source) + if err := Emailer.DialAndSend(m); err != nil { + fmt.Println(err) + } +} + +func SendSample(em *types.Email) { + source := EmailTemplate("comms/templates/error.html", nil) + m := gomail.NewMessage() + m.SetHeader("From", "info@betatude.com") + m.SetHeader("To", em.To) + m.SetHeader("Subject", em.Subject) + m.SetBody("text/html", source) + if err := Emailer.DialAndSend(m); err != nil { + fmt.Println(err) + } +} + +func LoadMailer(config *types.Communication) *gomail.Dialer { + Emailer = gomail.NewDialer(config.Host, config.Port, config.Username, config.Password) + Emailer.TLSConfig = &tls.Config{InsecureSkipVerify: true} + return Emailer } func EmailTemplate(tmpl string, data interface{}) string { t := template.New("error.html") - var err error t, err = t.ParseFiles(tmpl) if err != nil { panic(err) } - var tpl bytes.Buffer if err := t.Execute(&tpl, data); err != nil { log.Println(err) } - result := tpl.String() return result } - -func SendEmail(to, subject, body string) { - m := gomail.NewMessage() - m.SetHeader("From", "info@email.com") - m.SetHeader("To", to) - m.SetHeader("Subject", subject) - m.SetBody("text/html", body) - if err := mailer.DialAndSend(m); err != nil { - fmt.Println(err) - } -} diff --git a/comms/templates/error.html b/comms/templates/error.html index c7500030..51e79466 100644 --- a/comms/templates/error.html +++ b/comms/templates/error.html @@ -1,15 +1,61 @@ - - + + + + + + Sample Email + + + + + + +
+ - -

- Hello {{.}} +

+ + + +
- \ No newline at end of file diff --git a/communication.go b/communication.go index a29ce68b..0c1a4f12 100644 --- a/communication.go +++ b/communication.go @@ -1,77 +1,75 @@ package main -import "time" - -var ( - Communications []*Communication +import ( + "github.com/hunterlong/statup/comms" + "github.com/hunterlong/statup/types" + "time" ) -type Communication struct { - Id int64 `db:"id,omitempty" json:"id"` - Method string `db:"method" json:"method"` - Host string `db:"host" json:"host"` - Port int64 `db:"port" json:"port"` - User string `db:"user" json:"user"` - Password string `db:"password" json:"-"` - Var1 string `db:"var1" json:"var1"` - Var2 string `db:"var2" json:"var2"` - ApiKey string `db:"api_key" json:"api_key"` - ApiSecret string `db:"api_secret" json:"api_secret"` - Enabled bool `db:"enabled" json:"enabled"` - Limits int64 `db:"limits" json:"limits"` - Removable bool `db:"removable" json:"removable"` - CreatedAt time.Time `db:"created_at" json:"created_at"` +func LoadDefaultCommunications() { + emailer := SelectCommunication(1) + comms.LoadMailer(emailer) + go comms.EmailerQueue() } -func OnCommunicate() { - for _, c := range Communications { +func LoadComms() { + for _, c := range core.Communications { if c.Enabled { - c.Run() + } } } -func (c *Communication) Run() { +func Run(c *types.Communication) { + sample := &types.Email{ + To: "info@socialeck.com", + Subject: "Test Email from Statup", + } + + comms.AddEmail(sample) } -func SelectAllCommunications() ([]*Communication, error) { - var c []*Communication +func SelectAllCommunications() ([]*types.Communication, error) { + var c []*types.Communication col := dbSession.Collection("communication").Find() err := col.All(&c) - Communications = c + core.Communications = c return c, err } -func (c *Communication) Create() (int64, error) { +func Create(c *types.Communication) (int64, error) { c.CreatedAt = time.Now() uuid, err := dbSession.Collection("communication").Insert(c) + if err != nil { + panic(err) + } if uuid == nil { return 0, err } c.Id = uuid.(int64) - Communications = append(Communications, c) + core.Communications = append(core.Communications, c) return uuid.(int64), err } -func (c *Communication) Disable() { +func Disable(c *types.Communication) { c.Enabled = false - c.Update() + Update(c) } -func (c *Communication) Enable() { +func Enable(c *types.Communication) { c.Enabled = true - c.Update() + Update(c) } -func (c *Communication) Update() *Communication { +func Update(c *types.Communication) *types.Communication { col := dbSession.Collection("communication").Find("id", c.Id) col.Update(c) return c } -func SelectCommunication(id int64) *Communication { - for _, c := range Communications { +func SelectCommunication(id int64) *types.Communication { + for _, c := range core.Communications { if c.Id == id { return c } diff --git a/core.go b/core.go index ab15f3e4..e3a9fff5 100644 --- a/core.go +++ b/core.go @@ -3,21 +3,23 @@ package main import ( "github.com/gorilla/sessions" "github.com/hunterlong/statup/plugin" + "github.com/hunterlong/statup/types" ) type Core struct { - Name string `db:"name"` - Description string `db:"description"` - Config string `db:"config"` - ApiKey string `db:"api_key"` - ApiSecret string `db:"api_secret"` - Style string `db:"style"` - Footer string `db:"footer"` - Domain string `db:"domain"` - Version string `db:"version"` - Plugins []plugin.Info - Repos []PluginJSON - PluginFields []PluginSelect + Name string `db:"name"` + Description string `db:"description"` + Config string `db:"config"` + ApiKey string `db:"api_key"` + ApiSecret string `db:"api_secret"` + Style string `db:"style"` + Footer string `db:"footer"` + Domain string `db:"domain"` + Version string `db:"version"` + Plugins []plugin.Info + Repos []PluginJSON + PluginFields []PluginSelect + Communications []*types.Communication } func (c *Core) Update() (*Core, error) { diff --git a/database.go b/database.go index bfcdca76..5d3ee7d0 100644 --- a/database.go +++ b/database.go @@ -61,3 +61,7 @@ func DbConnection(dbType string) error { OnLoad(dbSession) return err } + +func Backup() { + +} diff --git a/failures.go b/failures.go index 35961bb6..9a7f554f 100644 --- a/failures.go +++ b/failures.go @@ -8,6 +8,7 @@ import ( type Failure struct { Id int `db:"id,omitempty"` Issue string `db:"issue"` + Method string `db:"method"` Service int64 `db:"service"` CreatedAt time.Time `db:"created_at"` } diff --git a/html/tmpl/plugins.html b/html/tmpl/plugins.html index 70828488..42c979ec 100644 --- a/html/tmpl/plugins.html +++ b/html/tmpl/plugins.html @@ -23,7 +23,10 @@ + {{ range .Communications }} +
+ +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+ +
+ {{ end }} +
{{ range .Repos }}
diff --git a/main.go b/main.go index 1ef294c7..58c18b0d 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "github.com/GeertJohan/go.rice" "github.com/go-yaml/yaml" "github.com/gorilla/sessions" - "github.com/hunterlong/statup/comms" "github.com/hunterlong/statup/plugin" "golang.org/x/crypto/bcrypt" "io" @@ -126,8 +125,6 @@ func main() { var err error fmt.Printf("Starting Statup v%v\n", VERSION) - comms.NewMailer() - RenderBoxes() configs, err = LoadConfig() if err != nil { diff --git a/main_test.go b/main_test.go index a5c68f41..c0fb73b4 100644 --- a/main_test.go +++ b/main_test.go @@ -23,6 +23,7 @@ func TestMySQLMakeConfig(t *testing.T) { 3306, "Testing MYSQL", "This is a test of Statup.io!", + "", "admin", "admin", nil, @@ -36,7 +37,7 @@ func TestMySQLMakeConfig(t *testing.T) { err = DbConnection(configs.Connection) assert.Nil(t, err) - + InsertDefaultComms() } func TestInsertMysqlSample(t *testing.T) { @@ -62,6 +63,7 @@ func TestSqliteMakeConfig(t *testing.T) { 5432, "Testing SQLITE", "This is a test of Statup.io!", + "", "admin", "admin", nil, @@ -75,6 +77,7 @@ func TestSqliteMakeConfig(t *testing.T) { err = DbConnection(configs.Connection) assert.Nil(t, err) + InsertDefaultComms() } func TestInsertSqliteSample(t *testing.T) { @@ -92,6 +95,7 @@ func TestPostgresMakeConfig(t *testing.T) { 5432, "Testing POSTGRES", "This is a test of Statup.io!", + "", "admin", "admin", nil, @@ -105,6 +109,7 @@ func TestPostgresMakeConfig(t *testing.T) { err = DbConnection(configs.Connection) assert.Nil(t, err) + InsertDefaultComms() } func TestInsertPostgresSample(t *testing.T) { @@ -130,8 +135,8 @@ func TestSelectCore(t *testing.T) { func TestUser_Create(t *testing.T) { user := &User{ - Username: "testuserhere", - Password: "password123", + Username: "admin", + Password: "admin", Email: "info@testuser.com", } id, err := user.Create() @@ -215,7 +220,7 @@ func TestService_Hits(t *testing.T) { assert.NotNil(t, service) hits, err := service.Hits() assert.Nil(t, err) - assert.Equal(t, 20, len(hits)) + assert.Equal(t, 26, len(hits)) } func TestService_LimitedHits(t *testing.T) { @@ -223,7 +228,7 @@ func TestService_LimitedHits(t *testing.T) { assert.NotNil(t, service) hits, err := service.LimitedHits() assert.Nil(t, err) - assert.Equal(t, 20, len(hits)) + assert.Equal(t, 26, len(hits)) } func Test(t *testing.T) { diff --git a/setup.go b/setup.go index b624d0e2..05ae241b 100644 --- a/setup.go +++ b/setup.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/go-yaml/yaml" "github.com/hunterlong/statup/plugin" + "github.com/hunterlong/statup/types" "net/http" "os" "strconv" @@ -20,13 +21,14 @@ type DbConfig struct { DbPort int `yaml:"port"` Project string `yaml:"-"` Description string `yaml:"-"` + Domain string `yaml:"-"` Username string `yaml:"-"` Password string `yaml:"-"` - Error error + Error error `yaml:"-"` } func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) { - if core.ApiKey != "" { + if core != nil { http.Redirect(w, r, "/", http.StatusSeeOther) return } @@ -42,6 +44,7 @@ func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) { password := r.PostForm.Get("password") sample := r.PostForm.Get("sample_data") description := r.PostForm.Get("description") + domain := r.PostForm.Get("domain") config := &DbConfig{ dbConn, @@ -52,6 +55,7 @@ func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) { dbPort, project, description, + domain, username, password, nil, @@ -97,12 +101,12 @@ func ProcessSetupHandler(w http.ResponseWriter, r *http.Request) { } func InsertDefaultComms() { - emailer := &Communication{ + emailer := &types.Communication{ Method: "email", Removable: false, Enabled: false, } - emailer.Create() + Create(emailer) } func DeleteConfig() { @@ -166,6 +170,7 @@ func (c *DbConfig) Save() error { []plugin.Info{}, []PluginJSON{}, []PluginSelect{}, + nil, } col := dbSession.Collection("core") diff --git a/sql/mysql_up.sql b/sql/mysql_up.sql index c3bcd25e..788087a0 100644 --- a/sql/mysql_up.sql +++ b/sql/mysql_up.sql @@ -46,6 +46,7 @@ CREATE TABLE hits ( CREATE TABLE failures ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, issue text, + method text, service INTEGER NOT NULL, created_at TIMESTAMP, INDEX (id, service), @@ -65,7 +66,7 @@ CREATE TABLE communication ( method text, host text, port integer, - user text, + username text, password text, var1 text, var2 text, diff --git a/sql/postgres_up.sql b/sql/postgres_up.sql index d549c5e4..5132500c 100644 --- a/sql/postgres_up.sql +++ b/sql/postgres_up.sql @@ -64,7 +64,7 @@ CREATE TABLE communication ( method text, host text, port integer, - user text, + username text, password text, var1 text, var2 text, @@ -76,7 +76,6 @@ CREATE TABLE communication ( created_at TIMESTAMP ); - CREATE INDEX idx_hits ON hits(service); CREATE INDEX idx_failures ON failures(service); CREATE INDEX idx_checkins ON checkins(service); \ No newline at end of file diff --git a/sql/sqlite_up.sql b/sql/sqlite_up.sql index 5c34f44d..513d2730 100644 --- a/sql/sqlite_up.sql +++ b/sql/sqlite_up.sql @@ -46,6 +46,7 @@ CREATE TABLE hits ( CREATE TABLE failures ( id SERIAL PRIMARY KEY, issue text, + method text, service INTEGER NOT NULL REFERENCES services(id) ON DELETE CASCADE ON UPDATE CASCADE, created_at TIMESTAMP ); @@ -63,7 +64,7 @@ CREATE TABLE communication ( method text, host text, port integer, - user text, + username text, password text, var1 text, var2 text, diff --git a/types/types.go b/types/types.go new file mode 100644 index 00000000..c6483ab2 --- /dev/null +++ b/types/types.go @@ -0,0 +1,28 @@ +package types + +import "time" + +type Communication struct { + Id int64 `db:"id,omitempty" json:"id"` + Method string `db:"method" json:"method"` + Host string `db:"host" json:"host"` + Port int `db:"port" json:"port"` + Username string `db:"username" json:"user"` + Password string `db:"password" json:"-"` + Var1 string `db:"var1" json:"var1"` + Var2 string `db:"var2" json:"var2"` + ApiKey string `db:"api_key" json:"api_key"` + ApiSecret string `db:"api_secret" json:"api_secret"` + Enabled bool `db:"enabled" json:"enabled"` + Limits int64 `db:"limits" json:"limits"` + Removable bool `db:"removable" json:"removable"` + CreatedAt time.Time `db:"created_at" json:"created_at"` +} + +type Email struct { + To string + Subject string + Template string + Data interface{} + Body string +} diff --git a/web.go b/web.go index 9f9859b5..7a0bec13 100644 --- a/web.go +++ b/web.go @@ -5,6 +5,8 @@ import ( "github.com/fatih/structs" "github.com/gorilla/mux" "github.com/gorilla/sessions" + "github.com/hunterlong/statup/comms" + "github.com/hunterlong/statup/types" "html/template" "net/http" "regexp" @@ -45,6 +47,7 @@ func Router() *mux.Router { r.Handle("/users/{id}/delete", http.HandlerFunc(UsersDeleteHandler)).Methods("GET") r.Handle("/settings", http.HandlerFunc(PluginsHandler)).Methods("GET") r.Handle("/settings", http.HandlerFunc(SaveSettingsHandler)).Methods("POST") + r.Handle("/settings/email", http.HandlerFunc(SaveEmailSettingsHandler)).Methods("POST") r.Handle("/plugins/download/{name}", http.HandlerFunc(PluginsDownloadHandler)) r.Handle("/plugins/{name}/save", http.HandlerFunc(PluginSavedHandler)).Methods("POST") r.Handle("/help", http.HandlerFunc(HelpHandler)) @@ -150,7 +153,7 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) { } func SetupHandler(w http.ResponseWriter, r *http.Request) { - if core.ApiKey != "" { + if core != nil { http.Redirect(w, r, "/", http.StatusSeeOther) return } @@ -245,6 +248,34 @@ func IsAuthenticated(r *http.Request) bool { return session.Values["authenticated"].(bool) } +func SaveEmailSettingsHandler(w http.ResponseWriter, r *http.Request) { + auth := IsAuthenticated(r) + if !auth { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + emailer := SelectCommunication(1) + + r.ParseForm() + emailer.Host = r.PostForm.Get("host") + emailer.Username = r.PostForm.Get("username") + emailer.Password = r.PostForm.Get("password") + emailer.Port = int(StringInt(r.PostForm.Get("port"))) + emailer.Var1 = r.PostForm.Get("address") + Update(emailer) + + sample := &types.Email{ + To: "info@socialeck.com", + Subject: "Sample Email", + Template: "templates/error.html", + Body: "okkokkok", + } + + comms.AddEmail(sample) + + http.Redirect(w, r, "/settings", http.StatusSeeOther) +} + func SaveSettingsHandler(w http.ResponseWriter, r *http.Request) { auth := IsAuthenticated(r) if !auth { @@ -290,6 +321,8 @@ func PluginsHandler(w http.ResponseWriter, r *http.Request) { } core.PluginFields = pluginFields + fmt.Println(core.Communications) + ExecuteResponse(w, r, "plugins.html", core) }