Skip to content

Commit

Permalink
add support for https
Browse files Browse the repository at this point in the history
  • Loading branch information
jfontan committed Jul 23, 2021
1 parent d1adf5e commit 8f95258
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ RUN go build -v ./server/cmd/glslsandbox
FROM debian:buster-slim

EXPOSE 8888
EXPOSE 8883
COPY --from=builder /build/ /glslsandbox/
ENTRYPOINT [ "/glslsandbox/entrypoint.sh" ]
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash

cd glslsandbox
export DEV ADDR AUTH_SECRET IMPORT
export TLS_ADDR DOMAINS
DATA_PATH=/data ./glslsandbox
12 changes: 11 additions & 1 deletion server/cmd/glslsandbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Config struct {
Import string `envconfig:"IMPORT"`
AuthSecret string `envconfig:"AUTH_SECRET" default:"secret"`
Addr string `envconfig:"ADDR" default:":8888"`
TLSAddr string `envconfig:"TLS_ADDR"`
Domains string `envconfig:"DOMAINS" default:"www.glslsandbox.com,glslsandbox.com"`
Dev bool `envconfig:"DEV" default:"true"`
}

Expand Down Expand Up @@ -72,7 +74,15 @@ func start() error {
}
}

s, err := server.New(cfg.Addr, effects, auth, cfg.DataPath, cfg.Dev)
s, err := server.New(
cfg.Addr,
cfg.TLSAddr,
cfg.Domains,
effects,
auth,
cfg.DataPath,
cfg.Dev,
)
if err != nil {
return fmt.Errorf("could not create server: %w", err)
}
Expand Down
55 changes: 48 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
"github.com/mrdoob/glsl-sandbox/server/store"
"golang.org/x/crypto/acme/autocert"
)

const (
pathGallery = "./server/assets/gallery.html"
pathThumbs = "./data/thumbs"
pathThumbs = "thumbs"
pathCerts = "certs"
perPage = 50
)

Expand Down Expand Up @@ -71,6 +73,8 @@ func (t *Template) Render(

type Server struct {
addr string
tlsAddr string
domains []string
echo *echo.Echo
template *Template
effects *store.Effects
Expand All @@ -80,6 +84,8 @@ type Server struct {

func New(
addr string,
tlsAddr string,
domains string,
e *store.Effects,
auth *Auth,
dataPath string,
Expand All @@ -94,9 +100,15 @@ func New(
}
}

if tlsAddr != "" && domains == "" {
return nil, fmt.Errorf("cannot specify TLS_ADDR without DOMAINS")
}

return &Server{
addr: addr,
echo: echo.New(),
addr: addr,
tlsAddr: tlsAddr,
domains: strings.Split(domains, ","),
echo: echo.New(),
template: &Template{
templates: tpl,
},
Expand All @@ -107,15 +119,44 @@ func New(
}

func (s *Server) Start() error {
s.setup()
err := s.setup()
if err != nil {
return err
}

if s.tlsAddr != "" {
go func() {
err := s.echo.Start(s.addr)
s.echo.Logger.Errorf("could not create http server: %s", err.Error())
}()

return s.echo.StartAutoTLS(s.tlsAddr)
}

return s.echo.Start(s.addr)
}

func (s *Server) setup() {
func (s *Server) setup() error {
if s.tlsAddr != "" {
certs := filepath.Join(s.dataPath, pathCerts)
err := os.MkdirAll(certs, 0750)
if err != nil {
return fmt.Errorf("could not create certs directory: %w", err)
}

s.echo.AutoTLSManager.Cache = autocert.DirCache(certs)
s.echo.AutoTLSManager.HostPolicy = autocert.HostWhitelist(s.domains...)

s.echo.Pre(middleware.HTTPSRedirect())
}

s.echo.Use(middleware.Recover())
s.echo.Renderer = s.template
s.echo.Logger.SetLevel(log.DEBUG)
s.echo.Use(middleware.Logger())
s.routes()

return nil
}

func (s *Server) routes() {
Expand All @@ -124,7 +165,7 @@ func (s *Server) routes() {
s.echo.POST("/e", s.saveHandler)
s.echo.GET("/item/:id", s.itemHandler)

s.echo.Static("/thumbs", filepath.Join(s.dataPath, "thumbs"))
s.echo.Static("/thumbs", filepath.Join(s.dataPath, pathThumbs))
s.echo.Static("/css", "./server/assets/css")
s.echo.Static("/js", "./server/assets/js")
s.echo.File("/diff", "./server/assets/diff.html")
Expand Down Expand Up @@ -440,7 +481,7 @@ func (s *Server) loginHandler(c echo.Context) error {
}

func thumbPath(dataPath string, id int) string {
return filepath.Join(dataPath, "thumbs", fmt.Sprintf("%d.png", id))
return filepath.Join(dataPath, pathThumbs, fmt.Sprintf("%d.png", id))
}

func saveImage(path string, data []byte) error {
Expand Down

0 comments on commit 8f95258

Please sign in to comment.