RoadSign/pkg/navi/struct.go

90 lines
2.6 KiB
Go
Raw Normal View History

2024-01-24 16:09:39 +00:00
package navi
import (
"fmt"
"net/url"
"strings"
2024-09-29 15:03:11 +00:00
"git.solsynth.dev/goatworks/roadsign/pkg/navi/transformers"
"git.solsynth.dev/goatworks/roadsign/pkg/warden"
2024-01-24 16:09:39 +00:00
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
type Region struct {
ID string `json:"id" toml:"id"`
Disabled bool `json:"disabled" toml:"disabled"`
Locations []Location `json:"locations" toml:"locations"`
Applications []warden.Application `json:"applications" toml:"applications"`
}
type Location struct {
ID string `json:"id" toml:"id"`
2024-02-14 07:22:24 +00:00
Hosts []string `json:"hosts" toml:"hosts"`
Paths []string `json:"paths" toml:"paths"`
2024-01-24 16:09:39 +00:00
Queries map[string]string `json:"queries" toml:"queries"`
Headers map[string][]string `json:"headers" toml:"headers"`
Destinations []Destination `json:"destinations" toml:"destinations"`
}
type DestinationType = int8
const (
DestinationHypertext = DestinationType(iota)
DestinationStaticFile
DestinationUnknown
)
type Destination struct {
ID string `json:"id" toml:"id"`
Uri string `json:"uri" toml:"uri"`
2024-01-30 16:57:47 +00:00
Helmet *HelmetConfig `json:"helmet" toml:"helmet"`
2024-01-24 16:09:39 +00:00
Transformers []transformers.TransformerConfig `json:"transformers" toml:"transformers"`
}
2024-01-26 05:07:42 +00:00
func (v *Destination) GetProtocol() string {
return strings.SplitN(v.Uri, "://", 2)[0]
}
2024-01-24 16:09:39 +00:00
func (v *Destination) GetType() DestinationType {
2024-01-26 05:07:42 +00:00
protocol := v.GetProtocol()
2024-01-24 16:09:39 +00:00
switch protocol {
case "http", "https":
return DestinationHypertext
case "file", "files":
return DestinationStaticFile
}
return DestinationUnknown
}
func (v *Destination) GetRawUri() (string, url.Values) {
uri := strings.SplitN(v.Uri, "://", 2)[1]
data := strings.SplitN(uri, "?", 2)
2024-10-03 12:59:18 +00:00
data = append(data, " ") // Make the data array at least have two elements
2024-01-26 05:07:42 +00:00
qs, _ := url.ParseQuery(data[1])
2024-01-24 16:09:39 +00:00
return data[0], qs
}
2024-09-29 15:51:05 +00:00
func (v *Destination) BuildUri(ctx *fiber.Ctx) string {
2024-01-24 16:09:39 +00:00
var queries []string
for k, v := range ctx.Queries() {
parsed, _ := url.QueryUnescape(v)
value := url.QueryEscape(parsed)
queries = append(queries, fmt.Sprintf("%s=%s", k, value))
}
path := string(ctx.Request().URI().Path())
hash := string(ctx.Request().URI().Hash())
2024-01-26 08:04:34 +00:00
protocol := v.GetProtocol()
2024-01-26 05:07:42 +00:00
uri, _ := v.GetRawUri()
2024-01-24 16:09:39 +00:00
2024-01-26 08:04:34 +00:00
return protocol + "://" + uri + path +
2024-01-24 16:09:39 +00:00
lo.Ternary(len(queries) > 0, "?"+strings.Join(queries, "&"), "") +
lo.Ternary(len(hash) > 0, "#"+hash, "")
}
2024-01-25 17:51:55 +00:00
func (v *Destination) MakeWebsocketUri(ctx *fiber.Ctx) string {
2024-09-29 15:51:05 +00:00
return strings.Replace(v.BuildUri(ctx), "http", "ws", 1)
2024-01-25 17:51:55 +00:00
}