2024-03-06 12:01:04 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2024-05-02 21:29:50 +01:00
|
|
|
"github.com/rramiachraf/dumb/utils"
|
2024-03-06 12:01:04 +01:00
|
|
|
"github.com/rramiachraf/dumb/views"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ContentTypeJSON = "application/json"
|
|
|
|
|
|
|
|
// TODO: move this to utils, so it can be used by other handlers.
|
2024-05-02 21:29:50 +01:00
|
|
|
func sendError(err error, status int, msg string, l *utils.Logger, w http.ResponseWriter) {
|
|
|
|
l.Error(err.Error())
|
2024-03-06 12:01:04 +01:00
|
|
|
w.WriteHeader(status)
|
|
|
|
if err := views.ErrorPage(status, msg).Render(context.Background(), w); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-06 12:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-02 21:29:50 +01:00
|
|
|
func instances(l *utils.Logger) http.HandlerFunc {
|
2024-03-06 12:01:04 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if instances, err := getCache[[]byte]("instances"); err == nil {
|
|
|
|
w.Header().Set("content-type", ContentTypeJSON)
|
|
|
|
_, err = w.Write(instances)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := sendRequest("https://raw.githubusercontent.com/rramiachraf/dumb/main/instances.json")
|
|
|
|
if err != nil {
|
|
|
|
sendError(err, http.StatusInternalServerError, "something went wrong", l, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
instances, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
sendError(err, http.StatusInternalServerError, "something went wrong", l, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("content-type", ContentTypeJSON)
|
|
|
|
if _, err = w.Write(instances); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-06 12:01:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = setCache("instances", instances); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-06 12:01:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|