redirect.go 572 Bytes
Newer Older
1 2 3
package corehttp

import (
4
	"net"
5 6
	"net/http"

tavit ohanian's avatar
tavit ohanian committed
7
	core "gitlab.dms3.io/dms3/go-dms3/core"
8 9 10 11
)

func RedirectOption(path string, redirect string) ServeOption {
	handler := &redirectHandler{redirect}
tavit ohanian's avatar
tavit ohanian committed
12
	return func(n *core.Dms3Node, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
13 14 15 16 17
		if len(path) > 0 {
			mux.Handle("/"+path+"/", handler)
		} else {
			mux.Handle("/", handler)
		}
18
		return mux, nil
19 20 21 22 23 24 25 26 27 28
	}
}

type redirectHandler struct {
	path string
}

func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, i.path, 302)
}