add a page for tickets from gitea (#2)
Co-authored-by: Blake Ridgway <blake@blakeridgway.com> Reviewed-on: https://git.ridgwaysystems.org/RidgwaySystems/rs_website/pulls/2
This commit is contained in:
@@ -7,11 +7,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/changelog"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/newsletter"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
)
|
||||
@@ -91,6 +93,13 @@ func (h *Handler) AdminRouter(w http.ResponseWriter, r *http.Request) {
|
||||
h.requireAuth(h.adminNewsletter)(w, r)
|
||||
}
|
||||
|
||||
case path == "/admin/outages":
|
||||
if r.Method == http.MethodPost {
|
||||
h.requireAuth(h.adminOutagesPost)(w, r)
|
||||
} else {
|
||||
h.requireAuth(h.adminOutagesGet)(w, r)
|
||||
}
|
||||
|
||||
default:
|
||||
h.renderErr(w, http.StatusNotFound, "Admin page not found.")
|
||||
}
|
||||
@@ -549,6 +558,74 @@ func (h *Handler) adminChangelogDelete(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/admin/changelog?flash=Entry+deleted", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// --- Outage tagger ---
|
||||
|
||||
type adminOutagesData struct {
|
||||
Issues []gitea.Issue
|
||||
Label string
|
||||
Flash string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (h *Handler) adminOutagesGet(w http.ResponseWriter, r *http.Request) {
|
||||
if h.gitea == nil || h.giteaOwner == "" || h.giteaRepo == "" {
|
||||
h.render(w, "admin-outages", adminOutagesData{Error: "Gitea not configured. Set GITEA_URL, GITEA_TOKEN, GITEA_OWNER, and GITEA_REPO."})
|
||||
return
|
||||
}
|
||||
issues, err := h.gitea.ListOpenIssues(h.giteaOwner, h.giteaRepo)
|
||||
if err != nil {
|
||||
h.render(w, "admin-outages", adminOutagesData{Error: "Could not load issues: " + err.Error()})
|
||||
return
|
||||
}
|
||||
h.render(w, "admin-outages", adminOutagesData{
|
||||
Issues: issues,
|
||||
Label: h.giteaLabel,
|
||||
Flash: r.URL.Query().Get("flash"),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) adminOutagesPost(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
h.renderErr(w, http.StatusBadRequest, "Bad form data.")
|
||||
return
|
||||
}
|
||||
if h.gitea == nil || h.giteaOwner == "" || h.giteaRepo == "" {
|
||||
h.renderErr(w, http.StatusServiceUnavailable, "Gitea not configured.")
|
||||
return
|
||||
}
|
||||
|
||||
action := r.FormValue("action") // "add" or "remove"
|
||||
issueNum, err := strconv.Atoi(r.FormValue("issue"))
|
||||
if err != nil || issueNum <= 0 {
|
||||
h.renderErr(w, http.StatusBadRequest, "Invalid issue number.")
|
||||
return
|
||||
}
|
||||
|
||||
label, err := h.gitea.GetOrCreateLabel(h.giteaOwner, h.giteaRepo, h.giteaLabel, "#e11d48")
|
||||
if err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Label error: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "add":
|
||||
if err := h.gitea.AddLabel(h.giteaOwner, h.giteaRepo, issueNum, label.ID); err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Could not add label: "+err.Error())
|
||||
return
|
||||
}
|
||||
case "remove":
|
||||
if err := h.gitea.RemoveLabel(h.giteaOwner, h.giteaRepo, issueNum, label.ID); err != nil {
|
||||
h.renderErr(w, http.StatusInternalServerError, "Could not remove label: "+err.Error())
|
||||
return
|
||||
}
|
||||
default:
|
||||
h.renderErr(w, http.StatusBadRequest, "Unknown action.")
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/admin/outages?flash=Updated+#"+strconv.Itoa(issueNum), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// sanitizeSlug ensures a slug is filesystem-safe.
|
||||
func sanitizeSlug(s string) string {
|
||||
s = strings.ToLower(strings.TrimSpace(s))
|
||||
|
||||
@@ -12,9 +12,11 @@ import (
|
||||
"time"
|
||||
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/newsletter"
|
||||
"ridgwaysystems.org/website/internal/ratelimit"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
"ridgwaysystems.org/website/internal/twitch"
|
||||
)
|
||||
|
||||
// Handler holds shared dependencies for all HTTP handlers.
|
||||
@@ -27,6 +29,11 @@ type Handler struct {
|
||||
contactEmail string
|
||||
devMode bool
|
||||
postLimit *ratelimit.Limiter // rate-limits contact + newsletter POSTs
|
||||
gitea *gitea.Client
|
||||
giteaOwner string
|
||||
giteaRepo string
|
||||
giteaLabel string
|
||||
twitch *twitch.Checker
|
||||
}
|
||||
|
||||
// New creates a Handler. dataDir is the path to the data/ directory.
|
||||
@@ -39,6 +46,15 @@ func New(store *blog.Store, news *newsletter.Store, dataDir string) *Handler {
|
||||
contactEmail: getenv("CONTACT_EMAIL", "hire@ridgwaysystems.org"),
|
||||
devMode: os.Getenv("DEV") == "1",
|
||||
postLimit: ratelimit.New(10*time.Minute, 5),
|
||||
giteaOwner: getenv("GITEA_OWNER", ""),
|
||||
giteaRepo: getenv("GITEA_REPO", ""),
|
||||
giteaLabel: getenv("GITEA_LABEL", "planned-outage"),
|
||||
}
|
||||
if url := os.Getenv("GITEA_URL"); url != "" {
|
||||
h.gitea = gitea.New(url, os.Getenv("GITEA_TOKEN"))
|
||||
}
|
||||
if id, secret, ch := os.Getenv("TWITCH_CLIENT_ID"), os.Getenv("TWITCH_CLIENT_SECRET"), os.Getenv("TWITCH_CHANNEL"); id != "" && secret != "" && ch != "" {
|
||||
h.twitch = twitch.New(id, secret, ch)
|
||||
}
|
||||
if !h.devMode {
|
||||
h.templates = mustLoadTemplates()
|
||||
@@ -46,6 +62,18 @@ func New(store *blog.Store, news *newsletter.Store, dataDir string) *Handler {
|
||||
return h
|
||||
}
|
||||
|
||||
// StartTwitch begins background polling for stream live status.
|
||||
// interval is how often to check; if zero a default of 2 minutes is used.
|
||||
func (h *Handler) StartTwitch(interval time.Duration) {
|
||||
if h.twitch == nil {
|
||||
return
|
||||
}
|
||||
if interval <= 0 {
|
||||
interval = 2 * time.Minute
|
||||
}
|
||||
h.twitch.Start(interval)
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
@@ -95,6 +123,8 @@ func mustLoadTemplates() map[string]*template.Template {
|
||||
{"admin-newsletter", "templates/admin/newsletter.html"},
|
||||
{"admin-changelog", "templates/admin/changelog.html"},
|
||||
{"admin-changelog-editor", "templates/admin/changelog-editor.html"},
|
||||
{"admin-outages", "templates/admin/outages.html"},
|
||||
{"stream", "templates/stream.html"},
|
||||
}
|
||||
|
||||
for _, p := range pages {
|
||||
@@ -110,8 +140,10 @@ func mustLoadTemplates() map[string]*template.Template {
|
||||
|
||||
// baseEnvelope wraps page-specific data with shared layout data for the base template.
|
||||
type baseEnvelope struct {
|
||||
Banner *siteBanner
|
||||
Inner any
|
||||
Banner *siteBanner
|
||||
Inner any
|
||||
TwitchLive bool
|
||||
TwitchChannel string
|
||||
}
|
||||
|
||||
// siteBanner holds the data for the site-wide status banner.
|
||||
@@ -164,8 +196,13 @@ func (h *Handler) render(w http.ResponseWriter, name string, data any) {
|
||||
http.Error(w, "template not found: "+name, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
env := baseEnvelope{Banner: h.computeBanner(), Inner: data}
|
||||
if h.twitch != nil {
|
||||
env.TwitchLive = h.twitch.IsLive()
|
||||
env.TwitchChannel = h.twitch.Channel()
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := t.ExecuteTemplate(w, "base", baseEnvelope{Banner: h.computeBanner(), Inner: data}); err != nil {
|
||||
if err := t.ExecuteTemplate(w, "base", env); err != nil {
|
||||
log.Printf("render %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
@@ -187,7 +224,12 @@ func (h *Handler) renderErr(w http.ResponseWriter, code int, msg string) {
|
||||
code, http.StatusText(code), msg)
|
||||
return
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "base", baseEnvelope{Banner: h.computeBanner(), Inner: data}); err != nil {
|
||||
env := baseEnvelope{Banner: h.computeBanner(), Inner: data}
|
||||
if h.twitch != nil {
|
||||
env.TwitchLive = h.twitch.IsLive()
|
||||
env.TwitchChannel = h.twitch.Channel()
|
||||
}
|
||||
if err := t.ExecuteTemplate(w, "base", env); err != nil {
|
||||
log.Printf("renderErr %d: %v", code, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,12 @@ func SecurityHeadersMiddleware(next http.Handler) http.Handler {
|
||||
if strings.HasPrefix(r.URL.Path, "/admin") {
|
||||
scriptSrc = "'self'"
|
||||
}
|
||||
w.Header().Set("Content-Security-Policy",
|
||||
"default-src 'self'; script-src "+scriptSrc+"; style-src 'self'; img-src 'self' data:; font-src 'self'; frame-ancestors 'none'")
|
||||
frameSrc := "'none'"
|
||||
if r.URL.Path == "/stream" {
|
||||
frameSrc = "https://player.twitch.tv"
|
||||
}
|
||||
csp := "default-src 'self'; script-src " + scriptSrc + "; style-src 'self'; img-src 'self' data:; font-src 'self'; frame-src " + frameSrc + "; frame-ancestors 'none'"
|
||||
w.Header().Set("Content-Security-Policy", csp)
|
||||
w.Header().Set("X-Frame-Options", "DENY")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -13,11 +14,19 @@ import (
|
||||
"ridgwaysystems.org/website/internal/blog"
|
||||
"ridgwaysystems.org/website/internal/changelog"
|
||||
"ridgwaysystems.org/website/internal/feed"
|
||||
"ridgwaysystems.org/website/internal/gitea"
|
||||
"ridgwaysystems.org/website/internal/mailer"
|
||||
"ridgwaysystems.org/website/internal/status"
|
||||
"ridgwaysystems.org/website/internal/uptime"
|
||||
)
|
||||
|
||||
// streamData is passed to the stream template.
|
||||
type streamData struct {
|
||||
Channel string
|
||||
Live bool
|
||||
Parent string // hostname for Twitch embed parent= param
|
||||
}
|
||||
|
||||
const postsPerPage = 10
|
||||
|
||||
// indexData is passed to the index template.
|
||||
@@ -158,6 +167,22 @@ func (h *Handler) Infrastructure(w http.ResponseWriter, r *http.Request) {
|
||||
h.render(w, "infrastructure", nil)
|
||||
}
|
||||
|
||||
func (h *Handler) Stream(w http.ResponseWriter, r *http.Request) {
|
||||
if h.twitch == nil {
|
||||
h.renderErr(w, http.StatusNotFound, "Stream page not configured.")
|
||||
return
|
||||
}
|
||||
host := r.Host
|
||||
if h, _, err := net.SplitHostPort(host); err == nil {
|
||||
host = h
|
||||
}
|
||||
h.render(w, "stream", streamData{
|
||||
Channel: h.twitch.Channel(),
|
||||
Live: h.twitch.IsLive(),
|
||||
Parent: host,
|
||||
})
|
||||
}
|
||||
|
||||
// serviceHistory bundles per-service uptime data for the status template.
|
||||
type serviceHistory struct {
|
||||
Blocks []uptime.DayBlock
|
||||
@@ -166,9 +191,10 @@ type serviceHistory struct {
|
||||
|
||||
// statusData is passed to the status template.
|
||||
type statusData struct {
|
||||
Page *status.Page
|
||||
LastChecked string
|
||||
History map[string]serviceHistory // keyed by service name
|
||||
Page *status.Page
|
||||
LastChecked string
|
||||
History map[string]serviceHistory // keyed by service name
|
||||
PlannedOutages []gitea.Issue
|
||||
}
|
||||
|
||||
func (h *Handler) Status(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -188,7 +214,17 @@ func (h *Handler) Status(w http.ResponseWriter, r *http.Request) {
|
||||
UptimePct: uptime.UptimePct(uptimePath, svc.Name),
|
||||
}
|
||||
}
|
||||
h.render(w, "status", statusData{Page: p, LastChecked: lastChecked, History: history})
|
||||
// Fetch planned outages from Gitea — best-effort, failure is non-fatal.
|
||||
var plannedOutages []gitea.Issue
|
||||
if h.gitea != nil && h.giteaOwner != "" && h.giteaRepo != "" {
|
||||
if issues, err := h.gitea.ListIssuesByLabel(h.giteaOwner, h.giteaRepo, h.giteaLabel); err == nil {
|
||||
plannedOutages = issues
|
||||
} else {
|
||||
log.Printf("status: gitea planned outages: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
h.render(w, "status", statusData{Page: p, LastChecked: lastChecked, History: history, PlannedOutages: plannedOutages})
|
||||
}
|
||||
|
||||
// changelogData is passed to the changelog template.
|
||||
|
||||
Reference in New Issue
Block a user