6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-28 09:41:02 +00:00
prologic-saltyim/internal/handlers.go

48 lines
1.3 KiB
Go
Raw Normal View History

package internal
import (
"net/http"
"path/filepath"
"git.mills.io/prologic/useragent"
"github.com/julienschmidt/httprouter"
)
func (s *Server) NotFoundHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") == "application/json" {
w.Header().Set("Content-Type", "application/json")
http.Error(w, "Endpoint Not Found", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 Not Found"))
}
// ConfigHandler ...
func (s *Server) ConfigHandler() httprouter.Handle {
configDir := filepath.Join(s.config.Data, wellknownPath)
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Set("Accept-Encoding", "br, gzip, deflate")
http.ServeFile(w, r, filepath.Join(configDir, p.ByName("config")))
}
}
// IndexHandler ...
func (s *Server) IndexHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
ua := useragent.Parse(r.UserAgent())
if ua != nil && ua.Type == useragent.Browser {
http.Redirect(w, r, "/app/", http.StatusFound)
}
}
}
// InboxHandler ...
func (s *Server) InboxHandler() httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
// TODO: Implement filtering / blocking at the broker level
s.bus.ServeHTTP(w, r)
}
}