~evanj/cms

ref: 7c487cb4cca6290eae5f854300d6e9c78f81ea25 cms/internal/c/redirect/redirect.go -rw-r--r-- 873 bytes
7c487cb4Evan J Feat(dynamic): Dynamic content pages have been added (rather than 9 months ago
                                                                                
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
package redirect

import (
	"context"
	"log"
	"net/http"

	"git.sr.ht/~evanj/cms/internal/c"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/v"
)

var redirectHTML = v.MustParse("html/redirect.html")

type Redirect struct {
	*c.Controller
	log *log.Logger
}

type dber interface {
	UserGet(ctx context.Context, username, password string) (user.User, error)
	UserGetFromToken(ctx context.Context, token string) (user.User, error)
}

func New(c *c.Controller, log *log.Logger, db dber) *Redirect {
	return &Redirect{
		c,
		log,
	}
}

func (rd *Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	to := r.FormValue("url")
	if to == "" {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte("no URL found to redirect to"))
		return
	}

	w.WriteHeader(http.StatusOK)
	rd.HTML(w, r, redirectHTML, map[string]interface{}{
		"URL": to,
	})
}