|
package config |
|
|
|
import ( |
|
"encoding/base64" |
|
"encoding/binary" |
|
"fmt" |
|
"net/http" |
|
"regexp" |
|
"strconv" |
|
"strings" |
|
|
|
"github.com/google/uuid" |
|
) |
|
|
|
|
|
|
|
|
|
func ShortURLMiddleware(next http.Handler) http.Handler { |
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
|
newPath := LongPath(req.URL.Path) |
|
if newPath == "" { |
|
next.ServeHTTP(w, req) |
|
return |
|
} |
|
|
|
cfg := FromContext(req.Context()) |
|
u := *req.URL |
|
u.Path = newPath |
|
|
|
|
|
http.Redirect(w, req, cfg.rawCallbackURL(u.String()).String(), http.StatusTemporaryRedirect) |
|
}) |
|
} |
|
|
|
|
|
const urlChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-." |
|
|
|
var ( |
|
alertURL = regexp.MustCompile(`^/alerts/\d+$`) |
|
serviceAlertsURL = regexp.MustCompile(`^/services/[a-f0-9-]+/alerts$`) |
|
urlEnc = base64.NewEncoding(urlChars).WithPadding(base64.NoPadding) |
|
) |
|
|
|
|
|
|
|
|
|
func ShortPath(longPath string) string { |
|
switch { |
|
case serviceAlertsURL.MatchString(longPath): |
|
idStr := strings.TrimPrefix(strings.TrimSuffix(longPath, "/alerts"), "/services/") |
|
id, err := uuid.Parse(idStr) |
|
if err != nil { |
|
return "" |
|
} |
|
return fmt.Sprintf("/s/%s", urlEnc.EncodeToString(id[:])) |
|
case alertURL.MatchString(longPath): |
|
i, err := strconv.Atoi(strings.TrimPrefix(longPath, "/alerts/")) |
|
if err != nil || i == 0 { |
|
return "" |
|
} |
|
buf := make([]byte, 8) |
|
n := binary.PutUvarint(buf, uint64(i)) |
|
return fmt.Sprintf("/a/%s", urlEnc.EncodeToString(buf[:n])) |
|
} |
|
return "" |
|
} |
|
|
|
|
|
|
|
func LongPath(shortPath string) string { |
|
switch { |
|
case strings.HasPrefix(shortPath, "/a/"): |
|
dec, err := urlEnc.DecodeString(strings.TrimPrefix(shortPath, "/a/")) |
|
if err != nil { |
|
return "" |
|
} |
|
id, _ := binary.Uvarint(dec) |
|
return fmt.Sprintf("/alerts/%d", id) |
|
case strings.HasPrefix(shortPath, "/s/"): |
|
dec, err := urlEnc.DecodeString(strings.TrimPrefix(shortPath, "/s/")) |
|
if err != nil { |
|
return "" |
|
} |
|
id, err := uuid.FromBytes(dec) |
|
if err != nil { |
|
return "" |
|
} |
|
return fmt.Sprintf("/services/%s/alerts", id.String()) |
|
} |
|
return "" |
|
} |
|
|