From edc785a2448a5ef9bb40f9bd20e398ad4cd24126 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Mon, 8 Jul 2024 20:16:52 -0300 Subject: [PATCH] refactor(api,robots): move ai.go and robots.go to the standard router --- api/ai.go | 30 ---------------------- api/robots.go | 30 ---------------------- routes/robots.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ routes/router.go | 12 +++++++++ 4 files changed, 79 insertions(+), 60 deletions(-) delete mode 100644 api/ai.go delete mode 100644 api/robots.go create mode 100644 routes/robots.go diff --git a/api/ai.go b/api/ai.go deleted file mode 100644 index 97c1235..0000000 --- a/api/ai.go +++ /dev/null @@ -1,30 +0,0 @@ -package api - -import ( - "io" - "net/http" - - "extrovert/internals" -) - -func AiTxt(w http.ResponseWriter, r *http.Request) { - error := internals.HttpErrorHelper(w) - - aiList, err := http.Get("https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/ai.txt") - if error("Error trying to fetch ai block list", err, http.StatusInternalServerError) { - return - } - - bytes, err := io.ReadAll(aiList.Body) - if error("Error trying to read ai block list", err, http.StatusInternalServerError) { - return - } - _, err = w.Write(bytes) - if error("Error trying to write ai block list", err, http.StatusInternalServerError) { - return - } - - w.Header().Add("Cache-Control", "max-age=604800, stale-while-revalidate=86400, stale-if-error=86400") - w.Header().Add("CDN-Cache-Control", "max-age=604800") - w.Header().Add("Content-Type", "text/plain") -} diff --git a/api/robots.go b/api/robots.go deleted file mode 100644 index d066485..0000000 --- a/api/robots.go +++ /dev/null @@ -1,30 +0,0 @@ -package api - -import ( - "io" - "net/http" - - "extrovert/internals" -) - -func RobotsTxt(w http.ResponseWriter, r *http.Request) { - error := internals.HttpErrorHelper(w) - - aiList, err := http.Get("https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/robots.txt") - if error("Error trying to fetch ai block list", err, http.StatusInternalServerError) { - return - } - - bytes, err := io.ReadAll(aiList.Body) - if error("Error trying to read ai block list", err, http.StatusInternalServerError) { - return - } - _, err = w.Write(bytes) - if error("Error trying to write ai block list", err, http.StatusInternalServerError) { - return - } - - w.Header().Add("Cache-Control", "max-age=604800, stale-while-revalidate=86400, stale-if-error=86400") - w.Header().Add("CDN-Cache-Control", "max-age=604800") - w.Header().Add("Content-Type", "text/plain") -} diff --git a/routes/robots.go b/routes/robots.go new file mode 100644 index 0000000..29cde3b --- /dev/null +++ b/routes/robots.go @@ -0,0 +1,67 @@ +package routes + +import ( + "context" + "io" + "net/http" + + "github.com/a-h/templ" + + "extrovert/internals" +) + +func AiTxt() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + aiList, err := http.Get("https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/ai.txt") + if err != nil { + return err + } + + bytes, err := io.ReadAll(aiList.Body) + if err != nil { + return err + } + _, err = io.WriteString(w, string(bytes)) + return err + }) +} + +func AiTxtHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Cache-Control", "max-age=604800, stale-while-revalidate=86400, stale-if-error=86400") + w.Header().Add("CDN-Cache-Control", "max-age=604800") + + error := internals.HttpErrorHelper(w) + err := AiTxt().Render(context.Background(), w) + if error("Error trying to create ai block list", err, http.StatusInternalServerError) { + return + } + w.Header().Add("Content-Type", "text/plain") +} + +func RobotsTxt() templ.Component { + return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + aiList, err := http.Get("https://raw.githubusercontent.com/ai-robots-txt/ai.robots.txt/main/robots.txt") + if err != nil { + return err + } + + bytes, err := io.ReadAll(aiList.Body) + if err != nil { + return err + } + _, err = io.WriteString(w, string(bytes)) + return err + }) +} + +func RobotsTxtHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Cache-Control", "max-age=604800, stale-while-revalidate=86400, stale-if-error=86400") + w.Header().Add("CDN-Cache-Control", "max-age=604800") + + error := internals.HttpErrorHelper(w) + err := RobotsTxt().Render(context.Background(), w) + if error("Error trying to create robots block list", err, http.StatusInternalServerError) { + return + } + w.Header().Add("Content-Type", "text/plain") +} diff --git a/routes/router.go b/routes/router.go index 646e317..890cc9d 100644 --- a/routes/router.go +++ b/routes/router.go @@ -19,6 +19,18 @@ var ROUTES = []Route{ Page: TwitterLogin(), Handler: TwitterLoginHandler, }, + { + Pattern: "/robots.txt", + Static: true, + Page: RobotsTxt(), + Handler: RobotsTxtHandler, + }, + { + Pattern: "/ai.txt", + Static: true, + Page: AiTxt(), + Handler: AiTxtHandler, + }, } type RouteHandler = func(http.ResponseWriter, *http.Request)