@@ -3,17 +3,15 @@ package server
33import (
44 "context"
55 "fmt"
6- "io/fs"
76 "log"
87 "net/http"
9- "os"
10- "path/filepath"
118 "strings"
129
1310 "github.com/dgraph-io/ristretto"
1411 "github.com/gorilla/mux"
1512 "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1613 "github.com/newrelic/go-agent/v3/integrations/nrgrpc"
14+ "github.com/odpf/salt/spa"
1715 "github.com/odpf/stencil/config"
1816 "github.com/odpf/stencil/internal/store/postgres"
1917 "github.com/odpf/stencil/ui"
@@ -95,7 +93,12 @@ func Start(cfg config.Config) {
9593 }
9694
9795 rtr := mux .NewRouter ()
98- rtr .PathPrefix ("/ui" ).Handler (http .StripPrefix ("/ui" , newSpaHandler ()))
96+
97+ spaHandler , err := spa .Handler (ui .Assets , "build" , "index.html" , false )
98+ if err != nil {
99+ log .Fatalln ("Failed to load spa:" , err )
100+ }
101+ rtr .PathPrefix ("/ui" ).Handler (http .StripPrefix ("/ui" , spaHandler ))
99102
100103 runWithGracefulShutdown (& cfg , grpcHandlerFunc (s , gatewayMux , rtr ), func () {
101104 conn .Close ()
@@ -120,56 +123,3 @@ func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler, uiHandl
120123 }
121124 }), & http2.Server {})
122125}
123-
124- func newSpaHandler () spaHandler {
125- fsys , err := fs .Sub (ui .Assets , "build" )
126- if err != nil {
127- panic (err )
128- }
129- return spaHandler {
130- staticPath : "./" ,
131- indexFile : "index.html" ,
132- buildFs : fsys ,
133- }
134- }
135-
136- type spaHandler struct {
137- staticPath string
138- indexFile string
139- buildFs fs.FS
140- }
141-
142- // ServeHTTP inspects the URL path to locate a file within the static dir
143- // on the SPA handler. If a file is found, it will be served. If not, the
144- // file located at the index path on the SPA handler will be served. This
145- // is suitable behavior for serving an SPA (single page application).
146- func (h spaHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
147- log .Println ("ui path getting called" )
148- // get the absolute path to prevent directory traversal
149- path := r .URL .Path
150-
151- path = filepath .Join (h .staticPath , path )
152-
153- // check whether a file exists at the given path
154- _ , err := fs .Stat (h .buildFs , path )
155-
156- log .Println (err , os .IsNotExist (err ))
157- if os .IsNotExist (err ) {
158- // file does not exist, serve index.html
159- data , err := fs .ReadFile (h .buildFs , filepath .Join (h .staticPath , h .indexFile ))
160- if err != nil {
161- http .Error (w , err .Error (), http .StatusNotFound )
162- return
163- }
164- w .Write (data )
165- return
166- } else if err != nil {
167- // if we got an error (that wasn't that the file doesn't exist) stating the
168- // file, return a 500 internal server error and stop
169- http .Error (w , err .Error (), http .StatusInternalServerError )
170- return
171- }
172-
173- // otherwise, use http.FileServer to serve the static dir
174- http .FileServer (http .FS (h .buildFs )).ServeHTTP (w , r )
175- }
0 commit comments