-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.go
64 lines (51 loc) · 1.25 KB
/
plugin.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
package tmpauth_traefik
import (
"context"
"log"
"net/http"
"sync"
"time"
"github.com/tmpim/tmpauth-go"
)
// Config the plugin configuration.
type Config = tmpauth.MiniConfig
// CreateConfig creates the default plugin configuration.
func CreateConfig() *Config {
return &Config{}
}
type AlwaysOnHandler struct {
handler *tmpauth.TmpauthStdlib
mu sync.Mutex
}
func (a *AlwaysOnHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.mu.Lock()
handler := a.handler
a.mu.Unlock()
if handler == nil {
http.Error(w, "tmpauth not initialized", http.StatusInternalServerError)
return
}
handler.ServeHTTP(w, r)
}
// New creates a new tmpauth plugin instance.
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
handler := &AlwaysOnHandler{}
go func() {
for {
if config.Host == "" {
log.Printf("tmpauth host is empty and must be set")
time.Sleep(10 * time.Second)
continue
}
ta, err := tmpauth.NewMini(*config, tmpauth.FromHTTPHandler(next))
if err != nil {
log.Printf("failed to initialize tmpauth mini for %q: %v", config.Host, err)
time.Sleep(10 * time.Second)
continue
}
handler.handler = ta.Stdlib()
break
}
}()
return handler, nil
}