59 lines
1.5 KiB
Go
59 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/golib/notify"
|
|
"git.haelnorr.com/h/oslstats/internal/view/page"
|
|
)
|
|
|
|
// Handles responses to the / path. Also serves a 404 Page for paths that
|
|
// don't have explicit handlers
|
|
func NotifyTester(server *hws.Server) http.Handler {
|
|
return http.HandlerFunc(
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
testErr := errors.New("This is a stack trace. No really i swear. Just pretend ok? Thanks")
|
|
if r.Method == "GET" {
|
|
// page, _ := ErrorPage(hws.HWSError{
|
|
// StatusCode: http.StatusTeapot,
|
|
// Message: "This error has been rendered as a test",
|
|
// Error: testErr,
|
|
// })
|
|
// page.Render(r.Context(), w)
|
|
page.Test().Render(r.Context(), w)
|
|
} else {
|
|
r.ParseForm()
|
|
target := r.Form.Get("target")
|
|
title := r.Form.Get("title")
|
|
level := map[string]notify.Level{
|
|
"info": notify.LevelInfo,
|
|
"success": notify.LevelSuccess,
|
|
"warn": notify.LevelWarn,
|
|
"error": notify.LevelError,
|
|
}[r.Form.Get("type")]
|
|
message := r.Form.Get("message")
|
|
nt := notify.Notification{
|
|
Target: notify.Target(target),
|
|
Title: title,
|
|
Message: message,
|
|
Level: level,
|
|
}
|
|
|
|
// For error level, serialize error details with code
|
|
if level == notify.LevelError {
|
|
nt.Details = SerializeErrorDetails(500, testErr)
|
|
}
|
|
|
|
if target == "all" {
|
|
server.NotifyAll(nt)
|
|
} else {
|
|
server.NotifySub(nt)
|
|
}
|
|
}
|
|
},
|
|
)
|
|
}
|