Deliver twitter.com/azimjon_p/status/1525795706388226049

This commit is contained in:
Azimjon Pulatov 2022-11-01 01:04:13 +01:00
parent 50139be273
commit 2e906ac9c1
2 changed files with 29 additions and 15 deletions

View file

@ -28,9 +28,10 @@ func DefaultFrameType(frames []string) FrameType {
}
var FrameMap = map[string]FrameType{
"forrest": Forrest,
"parrot": Parrot,
"clock": Clock,
"nyan": Nyan,
"rick": Rick,
"forrest": Forrest,
"parrot": Parrot,
"clock": Clock,
"nyan": Nyan,
"rick": Rick,
"can-you-hear-me": Rick,
}

33
main.go
View file

@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"net/http"
"strings"
"time"
"github.com/hugomd/ascii-live/frames"
@ -17,6 +18,10 @@ var NotFoundMessage = map[string]string{
"error": "Frames not found. Navigate to /list for list of frames. Navigate to https://github.com/hugomd/ascii-live to submit new frames.",
}
var NotCurledMessage = map[string]string{
"error": "You almost ruined a good surprise. Come on, curl it in terminal.",
}
var availableFrames []string
func init() {
@ -25,24 +30,26 @@ func init() {
}
}
func listHandler(w http.ResponseWriter, r *http.Request) {
func writeJson(w http.ResponseWriter, r *http.Request, res interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(map[string][]string{"frames": availableFrames})
data, err := json.Marshal(res)
if err != nil {
return
}
w.WriteHeader(http.StatusOK)
w.WriteHeader(status)
fmt.Fprint(w, string(data))
}
func listHandler(w http.ResponseWriter, r *http.Request) {
writeJson(w, r, map[string][]string{"frames": availableFrames}, http.StatusOK)
}
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(NotFoundMessage)
if err != nil {
return
}
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, string(data))
writeJson(w, r, NotFoundMessage, http.StatusNotFound)
}
func notCurledHandler(w http.ResponseWriter, r *http.Request) {
writeJson(w, r, NotCurledMessage, http.StatusExpectationFailed)
}
func handler(w http.ResponseWriter, r *http.Request) {
@ -59,6 +66,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
return
}
userAgent := r.Header.Get("User-Agent")
if !strings.Contains(userAgent, "curl") {
notCurledHandler(w, r)
return
}
w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK)