Skip to content

Commit

Permalink
feat: add xml responses for hyperview
Browse files Browse the repository at this point in the history
  • Loading branch information
believer committed Nov 27, 2023
1 parent 6ba0b56 commit 4513bcf
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 2 deletions.
9 changes: 9 additions & 0 deletions app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"believer/movies/db"
"believer/movies/router"
"os"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/template/html/v2"
"github.com/joho/godotenv"
)

Expand All @@ -24,8 +26,15 @@ func SetupAndRunApp() error {
// Close database connection when the app exits
defer db.CloseConnection()

// Setup templates
engine := html.New("./views", ".xml")

// Add custom functions to the template engine
engine.AddFunc("StringsJoin", strings.Join)

// Setup the app
app := fiber.New(fiber.Config{
Views: engine,
DisableStartupMessage: true,
PassLocalsToViews: true,
})
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ require (
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/a-h/templ v0.2.476
github.com/gofiber/template/html/v2 v2.0.5
github.com/qustavo/dotsql v1.1.0
golang.org/x/text v0.14.0
)

require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/gofiber/template v1.8.2 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
golang.org/x/net v0.18.0 // indirect
)
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs=
github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts=
github.com/gofiber/template v1.8.2 h1:PIv9s/7Uq6m+Fm2MDNd20pAFFKt5wWs7ZBd8iV9pWwk=
github.com/gofiber/template v1.8.2/go.mod h1:bs/2n0pSNPOkRa5VJ8zTIvedcI/lEYxzV3+YPXdBvq8=
github.com/gofiber/template/html/v2 v2.0.5 h1:BKLJ6Qr940NjntbGmpO3zVa4nFNGDCi/IfUiDB9OC20=
github.com/gofiber/template/html/v2 v2.0.5/go.mod h1:RCF14eLeQDCSUPp0IGc2wbSSDv6yt+V54XB/+Unz+LM=
github.com/gofiber/utils v1.1.0 h1:vdEBpn7AzIUJRhe+CiTOJdUcTg4Q9RK+pEa0KPbLdrM=
github.com/gofiber/utils v1.1.0/go.mod h1:poZpsnhBykfnY1Mc0KeEa6mSHrS3dV0+oBWyeQmb2e0=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down
18 changes: 18 additions & 0 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"os"
"strconv"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -29,6 +30,23 @@ func HandleFeed(c *fiber.Ctx) error {
panic(err)
}

if strings.Contains(c.Get("Accept"), "application/xml") {
template := "feed"

if page != 1 || c.Query("refresh") == "true" {
template = "feed_pages"
}

lastMovieYear := c.Query("lastMovieYear", movies[len(movies)-1].WatchedAt.Format("2006"))

return c.Render(template, fiber.Map{
"Movies": movies,
"Page": page + 1,
"CurrentYear": time.Now().Year(),
"LastMovieYear": lastMovieYear,
})
}

feed := views.Feed(
utils.IsAuthenticated(c),
movies,
Expand Down
41 changes: 40 additions & 1 deletion handlers/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand All @@ -37,6 +38,12 @@ func HandleGetMovieByID(c *fiber.Ctx) error {
}
}

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("movie", fiber.Map{
"Movie": movie,
})
}

return utils.TemplRender(c, views.Movie(movie))
}

Expand Down Expand Up @@ -93,6 +100,13 @@ func HandleGetMovieCastByID(c *fiber.Ctx) error {
}
}

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("cast", fiber.Map{
"Cast": updatedCastOrCrew,
"HasCharacters": hasCharacters,
})
}

return utils.TemplRender(c, components.CastList(updatedCastOrCrew, hasCharacters))
}

Expand All @@ -108,6 +122,12 @@ func HandleGetMovieSeenByID(c *fiber.Ctx) error {
return err
}

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("watched", fiber.Map{
"WatchedAt": watchedAt,
})
}

return utils.TemplRender(c, components.Watched(watchedAt, isAuth, id))
}

Expand All @@ -119,6 +139,10 @@ func HandleGetMovieNew(c *fiber.Ctx) error {
return c.Redirect("/")
}

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("newMovie", fiber.Map{})
}

return utils.TemplRender(c, views.NewMovie())
}

