Skip to content

Commit

Permalink
Serve assets and show effects editor
Browse files Browse the repository at this point in the history
  • Loading branch information
jfontan committed Jul 13, 2021
1 parent 767f464 commit f2af370
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is=
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q=
Expand Down Expand Up @@ -65,6 +66,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
73 changes: 73 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package server

import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"path"
"strconv"
"strings"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/mrdoob/glsl-sandbox/server/store"
)
Expand Down Expand Up @@ -65,12 +69,18 @@ func (s *Server) Start() error {
func (s *Server) setup() {
s.echo.Renderer = s.template
s.echo.Logger.SetLevel(log.DEBUG)
s.echo.Use(middleware.Logger())
s.routes()
}

func (s *Server) routes() {
s.echo.GET("/", s.indexHandler)
s.echo.GET("/e", s.effectHandler)
s.echo.GET("/item/:id", s.itemHandler)

s.echo.Static("/thumbs", "./data/thumbs")
s.echo.Static("/css", "./server/assets/css")
s.echo.Static("/js", "./server/assets/js")
}

func (s *Server) indexHandler(c echo.Context) error {
Expand Down Expand Up @@ -114,3 +124,66 @@ func (s *Server) indexHandler(c echo.Context) error {
// return c.File("./server/assets/gallery.html")
// return c.String(http.StatusOK, "index")
}

func (s *Server) effectHandler(c echo.Context) error {
return c.File("./static/index.html")
}

type itemResponse struct {
Code string `json:"code"`
User string `json:"user"`
Parent string `json:"parent,omitempty"`
}

func (s *Server) itemHandler(c echo.Context) error {
param := c.Param("id")
var idString, versionString string

parts := strings.Split(param, ".")
switch len(parts) {
case 1:
idString = parts[0]
case 2:
idString = parts[0]
versionString = parts[1]
default:
return c.String(http.StatusBadRequest, "{}")
}

id, err := strconv.Atoi(idString)
if err != nil {
return c.String(http.StatusBadRequest, "{}")
}

version, err := strconv.Atoi(versionString)
if err != nil {
return c.String(http.StatusBadRequest, "{}")
}

effect, err := s.store.Effect(id)
if err != nil {
return c.String(http.StatusBadRequest, "{}")
}

if version >= len(effect.Versions) || version < 0 {
return c.String(http.StatusNotFound, "{}")
}

parent := ""
if effect.Parent > 0 {
parent = fmt.Sprintf("/e#%d.%d", effect.Parent, effect.ParentVersion)
}

item := itemResponse{
Code: effect.Versions[version].Code,
User: effect.User,
Parent: parent,
}

data, err := json.Marshal(item)
if err != nil {
return c.String(http.StatusInternalServerError, "{}")
}

return c.Blob(http.StatusOK, "application/json", data)
}

0 comments on commit f2af370

Please sign in to comment.