Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: add read only mode #92

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading