~evanj/cms

ref: ed17a2199584ba94d1ca44e9002b52c48b2c02fa cms/internal/c/redirect/redirect.go -rw-r--r-- 873 bytes
ed17a219Evan J Feat(context): Thread context throughout data layer. TODO: Update 1 year, 21 days 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,
	})
}