-
Notifications
You must be signed in to change notification settings - Fork 4
/
themepark.go
127 lines (105 loc) · 2.78 KB
/
themepark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Package traefik_themepark a plugin to rewrite response body.
package traefik_themepark
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/packruler/rewrite-body/handler"
)
// Config holds the plugin configuration.
type Config struct {
Theme string `json:"theme,omitempty"`
App string `json:"app,omitempty"`
BaseURL string `json:"baseUrl,omitempty"`
LogLevel int8 `json:"logLevel,omitempty"`
Addons []string `json:"addons,omitempty"`
Target string `json:"target,omitempty"`
}
// CreateConfig creates and initializes the plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
// New creates and returns a new rewrite body plugin instance.
func New(context context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
config.setDefaults()
handlerConfig := &handler.Config{
Rewrites: []handler.Rewrite{
{
Regex: config.Target,
Replacement: config.getReplacementString(),
},
},
LogLevel: config.LogLevel,
}
return handler.New(context, next, handlerConfig, name)
}
const replFormat string = "<link " +
"rel=\"stylesheet\" " +
"type=\"text/css\" " +
"href=\"%s/css/base/%s/%s.css\">"
const addonFormatLegacy string = "<link " +
"rel=\"stylesheet\" " +
"type=\"text/css\" " +
"href=\"%s/css/addons/%s/%s-%s/%s-%s.css\">"
const addonFormat string = "<link " +
"rel=\"stylesheet\" " +
"type=\"text/css\" " +
"href=\"%s/css/addons/%s/%s/%s.css\">"
func (config *Config) getReplacementString() string {
var stringBuilder strings.Builder
stringBuilder.WriteString(fmt.Sprintf(replFormat, config.BaseURL, config.App, config.Theme))
for _, addon := range config.Addons {
if strings.HasPrefix(addon, config.App) {
stringBuilder.WriteString(fmt.Sprintf(addonFormat, config.BaseURL, config.App, addon, addon))
} else {
stringBuilder.WriteString(
fmt.Sprintf(
addonFormatLegacy,
config.BaseURL,
config.App,
config.App,
addon,
config.App,
addon,
),
)
}
}
stringBuilder.WriteString(config.Target)
return stringBuilder.String()
}
func (config *Config) setDefaults() {
if config.BaseURL == "" {
config.BaseURL = "https://theme-park.dev"
}
if config.Theme == "" || config.Theme == "base" {
config.Theme = config.App + "-base"
}
if config.Target == "" {
config.Target = config.getRegexTarget()
}
}
func getBodyBasedAppsRegex() string {
bodyBasedAppsList := []string{
"vuetorrent",
"qbittorrent",
"emby",
"jellyfin",
"radarr",
"prowlarr",
"sonarr",
"readarr",
"lidarr",
"whisparr",
}
return "(?i)" + strings.Join(bodyBasedAppsList, "|")
}
func (config *Config) getRegexTarget() string {
match, _ := regexp.MatchString(getBodyBasedAppsRegex(), config.App)
if match {
return "</body>"
}
return "</head>"
}