Compare commits

..

No commits in common. 'drew/ui' and 'main' have entirely different histories.

@ -1,52 +0,0 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
silent = false
time = false
[misc]
clean_on_exit = false
[proxy]
app_port = 0
enabled = false
proxy_port = 0
[screen]
clear_on_rebuild = false
keep_scroll = true

1
.gitignore vendored

@ -21,4 +21,3 @@
# Go workspace file
go.work
tmp/

@ -1,3 +1,3 @@
module git.runcible.io/androiddrew/learning_sse
go 1.24.1
go 1.21.0

@ -3,39 +3,14 @@ package main
import (
"context"
"fmt"
"html/template"
//"io"
"io"
"log/slog"
"net/http"
//"os"
"os"
"strings"
"time"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
files := []string{
"./ui/html/base.go.tmpl",
"./ui/html/index.go.tmpl",
}
ts, err := template.ParseFiles(files...)
if err != nil {
slog.Error("error parsing files", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
err = ts.ExecuteTemplate(w, "base", nil)
if err != nil {
slog.Error("error executing template", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func SSEHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
@ -48,31 +23,26 @@ func SSEHandler(w http.ResponseWriter, r *http.Request) {
}
func routes() http.Handler {
fileServer := http.FileServer(http.Dir("./ui/static/"))
// TODO add static server and an HTML index with `new EventSource` JS that writes the data message contents into an element on the page
mux := http.NewServeMux()
mux.Handle("GET /static/", http.StripPrefix("/static", fileServer))
mux.HandleFunc("GET /sse", SSEHandler)
mux.HandleFunc("GET /{$}", IndexHandler)
mux.HandleFunc("/sse", SSEHandler)
return mux
}
// TODO add args slice and process flags
// TODO add io.Writer and pass that to the logger
func run(ctx context.Context) error {
func run(ctx context.Context, w io.Writer, args []string) error {
addr := fmt.Sprintf("%s:%d", "0.0.0.0", 8888)
srv := &http.Server{
Addr: addr,
Handler: routes(),
}
slog.Info("Listing on: http://0.0.0.0:8888/")
slog.Info("Listing on: http://0.0.0.0:8888")
return srv.ListenAndServe()
}
func main() {
ctx := context.Background()
// TODO add os.Args
if err := run(ctx); err != nil {
if err := run(ctx, os.Stdout, os.Args); err != nil {
slog.Error("an error occurred", "error", err)
}
}

@ -1,21 +0,0 @@
{{ define "base" -}}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{template "title" .}}</title>
<link rel="stylesheet" href="/static/css/main.css"/>
<link rel="short icon" href="/static/img/favicon.ico" type="image/x-icon"/>
</head>
<body>
<header>
<h1>Server Sent Events Example</h1>
</header>
<main>
{{template "main" .}}
</main>
</body>
<footer>Powered By <a href='https://golang.org/'>Go</a> in {{ .CurrentYear }}</footer>
<script src="/static/js/main.js" type="text/javascript"></script>
</html>
{{ end }}

@ -1,5 +0,0 @@
{{define "title"}}SSE Example{{end}}
{{define "main"}}
<p id="stuff"></p>
{{end}}

@ -1,4 +0,0 @@
body {
background-color: slategrey;
text-align: center;
}

@ -1,8 +0,0 @@
const evtSource = new EventSource("/sse");
let content = document.getElementById("stuff")
evtSource.onmessage = (event) => {
content.innerHTML += event.data + ' ';
};
Loading…
Cancel
Save