-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
48 lines (41 loc) · 1.02 KB
/
main.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
package fibervhost
import (
"github.com/gofiber/fiber/v2"
)
type Vhost struct {
Host string
Hostname string
HostnameRegexpString string
}
func New(config ...Config) fiber.Handler {
cfg := configDefault(config...)
return func(c *fiber.Ctx) error {
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
if cfg.HostnameRegexpString != "" {
re, _ := compile_regexp(cfg.HostnameRegexpString)
if match(re, c.Hostname()) {
vh := Vhost{
Host: c.Hostname(),
Hostname: cfg.Hostname,
HostnameRegexpString: cfg.HostnameRegexpString,
}
c.Locals("vhost", vh)
}
} else if re, _ := string_to_regexp(cfg.Hostname); match(re, c.Hostname()) {
vh := Vhost{
Host: c.Hostname(),
Hostname: cfg.Hostname,
HostnameRegexpString: cfg.HostnameRegexpString,
}
c.Locals("vhost", vh)
} else {
return c.Next()
}
return cfg.Handler(c)
}
}
func ToVhostStruct(val interface{}) Vhost {
return val.(Vhost)
}