Expand Down Expand Up @@ -151,6 +175,7 @@ func tmdbFetchMovie(route string) (map[string]interface{}, error) {

// Handle adding a movie
func HandlePostMovieNew(c *fiber.Ctx) error {
log.Println("HandlePostMovieNew", c.FormValue("watched_at"))
isAuth := utils.IsAuthenticated(c)

if isAuth == false {
Expand Down Expand Up @@ -189,7 +214,15 @@ func HandlePostMovieNew(c *fiber.Ctx) error {
watchedAt, err := time.Parse("2006-01-02T15:04", data.WatchedAt)

if err != nil {
watchedAt = time.Now()
now := time.Now()
watchedAt, err = time.Parse("2006-01-02", data.WatchedAt)

if err != nil {
watchedAt = now
}

// Add 12 hours to the date if it doesn't have a time
watchedAt.Add(time.Duration(now.Hour()))
}

tx := db.Client.MustBegin()
Expand Down Expand Up @@ -390,6 +423,12 @@ func HandlePostMovieNew(c *fiber.Ctx) error {

tx.Commit()

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("newMovieAdded", fiber.Map{
"Title": movieInformation["title"],
})
}

c.Set("HX-Redirect", fmt.Sprintf("/movies/%d", movieId))

return c.SendStatus(fiber.StatusOK)
Expand Down
7 changes: 7 additions & 0 deletions handlers/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"believer/movies/utils"
"believer/movies/views"
"database/sql"
"strings"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -24,5 +25,11 @@ func HandleGetPersonByID(c *fiber.Ctx) error {
return err
}

if strings.Contains(c.Get("Accept"), "application/xml") {
return c.Render("person", fiber.Map{
"Person": person,
})
}

return utils.TemplRender(c, views.Person(person))
}
2 changes: 1 addition & 1 deletion public/styles.css

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions views/cast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<view xmlns="https://hyperview.org/hyperview" style="Cast__List">
{{ range $i, $cast := .Cast}}
<view style="Section">
<view style="Divider">
<text style="Divider__Text">{{ $cast.Job }}</text>
<view style="Divider__Line"></view>
</view>
{{ range $cast.People }}
{{ if $.HasCharacters }}
<view style="Cast__Row" key="{{ .Name }}-{{ $cast.Job }}">
<text style="Body__Text">
<behavior action="push" href="/person/{{ .ID }}" show-during-load="loading" />
{{ .Name }}
</text>
{{ if .Character }}
<view style="Cast__Row__Line"></view>
{{ end }}
<view style="Cast__Character">
<text style="Body__Text" numberOfLines="1">{{ .Character }}</text>
</view>
</view>
{{ else }}
<view style="Cast__Row" key="{{ .Name }}-{{ $cast.Job }}">
<text style="Body__Text">{{ .Name }}</text>
</view>
{{ end }}
{{ end }}
</view>
{{ end }}
</view>
50 changes: 50 additions & 0 deletions views/feed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<doc xmlns="https://hyperview.org/hyperview">
<screen id="feed">
<styles>
<style id="Body" backgroundColor="rgb(23,23,23)" flex="1"></style>
<style id="Loading" backgroundColor="red" flex="1"></style>
<style id="Link" borderBottomWidth="1" borderBottomColor="rgb(212,212,212)" borderStyle="solid"></style>
<style id="Link__Text" color="rgb(212,212,212)" fontSize="16"></style>
<style id="movieList" flex="1" paddingHorizontal="20" paddingTop="24"></style>
<style id="Movie" marginBottom="24" rowGap="16"></style>
<style id="Movie__Header" rowGap="4"></style>
<style id="Movie__Title" color="rgb(212,212,212)" fontSize="16" fontWeight="500"></style>
<style id="Movie__Overview" color="rgb(163,163,163)" fontSize="14" fontWeight="400" lineHeight="20"></style>
<style id="Spinner" alignItems="center" flex="1" height="48" justifyContent="center" ></style>
<style id="Movie__WatchedAt" color="rgb(163,163,163)" fontSize="12" fontWeight="400" lineHeight="16"></style>
<style id="FAB" position="absolute" bottom="24" right="24" backgroundColor="#262626" borderRadius="24" justifyContent="center" alignItems="center" height="56" width="56">
<modifier pressed="true">
<style backgroundColor="#404040" />
</modifier>
</style>
<style id="FAB__Text" color="rgb(229,229,229)" fontSize="24" fontWeight="400"></style>
<style id="Year__Wrap" marginVertical="16"></style>
<style id="Year" color="rgb(23,23,23)" fontSize="48" fontWeight="400" textShadowColor="rgba(255,255,255,0.8)" textShadowRadius="1"></style>
</styles>
<body style="Body" safe-area="true">
<list style="movieList" id="movies" trigger="refresh" href="/?refresh=true" action="replace-inner" target="movie-items">
<item style="Year__Wrap">
<text style="Year">
{{ .CurrentYear }}
</text>
</item>
{{ template "feed_pages" .}}
</list>
<view style="FAB">
<behavior show-during-load="loading" action="push" trigger="press" href="/movies/new" />
<text style="FAB__Text">
+
</text>
</view>
</body>
</screen>

<screen id="loading">
<styles>
<style id="Spinner" backgroundColor="rgb(23,23,23)" flex="1" alignItems="center" justifyContent="center"></style>
</styles>
<body style="Spinner">
<spinner color="rgba(63,63,63)" />
</body>
</screen>
</doc>
40 changes: 40 additions & 0 deletions views/feed_pages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{ $year := .LastMovieYear }}

<items xmlns="https://hyperview.org/hyperview" id="movie-items">
{{ range .Movies }}
{{if ne $year (.WatchedAt.Format "2006")}}
<item key="year-{{ .ID }}" style="Year__Wrap">
<text style="Year">
{{ .WatchedAt.Format "2006" }}
</text>
</item>
{{ $year = (.WatchedAt.Format "2006") }}
{{end}}
<item key="{{ .ID }}-{{ .WatchedAt.Format "2006-01-02 15:04" }}" style="Movie">
<behavior action="push" href="/movies/{{ .ID }}" show-during-load="loading" />
<view style="Movie__Header">
<text style="Movie__Title">
{{ .Title }}
</text>
<text style="Movie__WatchedAt">
{{ .WatchedAt.Format "January 02 2006" }}
</text>
</view>
{{ if .Overview }}
<text style="Movie__Overview" numberOfLines="3">
{{ .Overview }}
</text>
{{ end }}
</item>
{{ end }}
<item
key="loadMore-{{ .Page }}"
trigger="visible"
once="true"
href="/?page={{ .Page }}&lastMovieYear={{ $year }}"
action="replace"
style="Spinner"
>
<spinner />
</item>
</items>
Loading

0 comments on commit 4513bcf

Please sign in to comment.