refactor(api,robots): move ai.go and robots.go to the standard router

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-07-08 20:16:52 -03:00
parent 493969ea72
commit edc785a244
4 changed files with 79 additions and 60 deletions

View File

@@ -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")
}

View File

@@ -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")
}

67
routes/robots.go Normal file
View File

@@ -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")
}

View File

@@ -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)