summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-08-07 20:31:17 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-08-07 20:45:26 -0400
commitce64aa9499e4497e193749bf488613afcb6bc47d (patch)
tree281da368e62efccf069a5d6eeac2da0eb41ed3ff
parentf28c494bfe2993d59ff736976c29a67ee9d09109 (diff)
Implement subscribe status and contact form handlers
-rw-r--r--internal/api/handlers.go184
-rw-r--r--main.go5
2 files changed, 103 insertions, 86 deletions
diff --git a/internal/api/handlers.go b/internal/api/handlers.go
index aba73a2..638dc6a 100644
--- a/internal/api/handlers.go
+++ b/internal/api/handlers.go
@@ -20,10 +20,10 @@ import (
"encoding/json"
"fmt"
"io"
- "net/http"
"net"
- "strings"
+ "net/http"
"regexp"
+ "strings"
"time"
)
@@ -91,19 +91,19 @@ func CreateLoginHandler(logs *[LogLength]string, n *int, logChan chan string) ht
const (
StatusOperational = "operational"
- StatusDegraded = "degraded"
- StatusDown = "down"
+ StatusDegraded = "degraded"
+ StatusDown = "down"
)
const (
- IconAPIGateway = "🔗"
- IconWeb = "🌐"
- IconAuth = "🔐"
- IconDB = "🗄️"
- IconStorage = "📁"
+ IconAPIGateway = "🔗"
+ IconWeb = "🌐"
+ IconAuth = "🔐"
+ IconDB = "🗄️"
+ IconStorage = "📁"
IconNotifications = "📧"
- IconSearch = "🔍"
- IconAnalytics = "📊"
+ IconSearch = "🔍"
+ IconAnalytics = "📊"
)
const (
@@ -112,40 +112,40 @@ const (
)
const (
- ServiceAPIGateway = "api"
- ServiceWeb = "web"
- ServiceAuth = "auth"
- ServiceDB = "database"
- ServiceStorage = "storage"
+ ServiceAPIGateway = "api"
+ ServiceWeb = "web"
+ ServiceAuth = "auth"
+ ServiceDB = "database"
+ ServiceStorage = "storage"
ServiceNotifications = "notifications"
- ServiceSearch = "search"
- ServiceAnalytics = "analytics"
+ ServiceSearch = "search"
+ ServiceAnalytics = "analytics"
)
func StatusHandler(w http.ResponseWriter, r *http.Request) {
type response struct {
- Status string `json:"status"`
- Description string `json:"description"`
- Uptime float64 `json:"uptime"`
- ResponseTime int `json:"responseTime"`
- ActiveIncidents int `json:"activeIncidents"`
- ScheduledMaintenance int `json:"scheduledMaintenance"`
+ Status string `json:"status"`
+ Description string `json:"description"`
+ Uptime float64 `json:"uptime"`
+ ResponseTime int `json:"responseTime"`
+ ActiveIncidents int `json:"activeIncidents"`
+ ScheduledMaintenance int `json:"scheduledMaintenance"`
}
// res := response{
- // Status: StatusDown,
- // Description: "Everything is on fire",
- // Uptime: 0.0,
- // ResponseTime: 0.0,
- // ActiveIncidents: 9001,
- // ScheduledMaintenance: 0,
+ // Status: StatusDown,
+ // Description: "Everything is on fire",
+ // Uptime: 0.0,
+ // ResponseTime: 0.0,
+ // ActiveIncidents: 9001,
+ // ScheduledMaintenance: 0,
// }
res := response{
- Status: StatusDegraded,
- Description: "Everything is not great",
- Uptime: 50.0,
- ResponseTime: 20000,
- ActiveIncidents: 9001,
+ Status: StatusDegraded,
+ Description: "Everything is not great",
+ Uptime: 50.0,
+ ResponseTime: 20000,
+ ActiveIncidents: 9001,
ScheduledMaintenance: 1,
}
@@ -155,54 +155,54 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
func StatusServicesHandler(w http.ResponseWriter, r *http.Request) {
type service struct {
- Id string `json:"id"`
- Name string `json:"name"`
- Description string `json:"description"`
- Icon string `json:"icon"`
- Status string `json:"status"`
- ResponseTime int `json:"responseTime"`
- Uptime float64 `json:"uptime"`
+ Id string `json:"id"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ Icon string `json:"icon"`
+ Status string `json:"status"`
+ ResponseTime int `json:"responseTime"`
+ Uptime float64 `json:"uptime"`
}
response := []service{}
response = append(response, service{
- Id: ServiceAPIGateway,
- Name: "API Gateway",
- Description: "Core API services",
- Icon: IconAPIGateway,
- Status: StatusOperational,
+ Id: ServiceAPIGateway,
+ Name: "API Gateway",
+ Description: "Core API services",
+ Icon: IconAPIGateway,
+ Status: StatusOperational,
ResponseTime: 50,
- Uptime: 69.420,
+ Uptime: 69.420,
})
response = append(response, service{
- Id: "logs",
- Name: "Log Warden",
- Description: "The master of the Logs",
- Icon: "📜",
- Status: StatusOperational,
+ Id: "logs",
+ Name: "Log Warden",
+ Description: "The master of the Logs",
+ Icon: "📜",
+ Status: StatusOperational,
ResponseTime: 69,
- Uptime: 99.999,
+ Uptime: 99.999,
})
-
+
jsonData, _ := json.Marshal(response)
fmt.Fprint(w, string(jsonData))
}
func StatusMetricsHandler(w http.ResponseWriter, r *http.Request) {
type response struct {
- Uptime float64 `json:"uptime"`
- ResponseTime int `json:"responseTime"`
- RequestVolume int `json:"requestVolume"`
- ErrorRate float64 `json:"errorRate"`
+ Uptime float64 `json:"uptime"`
+ ResponseTime int `json:"responseTime"`
+ RequestVolume int `json:"requestVolume"`
+ ErrorRate float64 `json:"errorRate"`
}
res := response{
- Uptime: 90.5,
- ResponseTime: 169,
+ Uptime: 90.5,
+ ResponseTime: 169,
RequestVolume: 5,
- ErrorRate: 106.0,
+ ErrorRate: 106.0,
}
jsonData, _ := json.Marshal(res)
@@ -211,13 +211,13 @@ func StatusMetricsHandler(w http.ResponseWriter, r *http.Request) {
func StatusIncidentsHandler(w http.ResponseWriter, r *http.Request) {
type incident struct {
- Id string `json:"id"`
- Title string `json:"title"`
- Description string `json:"description"`
- Status string `json:"status"`
- Severity string `json:"severity"`
- StartTime time.Time `json:"startTime"`
- AffectedServices []string `json:"affectedServices"`
+ Id string `json:"id"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ Status string `json:"status"`
+ Severity string `json:"severity"`
+ StartTime time.Time `json:"startTime"`
+ AffectedServices []string `json:"affectedServices"`
}
response := []incident{}
@@ -227,12 +227,12 @@ func StatusIncidentsHandler(w http.ResponseWriter, r *http.Request) {
func StatusMaintenanceHandler(w http.ResponseWriter, r *http.Request) {
type event struct {
- Id string `json:"id"`
- Title string `json:"title"`
- Description string `json:"description"`
- StartTime time.Time `json:"startTime"`
- EndTime time.Time `json:"endTime"`
- AffectedServices []string `json:"affectedServices"`
+ Id string `json:"id"`
+ Title string `json:"title"`
+ Description string `json:"description"`
+ StartTime time.Time `json:"startTime"`
+ EndTime time.Time `json:"endTime"`
+ AffectedServices []string `json:"affectedServices"`
}
response := []event{}
@@ -242,8 +242,8 @@ func StatusMaintenanceHandler(w http.ResponseWriter, r *http.Request) {
func StatusUptimeHandler(w http.ResponseWriter, r *http.Request) {
type event struct {
- Date time.Time `json:"date"`
- Uptime float64 `json:"uptime"`
+ Date time.Time `json:"date"`
+ Uptime float64 `json:"uptime"`
}
response := []event{}
@@ -251,14 +251,32 @@ func StatusUptimeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, string(jsonData))
}
-// TODO: SUBSCRIBE TO EMAILS
-func StatusSubscribeHandler(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "{}")
+func CreateStatusSubscribeHandler(logs *[LogLength]string, n *int, logChan chan string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Bad Request", http.StatusBadRequest)
+ return
+ }
+ defer r.Body.Close()
+
+ addRotLog(logs, n, logChan, fmt.Sprintf(`{"subscribeEmails": %s}`, string(body)))
+ fmt.Fprint(w, "{}")
+ }
}
-// TODO: CONTACT HANDLER
-func ContactHandler(w http.ResponseWriter, r *http.Request) {
-
+func CreateContactHandler(logs *[LogLength]string, n *int, logChan chan string) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ http.Error(w, "Bad Request", http.StatusBadRequest)
+ return
+ }
+ defer r.Body.Close()
+
+ addRotLog(logs, n, logChan, fmt.Sprintf(`{"contactMessage": %s}`, string(body)))
+ fmt.Fprint(w, "{}")
+ }
}
func redactIP(input string) string {
diff --git a/main.go b/main.go
index b92c0f4..b74c7d0 100644
--- a/main.go
+++ b/main.go
@@ -51,9 +51,9 @@ func main() {
http.HandleFunc("/api/v1/status/incidents", api.StatusIncidentsHandler)
http.HandleFunc("/api/v1/status/maintenance", api.StatusMaintenanceHandler)
http.HandleFunc("/api/v1/status/uptime", api.StatusUptimeHandler)
- http.HandleFunc("/api/v1/status/subscribe", api.StatusSubscribeHandler)
+ http.HandleFunc("/api/v1/status/subscribe", api.CreateStatusSubscribeHandler(&logs, &n, logChan))
- http.HandleFunc("/api/v1/contact", api.ContactHandler)
+ http.HandleFunc("/api/v1/contact", api.CreateContactHandler(&logs, &n, logChan))
// Start the server on port 8080
fmt.Println("Server is listening on port 8080...")
@@ -73,4 +73,3 @@ func parser(input chan string, king *string) {
}
}
}
-