This commit is contained in:
parent
b5f063381e
commit
fd124f2a5d
3 changed files with 106 additions and 99 deletions
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module github.com/hugomd/ascii-live
|
module forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live
|
||||||
|
|
||||||
go 1.24.1
|
go 1.24.1
|
||||||
|
|
||||||
|
|
102
handlers/handlers.go
Normal file
102
handlers/handlers.go
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"forgejo.edf-bootstrap.cx.fg1.ffm.osc.live/Franz.Germann/ascii-live/frames"
|
||||||
|
"github.com/golang/glog"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
|
)
|
||||||
|
|
||||||
|
var NotFoundMessage = map[string]string{
|
||||||
|
"error": "Frames not found. Navigate to /ascii/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() {
|
||||||
|
for k := range frames.FrameMap {
|
||||||
|
availableFrames = append(availableFrames, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeJson(w http.ResponseWriter, r *http.Request, res interface{}, status int) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
data, err := json.Marshal(res)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
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) {
|
||||||
|
ctx := r.Context()
|
||||||
|
flusher := w.(http.Flusher)
|
||||||
|
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
frameSource := vars["frameSource"]
|
||||||
|
glog.Infof("Frame source %s", frameSource)
|
||||||
|
|
||||||
|
frames, ok := frames.FrameMap[frameSource]
|
||||||
|
if !ok {
|
||||||
|
NotFoundHandler(w, r)
|
||||||
|
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)
|
||||||
|
|
||||||
|
i := 0
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
// Handle client disconnects
|
||||||
|
case <-ctx.Done():
|
||||||
|
glog.Infof("Client stopped listening")
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
if i >= frames.GetLength() {
|
||||||
|
i = 0
|
||||||
|
}
|
||||||
|
// Artificially wait between reponses.
|
||||||
|
time.Sleep(frames.GetSleep())
|
||||||
|
|
||||||
|
// Clear screen
|
||||||
|
clearScreen := "\033[2J\033[H"
|
||||||
|
newLine := "\n"
|
||||||
|
|
||||||
|
// Write frames
|
||||||
|
fmt.Fprint(w, clearScreen+frames.GetFrame(i)+newLine)
|
||||||
|
i++
|
||||||
|
|
||||||
|
// Send some data.
|
||||||
|
flusher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
101
main.go
101
main.go
|
@ -1,108 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hugomd/ascii-live/frames"
|
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
var NotFoundMessage = map[string]string{
|
|
||||||
"error": "Frames not found. Navigate to /ascii/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() {
|
|
||||||
for k := range frames.FrameMap {
|
|
||||||
availableFrames = append(availableFrames, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeJson(w http.ResponseWriter, r *http.Request, res interface{}, status int) {
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
data, err := json.Marshal(res)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
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) {
|
|
||||||
ctx := r.Context()
|
|
||||||
flusher := w.(http.Flusher)
|
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
frameSource := vars["frameSource"]
|
|
||||||
glog.Infof("Frame source %s", frameSource)
|
|
||||||
|
|
||||||
frames, ok := frames.FrameMap[frameSource]
|
|
||||||
if !ok {
|
|
||||||
notFoundHandler(w, r)
|
|
||||||
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)
|
|
||||||
|
|
||||||
i := 0
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
// Handle client disconnects
|
|
||||||
case <-ctx.Done():
|
|
||||||
glog.Infof("Client stopped listening")
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
if i >= frames.GetLength() {
|
|
||||||
i = 0
|
|
||||||
}
|
|
||||||
// Artificially wait between reponses.
|
|
||||||
time.Sleep(frames.GetSleep())
|
|
||||||
|
|
||||||
// Clear screen
|
|
||||||
clearScreen := "\033[2J\033[H"
|
|
||||||
newLine := "\n"
|
|
||||||
|
|
||||||
// Write frames
|
|
||||||
fmt.Fprint(w, clearScreen+frames.GetFrame(i)+newLine)
|
|
||||||
i++
|
|
||||||
|
|
||||||
// Send some data.
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server.
|
// Server.
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
@ -110,9 +15,9 @@ func main() {
|
||||||
flag.Set("logtostderr", "true")
|
flag.Set("logtostderr", "true")
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
r.HandleFunc("/ascii-live/list", listHandler).Methods("GET")
|
r.HandleFunc("/ascii-live/list", ListHandler).Methods("GET")
|
||||||
r.HandleFunc("/ascii-live/{frameSource}", handler).Methods("GET")
|
r.HandleFunc("/ascii-live/{frameSource}", Handler).Methods("GET")
|
||||||
r.NotFoundHandler = http.HandlerFunc(notFoundHandler)
|
r.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Handler: r,
|
Handler: r,
|
||||||
|
|
Loading…
Reference in a new issue