/
handler.go
47 lines (43 loc) · 1.04 KB
/
handler.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
package src
import (
"github.com/gorilla/mux"
"net/http"
)
func HandleBasic(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
vars := mux.Vars(r)
id := vars["id"]
var redirection Redirect
for _, red := range Cfg.Redirections {
if id == red.Id {
redirection = red
}
}
if redirection.Id == "" {
w.WriteHeader(http.StatusNotFound)
return
}
redirect := vars["redirect"]
loc := Location{redirection.Path.generateOrigin(), redirect}
http.Redirect(w, r, loc.generateUrl(), http.StatusSeeOther)
}
func HandleSlug(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
getSlug(w, r)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func getSlug(w http.ResponseWriter, r *http.Request) {
slug := mux.Vars(r)["redirect"]
redirect := Redirection{}
DB.First(&redirect, "slug = ?", slug)
if redirect.Slug != slug {
w.WriteHeader(http.StatusNotFound)
return
}
http.Redirect(w, r, redirect.Destination, http.StatusSeeOther)
}