aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-04-01 16:05:51 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-04-01 16:10:01 +0200
commit5217357b4635fad76ca35e655517387f61ccbb2a (patch)
treed7c347473cd1f6986e32d8358232eb25df0c1a30 /main.go
parentf0c2bbf9a990b963a225d244974062ee727103d1 (diff)
downloadjnfilter-5217357b4635fad76ca35e655517387f61ccbb2a.tar.gz
jnfilter-5217357b4635fad76ca35e655517387f61ccbb2a.tar.bz2
jnfilter-5217357b4635fad76ca35e655517387f61ccbb2a.zip
feat: Add view to select series
Diffstat (limited to 'main.go')
-rw-r--r--main.go33
1 files changed, 26 insertions, 7 deletions
diff --git a/main.go b/main.go
index 22d1489..7a8d2a6 100644
--- a/main.go
+++ b/main.go
@@ -2,10 +2,12 @@ package main
import (
"context"
+ "embed"
"errors"
"flag"
"fmt"
"io"
+ "log/slog"
"net/http"
"os"
"regexp"
@@ -20,15 +22,17 @@ import (
)
const (
- FeedUrl = "https://api.jovemnerd.com.br/feed-nerdcast/"
+ feedUrl = "https://api.jovemnerd.com.br/feed-nerdcast/"
)
type (
- ErrorRequestHandler func(w http.ResponseWriter, r *http.Request) error
+ errorRequestHandler func(w http.ResponseWriter, r *http.Request) error
)
var (
- SerieRegex = regexp.MustCompile(`(?P<serie>.+) (?P<number>[0-9abc]+) \- (?P<title>.+)`)
+ //go:embed static/*
+ assets embed.FS
+ serieRegex = regexp.MustCompile(`(?P<serie>.+) (?P<number>[0-9abc]+) \- (?P<title>.+)`)
)
var (
@@ -46,7 +50,6 @@ var (
"cloud": "Nerd na Cloud [0-9]+",
"contar": "Vou (T|t)e Contar [0-9]+",
"parceiro": "Papo de Parceiro [0-9]+",
- "cash": "NerdCash [0-9]+",
}
feedRequest = promauto.NewHistogramVec(prometheus.HistogramOpts{
@@ -105,7 +108,7 @@ func fetchXML(_ context.Context) ([]byte, error) {
feedRequest.WithLabelValues(code).Observe(since)
}()
- res, err := http.Get(FeedUrl)
+ res, err := http.Get(feedUrl)
if err != nil {
return nil, err
}
@@ -157,9 +160,10 @@ func filterBySeries(series []string, xml []byte, temper bool) ([]byte, error) {
return doc.WriteToBytes()
}
-func handleError(next ErrorRequestHandler) http.HandlerFunc {
+func handleError(next errorRequestHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := next(w, r); err != nil {
+ slog.ErrorContext(r.Context(), "Error", "error", err.Error())
w.WriteHeader(http.StatusInternalServerError)
}
}
@@ -217,6 +221,20 @@ func titles(w http.ResponseWriter, r *http.Request) error {
return nil
}
+func view(w http.ResponseWriter, r *http.Request) error {
+ data, err := assets.ReadFile("static/index.html")
+ if err != nil {
+ return err
+ }
+
+ _, err = w.Write(data)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
+
func podcast(w http.ResponseWriter, r *http.Request) error {
xml, err := fetchXML(r.Context())
if err != nil {
@@ -253,7 +271,7 @@ func genSeries() error {
els := doc.FindElements("//channel/item")
for _, e := range els {
txt := e.FindElement("title").Text()
- res := SerieRegex.FindStringSubmatch(txt)
+ res := serieRegex.FindStringSubmatch(txt)
if len(res) > 1 {
unique[res[1]] = nil
}
@@ -284,6 +302,7 @@ func main() {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/titles", wrap(handleError(titles)))
+ mux.HandleFunc("/view", wrap(handleError(view)))
mux.HandleFunc("/", wrap(observe(handleError(podcast))))
server := http.Server{