Merge pull request #7 from azimjohn/master

Prevent requests made without curl
This commit is contained in:
Hugo 2022-11-01 11:14:22 +11:00 committed by GitHub
commit 9337f80c4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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{ var FrameMap = map[string]FrameType{
"forrest": Forrest, "forrest": Forrest,
"parrot": Parrot, "parrot": Parrot,
"clock": Clock, "clock": Clock,
"nyan": Nyan, "nyan": Nyan,
"rick": Rick, "rick": Rick,
"can-you-hear-me": Rick,
} }

33
main.go
View file

@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/hugomd/ascii-live/frames" "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.", "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 var availableFrames []string
func init() { 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") w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(map[string][]string{"frames": availableFrames}) data, err := json.Marshal(res)
if err != nil { if err != nil {
return return
} }
w.WriteHeader(http.StatusOK) w.WriteHeader(status)
fmt.Fprint(w, string(data)) 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) { func notFoundHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") writeJson(w, r, NotFoundMessage, http.StatusNotFound)
data, err := json.Marshal(NotFoundMessage) }
if err != nil {
return func notCurledHandler(w http.ResponseWriter, r *http.Request) {
} writeJson(w, r, NotCurledMessage, http.StatusExpectationFailed)
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, string(data))
} }
func handler(w http.ResponseWriter, r *http.Request) { func handler(w http.ResponseWriter, r *http.Request) {
@ -59,6 +66,12 @@ func handler(w http.ResponseWriter, r *http.Request) {
return return
} }
userAgent := r.Header.Get("User-Agent")
if !strings.Contains(userAgent, "curl") {
notCurledHandler(w, r)
return
}
w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Transfer-Encoding", "chunked")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)