Skip to content

Commit

Permalink
server: add read only mode
Browse files Browse the repository at this point in the history
In read only mode the endpoint to save new effects or add new versions
is deactivated. Also in the gallery the create button is deactivated and
there is a notice that says:

    The server is in maintenance mode. You can not create or modify effects.

The button to save effects in the editor is not deactivated as it will
take a bit more time and wanted to have the page up as soon as possible.
  • Loading branch information
jfontan committed Jan 8, 2024
1 parent b0a0012 commit 6f577b1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions server/assets/gallery.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ <h1><a href="/" style="text-transform:uppercase">GLSL Sandbox</a> <a href="https
<a href="https://twitter.com/mrdoob">@mrdoob</a><br/>
editor by <a href="https://twitter.com/mrdoob">@mrdoob</a>, <a href="https://twitter.com/mrkishi">@mrkishi</a>, <a href="https://twitter.com/p01">@p01</a>, <a href="https://twitter.com/alteredq">@alteredq</a>, <a href="https://twitter.com/kusmabite">@kusmabite</a> and <a href="https://twitter.com/emackey">@emackey</a>
<br/><br/>

{{ if .ReadOnly }}
<h2 style="color:#ff6961">The server is in maintenance mode. You can not create or modify effects.</h2>
{{ else }}
<a href="/e"><button>new shader</button></a>
{{ end }}

<div id="gallery">

{{ if .Admin }}
Expand Down
2 changes: 2 additions & 0 deletions server/cmd/glslsandbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
TLSAddr string `envconfig:"TLS_ADDR"`
Domains string `envconfig:"DOMAINS" default:"www.glslsandbox.com,glslsandbox.com"`
Dev bool `envconfig:"DEV" default:"true"`
ReadOnly bool `envconfig:"READ_ONLY" default:"false"`
}

func main() {
Expand Down Expand Up @@ -82,6 +83,7 @@ func start() error {
auth,
cfg.DataPath,
cfg.Dev,
cfg.ReadOnly,
)
if err != nil {
return fmt.Errorf("could not create server: %w", err)
Expand Down
15 changes: 11 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const (
perPage = 50
)

var (
ErrInvalidData = fmt.Errorf("invalid data")
)
var ErrInvalidData = fmt.Errorf("invalid data")

type Template struct {
templates *template.Template
Expand Down Expand Up @@ -80,6 +78,7 @@ type Server struct {
effects *store.Effects
auth *Auth
dataPath string
readOnly bool
}

func New(
Expand All @@ -90,6 +89,7 @@ func New(
auth *Auth,
dataPath string,
dev bool,
readOnly bool,
) (*Server, error) {
var tpl *template.Template
if !dev {
Expand All @@ -115,6 +115,7 @@ func New(
effects: e,
auth: auth,
dataPath: dataPath,
readOnly: readOnly,
}, nil
}

Expand Down Expand Up @@ -163,7 +164,10 @@ func (s *Server) routes() {
s.echo.GET("/", s.indexHandler)
s.echo.GET("/e", s.effectHandler)
s.echo.GET("/e_", s.effectHandler_)
s.echo.POST("/e", s.saveHandler)

if !s.readOnly {
s.echo.POST("/e", s.saveHandler)
}

cors := middleware.CORSWithConfig(middleware.CORSConfig{
Skipper: middleware.DefaultSkipper,
Expand Down Expand Up @@ -228,6 +232,8 @@ type galleryData struct {
NextPage string
// Admin is true when accessing "/admin" path.
Admin bool
// ReadOnly tells the server is in read only mode.
ReadOnly bool
}

func (s *Server) indexRender(c echo.Context, admin bool) error {
Expand Down Expand Up @@ -290,6 +296,7 @@ func (s *Server) indexRender(c echo.Context, admin bool) error {
IsPrevious: page > 0,
PreviousPage: previousPage,
Admin: admin,
ReadOnly: s.readOnly,
}

return c.Render(http.StatusOK, "gallery", d)
Expand Down

0 comments on commit 6f577b1

Please sign in to comment.