// +build ignore package main import ( "bytes" "encoding/json" "fmt" "github.com/statping/statping/utils" "github.com/tdewolff/minify/v2" "github.com/tdewolff/minify/v2/html" "os" "time" ) var ( mjmlApplication string mjmlPrivate string ) func main() { utils.InitEnvs() mjmlApplication = os.Getenv("MJML_APP") mjmlPrivate = os.Getenv("MJML_PRIVATE") if mjmlApplication == "" || mjmlPrivate == "" { fmt.Println("skipping email MJML template render, missing MJML_APP and MJML_PRIVATE") return } fmt.Println("Generating success/failure email templates from MJML to a HTML golang constant") success := convertMJML(emailSuccessMJML) fail := convertMJML(emailFailureMJML) htmlOut := `// DO NOT EDIT ** This file was generated with go generate on ` + utils.Now().String() + ` ** DO NOT EDIT // package notifiers const emailSuccess = ` + minimize(success) + ` const emailFailure = ` + minimize(fail) + ` ` utils.SaveFile("email_rendered.go", []byte(htmlOut)) fmt.Println("Email MJML to HTML const saved: notifiers/email_rendered.go") } type mjmlInput struct { Mjml string `json:"mjml"` } func minimize(val string) string { m := minify.New() m.Add("text/html", &html.Minifier{ KeepDefaultAttrVals: true, }) s, err := m.String("text/html", val) if err != nil { panic(err) } return fmt.Sprintf("`%s`", s) } func convertMJML(mjml string) string { input, _ := json.Marshal(mjmlInput{mjml}) auth := fmt.Sprintf("%s:%s", mjmlApplication, mjmlPrivate) resp, _, err := utils.HttpRequest("https://"+auth+"@api.mjml.io/v1/render", "POST", "application/json", nil, bytes.NewBuffer(input), 15*time.Minute, false, nil) if err != nil { panic(err) } var respData mjmlApi if err := json.Unmarshal(resp, &respData); err != nil { panic(err) } return respData.Html } type mjmlApi struct { Html string `json:"html"` Mjml string `json:"mjml"` Version string `json:"mjml_version"` } const emailFailureMJML = ` Statping Service Notification {{.Service.Name}} is currently offline, you might want to check it. Offline for {{.Service.Downtime.Human}} View Dashboard Service Domain {{.Service.Domain}} Current Issue {{.Failure.Issue}} You are receiving this email because one of your services has changed on your Statping instance. You can modify this email on the Email Notifier page in Settings. © Statping Statping.com         Github         Privacy         ` const emailSuccessMJML = ` Statping Service Notification {{.Service.Name}} is currently offline, you might want to check it. Offline for {{.Service.Downtime.Human}} View Dashboard Service Domain {{.Service.Domain}} Current Issue {{.Failure.Issue}} You are receiving this email because one of your services has changed on your Statping instance. You can modify this email on the Email Notifier page in Settings. © Statping Statping.com         Github         Privacy         `