-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
44 lines (35 loc) · 1.04 KB
/
http.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
const defaultCatchAllRedirectUrl = "https://bing.com"
func RedirectVisitor(w http.ResponseWriter, r *http.Request, url string) {
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func RedirectToCatchAllUrl(w http.ResponseWriter, r *http.Request) {
RedirectVisitor(w, r, CatchAllUrl())
}
func CatchAllUrl() string {
catchAllUrl := os.Getenv(EnvCatchAllRedirectUrl)
if catchAllUrl == "" {
return defaultCatchAllRedirectUrl
}
return catchAllUrl
}
func WriteJSON(w http.ResponseWriter, status int, v any) error {
w.Header().Set(HTTPHeaderContentType, ContentTypeApplicationJson)
w.WriteHeader(status)
return json.NewEncoder(w).Encode(v)
}
func WriteUnauthorized(w http.ResponseWriter) error {
return WriteJSON(w, http.StatusUnauthorized, NewJsonResponse(false, nil, unauthorized()))
}
func TelegramAPIMessageUrl(botID string) string {
return fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botID)
}
func BearerHeader(value string) string {
return "Bearer " + value
}