Embed trust center v2 in the go backend

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Sacha Al Himdani
2025-09-30 16:36:31 +02:00
parent e32927ccf6
commit f3194def05
8 changed files with 356 additions and 173 deletions

View File

@@ -15,189 +15,35 @@
package web
import (
"compress/gzip"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"strings"
"github.com/getprobo/probo/apps/console"
"github.com/getprobo/probo/pkg/server/statichandler"
)
type Server struct {
spaFS http.FileSystem
etags map[string]string
indexETag string
indexContent []byte
*statichandler.Server
}
func NewServer() (*Server, error) {
subFS, err := fs.Sub(console.StaticFiles, "dist")
if err != nil {
return nil, err
gzipOptions := statichandler.GzipOptions{
EnableFileTypeCheck: false,
}
etags := make(map[string]string)
err = fs.WalkDir(
subFS,
".",
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
info, err := d.Info()
if err != nil {
return err
}
content := make([]byte, info.Size())
file, err := subFS.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = file.Read(content)
if err != nil {
return err
}
hash := md5.Sum(content)
etag := hex.EncodeToString(hash[:])
etags["/"+path] = etag
return nil
},
)
if err != nil {
return nil, fmt.Errorf("cannot generate etags: %w", err)
}
indexETag, ok := etags["/index.html"]
if !ok {
return nil, errors.New("index.html not found")
}
indexFile, err := subFS.Open("index.html")
if err != nil {
return nil, err
}
indexContent, err := io.ReadAll(indexFile)
spaServer, err := statichandler.NewServer(console.StaticFiles, "dist", gzipOptions)
if err != nil {
return nil, err
}
return &Server{
spaFS: http.FS(subFS),
indexETag: indexETag,
indexContent: indexContent,
etags: etags,
Server: spaServer,
}, nil
}
func (s *Server) ServeSPA(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
f, err := s.spaFS.Open(path)
if err != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("ETag", `"`+s.indexETag+`"`)
if r.Header.Get("If-None-Match") == `"`+s.indexETag+`"` {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.WriteHeader(http.StatusOK)
w.Write(s.indexContent)
return
}
defer f.Close()
info, err := f.Stat()
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if info.IsDir() {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("ETag", `"`+s.indexETag+`"`)
if r.Header.Get("If-None-Match") == `"`+s.indexETag+`"` {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
w.WriteHeader(http.StatusOK)
w.Write(s.indexContent)
return
}
etag, ok := s.etags[path]
if !ok {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.Header().Set("ETag", etag)
if matchETag := r.Header.Get("If-None-Match"); matchETag != "" {
if matchETag == etag {
w.WriteHeader(http.StatusNotModified)
return
}
}
if strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".css") ||
strings.HasSuffix(path, ".png") || strings.HasSuffix(path, ".jpg") ||
strings.HasSuffix(path, ".svg") || strings.HasSuffix(path, ".woff2") {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
} else {
w.Header().Set("Cache-Control", "public, max-age=3600")
}
http.FileServer(s.spaFS).ServeHTTP(w, r)
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
s.ServeSPA(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
return
}
s.ServeSPA(w, r)
s.Server.ServeHTTP(w, r)
}
func (s *Server) ServeSPA(w http.ResponseWriter, r *http.Request) {
s.Server.ServeSPA(w, r)
}