impl home.
This commit is contained in:
@@ -13,8 +13,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.bel.blue/local/rproxy3/config"
|
"gitea.bel.blue/local/rproxy3/config"
|
||||||
@@ -198,9 +200,13 @@ func (s *Server) Pre(foo http.HandlerFunc) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if mapKey(r.Host) == "_" {
|
switch mapKey(r.Host) {
|
||||||
|
case "_":
|
||||||
s.List(w)
|
s.List(w)
|
||||||
return
|
return
|
||||||
|
case "home":
|
||||||
|
s.Home(w, r)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if auth, err := s.lookupAuth(mapKey(r.Host)); err != nil {
|
if auth, err := s.lookupAuth(mapKey(r.Host)); err != nil {
|
||||||
@@ -285,6 +291,50 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
s.Pre(s.Proxy)(w, r)
|
s.Pre(s.Proxy)(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) Home(w http.ResponseWriter, r *http.Request) {
|
||||||
|
keys := s.db.Keys(nsRouting)
|
||||||
|
routePrefixes := []string{}
|
||||||
|
for _, key := range keys {
|
||||||
|
u, _ := s.lookup(key)
|
||||||
|
if u != nil && u.String() != "" {
|
||||||
|
routePrefixes = append(routePrefixes, strings.Split(key, "/")[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices.Sort(routePrefixes)
|
||||||
|
t := strings.ReplaceAll(`
|
||||||
|
<DOCTYPE! html>
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
{{- $baseDomain := .baseDomain }}
|
||||||
|
{{- $scheme := .scheme }}
|
||||||
|
{{- range .routePrefixes }}
|
||||||
|
{{- $url := printf "%s://%s.%s" $scheme . $baseDomain }}
|
||||||
|
<a href="{{ $url }}">{{ . }}</a><br>
|
||||||
|
{{- end }}
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
|
`, "\t", " ")
|
||||||
|
tmpl, err := template.New("home").Parse(t)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
if err := tmpl.Execute(w, map[string]any{
|
||||||
|
"routePrefixes": routePrefixes,
|
||||||
|
"domain": r.Host,
|
||||||
|
"baseDomain": strings.Join(strings.Split(r.Host, ".")[1:], "."),
|
||||||
|
"scheme": r.URL.Scheme,
|
||||||
|
}); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) List(w http.ResponseWriter) {
|
func (s *Server) List(w http.ResponseWriter) {
|
||||||
keys := s.db.Keys(nsRouting)
|
keys := s.db.Keys(nsRouting)
|
||||||
hostURL := map[string]string{}
|
hostURL := map[string]string{}
|
||||||
|
|||||||
@@ -38,20 +38,39 @@ func TestServerStart(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mockServer() *Server {
|
func mockServer() *Server {
|
||||||
|
s := _mockServer()
|
||||||
portServer := httptest.NewServer(nil)
|
portServer := httptest.NewServer(nil)
|
||||||
port := strings.Split(portServer.URL, ":")[2]
|
s.addr = ":" + strings.Split(portServer.URL, ":")[2]
|
||||||
portServer.Close()
|
portServer.Close()
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func _mockServer() *Server {
|
||||||
s := &Server{
|
s := &Server{
|
||||||
db: storage.NewMap(),
|
db: storage.NewMap(),
|
||||||
addr: ":" + port,
|
|
||||||
limiter: rate.NewLimiter(rate.Limit(50), 50),
|
limiter: rate.NewLimiter(rate.Limit(50), 50),
|
||||||
}
|
}
|
||||||
if err := s.Routes(); err != nil {
|
if err := s.Routes(); err != nil {
|
||||||
panic(fmt.Sprintf("cannot initiate server routes; %v", err))
|
panic(fmt.Sprintf("cannot initiate server routes; %v", err))
|
||||||
}
|
}
|
||||||
|
if err := s.Route("test", config.Proxy{To: "http://icanhazip.com"}); err != nil {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServerHome(t *testing.T) {
|
||||||
|
s := _mockServer()
|
||||||
|
r := httptest.NewRequest(http.MethodGet, "https://home.int.bel.blue", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.Home(w, r)
|
||||||
|
if w.Code != http.StatusOK {
|
||||||
|
t.Fatalf("(%d) %s", w.Code, w.Body.Bytes())
|
||||||
|
}
|
||||||
|
t.Logf("%s", w.Body.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
func TestServerRoute(t *testing.T) {
|
func TestServerRoute(t *testing.T) {
|
||||||
server := mockServer()
|
server := mockServer()
|
||||||
p := config.Proxy{
|
p := config.Proxy{
|
||||||
|
|||||||
Reference in New Issue
Block a user