mirror of
https://github.com/airlabspl/uptimemonitor.git
synced 2025-08-14 12:19:19 +02:00
initial commit
This commit is contained in:
30
form/login_form.go
Normal file
30
form/login_form.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package form
|
||||
|
||||
import "net/mail"
|
||||
|
||||
type LoginForm struct {
|
||||
Email string
|
||||
Password string
|
||||
|
||||
Errors map[string]string
|
||||
}
|
||||
|
||||
func (f *LoginForm) Validate() bool {
|
||||
f.Errors = map[string]string{}
|
||||
|
||||
if f.Email == "" {
|
||||
f.Errors["Email"] = "The email is required"
|
||||
}
|
||||
|
||||
if f.Email == "" {
|
||||
f.Errors["Email"] = "The email is required"
|
||||
} else if _, err := mail.ParseAddress(f.Email); err != nil {
|
||||
f.Errors["Email"] = "The email format is invalid"
|
||||
}
|
||||
|
||||
if f.Password == "" {
|
||||
f.Errors["Password"] = "The password is required"
|
||||
}
|
||||
|
||||
return len(f.Errors) == 0
|
||||
}
|
69
form/monitor_form.go
Normal file
69
form/monitor_form.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package form
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type MonitorForm struct {
|
||||
Url string
|
||||
HttpMethod string
|
||||
HasCustomHeaders bool
|
||||
HttpHeaders string
|
||||
HasCustomBody bool
|
||||
HttpBody string
|
||||
HasWebhook bool
|
||||
WebhookMethod string
|
||||
WebhookUrl string
|
||||
WebhookBody string
|
||||
WebhookHeaders string
|
||||
|
||||
Errors map[string]string
|
||||
}
|
||||
|
||||
func (f *MonitorForm) Validate() bool {
|
||||
f.Errors = map[string]string{}
|
||||
|
||||
if f.Url == "" {
|
||||
f.Errors["Url"] = "The url is required"
|
||||
} else if _, err := url.ParseRequestURI(f.Url); err != nil {
|
||||
f.Errors["Url"] = "The url is invalid"
|
||||
}
|
||||
|
||||
methods := []string{
|
||||
http.MethodGet, http.MethodPost, http.MethodPut,
|
||||
http.MethodPatch, http.MethodDelete,
|
||||
}
|
||||
|
||||
if !slices.Contains(methods, f.HttpMethod) {
|
||||
f.Errors["HttpMethod"] = "The http method is invalid"
|
||||
}
|
||||
|
||||
if f.HasCustomHeaders {
|
||||
headers := map[string]any{}
|
||||
err := json.Unmarshal([]byte(f.HttpHeaders), &headers)
|
||||
|
||||
if err != nil {
|
||||
f.Errors["HttpHeaders"] = "The http headers should be a valid JSON"
|
||||
}
|
||||
}
|
||||
|
||||
if f.HasWebhook {
|
||||
if f.WebhookUrl == "" {
|
||||
f.Errors["WebhookUrl"] = "The webhook url is required"
|
||||
} else if _, err := url.ParseRequestURI(f.WebhookUrl); err != nil {
|
||||
f.Errors["WebhookUrl"] = "The webhook url is invalid"
|
||||
}
|
||||
|
||||
headers := map[string]any{}
|
||||
err := json.Unmarshal([]byte(f.WebhookHeaders), &headers)
|
||||
|
||||
if err != nil {
|
||||
f.Errors["WebhookHeaders"] = "The webhook headers should be a valid JSON"
|
||||
}
|
||||
}
|
||||
|
||||
return len(f.Errors) == 0
|
||||
}
|
31
form/setup_form.go
Normal file
31
form/setup_form.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package form
|
||||
|
||||
import "net/mail"
|
||||
|
||||
type SetupForm struct {
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
|
||||
Errors map[string]string
|
||||
}
|
||||
|
||||
func (f *SetupForm) Validate() bool {
|
||||
f.Errors = map[string]string{}
|
||||
|
||||
if f.Name == "" {
|
||||
f.Errors["Name"] = "The name is required"
|
||||
}
|
||||
|
||||
if f.Email == "" {
|
||||
f.Errors["Email"] = "The email is required"
|
||||
} else if _, err := mail.ParseAddress(f.Email); err != nil {
|
||||
f.Errors["Email"] = "The email format is invalid"
|
||||
}
|
||||
|
||||
if f.Password == "" {
|
||||
f.Errors["Password"] = "The password is required"
|
||||
}
|
||||
|
||||
return len(f.Errors) == 0
|
||||
}
|
Reference in New Issue
Block a user