From e7a075988cdd7459210f4a0886af9e10d8437028 Mon Sep 17 00:00:00 2001 From: long Date: Sun, 23 Feb 2025 16:05:50 -0600 Subject: [PATCH] got web server and views working --- .gitignore | 1 + components.templ | 284 +++++++++++++++++++++++++++++++++++++++++++++ event/query.sql.go | 20 +--- main.go | 14 +-- server.go | 60 ++++++++++ sql/query.sql | 2 +- 6 files changed, 358 insertions(+), 23 deletions(-) create mode 100644 components.templ create mode 100644 server.go diff --git a/.gitignore b/.gitignore index e9f8ba3..36ea337 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ testdata/ db.sqlite* test-db.sqlite* +*_templ.go diff --git a/components.templ b/components.templ new file mode 100644 index 0000000..202b88d --- /dev/null +++ b/components.templ @@ -0,0 +1,284 @@ +package main + +import "mmaschedule-go/event" +import "strconv" + +templ TemplEventPage(e event.Event, upcoming []event.ListUpcomingEventsRow) { + + + + + MMA Schedule + + + //@import "tailwindcss"; + + + +
+
+ +
+ + +
+
+
+
+
+ +
+
+

+ { e.Name } +

+
+
+ @TemplIconCalendar("mr-2") + @TemplTimestamp(e.Date) +
+ if len(e.Location) > 0 { +

+ @TemplIconLocation("mr-1") + { e.Location } +

+ } +
+
+
+
+
+
+ for _, f := range e.UnmarshalFights() { + @TemplFightRow(f) + } +
+
+
+
+ + +
+
+
+ @TemplJSTimestamp() + + +} + +templ TemplFightRow(f *event.Fight) { +
+
+

+ { f.Weight } +

+
+
+ @TemplFighterCol(f.FighterA, false) +
+
+

VS

+
+
+ @TemplFighterCol(f.FighterB, true) +
+
+} + +templ TemplFighterCol(f *event.Fighter, right bool) { +
+ 0 { + target="_blank" + href={ templ.URL(f.Link) } + class="no-underline hover:underline" + } + > +
+ +
+

+ { f.Name } +

+ if len(f.Country) > 0 { +

+ { f.Country } +

+ } +
+
+
+
+} + +templ TemplTimestamp(timestamp int64) { + + //{{ date|date:'D M j, g:i A T' }} + +} + +templ TemplIconLocation(class string) { + + + +} + +templ TemplIconCalendar(class string) { + + + +} + +templ TemplJSTimestamp() { + +} diff --git a/event/query.sql.go b/event/query.sql.go index abe49ec..2c75c34 100644 --- a/event/query.sql.go +++ b/event/query.sql.go @@ -74,7 +74,7 @@ func (q *Queries) GetTapology(ctx context.Context, name string) (Tapology, error const getUpcomingEvent = `-- name: GetUpcomingEvent :one SELECT - url, slug, name, location, organization, image, date, fights, history + slug FROM event WHERE @@ -85,21 +85,11 @@ LIMIT 1 ` -func (q *Queries) GetUpcomingEvent(ctx context.Context, date int64) (Event, error) { +func (q *Queries) GetUpcomingEvent(ctx context.Context, date int64) (string, error) { row := q.db.QueryRowContext(ctx, getUpcomingEvent, date) - var i Event - err := row.Scan( - &i.Url, - &i.Slug, - &i.Name, - &i.Location, - &i.Organization, - &i.Image, - &i.Date, - &i.Fights, - &i.History, - ) - return i, err + var slug string + err := row.Scan(&slug) + return slug, err } const listEvents = `-- name: ListEvents :many diff --git a/main.go b/main.go index bc2fd59..2900952 100644 --- a/main.go +++ b/main.go @@ -16,15 +16,15 @@ import ( var migrations embed.FS func main() { - q, err := InitDb("db.sqlite") + queries, err := InitDb("db.sqlite") if err != nil { fmt.Println("Error initializing database:", err) - } else { - client := NewScraperClient() - err := UpdateAllTapology(q, client) - if err != nil { - fmt.Println("Error updating all tapology:", err) - } + return + } + + err = RunServer("0.0.0.0:3001", queries) + if err != nil { + fmt.Println("Error starting web server:", err) } } diff --git a/server.go b/server.go new file mode 100644 index 0000000..8355a28 --- /dev/null +++ b/server.go @@ -0,0 +1,60 @@ +package main + +import ( + "mmaschedule-go/event" + "net/http" + "time" +) + +func RunServer(addr string, queries *event.Queries) error { + mux := http.NewServeMux() + state := ServerState{queries: queries} + mux.HandleFunc("GET /", state.HandleFunc(RouteIndex)) + mux.HandleFunc("GET /events/{slug}/", state.HandleFunc(RouteEvent)) + err := http.ListenAndServe(addr, mux) + return err +} + +func RouteIndex(w http.ResponseWriter, r *http.Request, q *event.Queries) { + slug, err := q.GetUpcomingEvent(r.Context(), time.Now().Unix()) + if err != nil { + http.NotFound(w, r) + } else { + http.Redirect(w, r, "/events/"+slug+"/", http.StatusSeeOther) + } +} + +func RouteEvent(w http.ResponseWriter, r *http.Request, q *event.Queries) { + slug := r.PathValue("slug") + + if len(slug) == 0 { + http.NotFound(w, r) + return + } + + event, err := q.GetEvent(r.Context(), slug) + if err != nil { + http.NotFound(w, r) + return + } + + upcoming, err := q.ListUpcomingEvents(r.Context(), time.Now().Unix()) + if err != nil { + http.NotFound(w, r) + return + } + + TemplEventPage(event, upcoming).Render(r.Context(), w) +} + +type StateHandler func(w http.ResponseWriter, r *http.Request, q *event.Queries) + +type ServerState struct { + queries *event.Queries +} + +func (state *ServerState) HandleFunc(handler StateHandler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + handler(w, r, state.queries) + } +} diff --git a/sql/query.sql b/sql/query.sql index 4182272..9828861 100644 --- a/sql/query.sql +++ b/sql/query.sql @@ -16,7 +16,7 @@ FROM -- name: GetUpcomingEvent :one SELECT - * + slug FROM event WHERE