Skip to content

Commit 521d0ad

Browse files
authored
feat: move spa to salt spa (#156)
1 parent c5fbcad commit 521d0ad

7 files changed

Lines changed: 22 additions & 68 deletions

File tree

cmd/namespace.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ func listNamespaceCmd(cdk *CDK) *cobra.Command {
7575
fmt.Printf("\nShowing %[1]d of %[1]d namespaces \n \n", len(namespaces))
7676
report := [][]string{}
7777
index := 1
78-
report = append(report, []string{"INDEX", "NAMESPACE", "FORMAT", "COMPATIBILITY"})
78+
report = append(report, []string{
79+
term.Bold("INDEX"),
80+
term.Bold("NAMESPACE"),
81+
term.Bold("FORMAT"),
82+
term.Bold("COMPATIBILITY"),
83+
})
7984
for _, n := range namespaces {
8085
report = append(report,
8186
[]string{term.Greenf("#%d", index),

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ type DBConfig struct {
1414
ConnectionString string
1515
}
1616

17-
//GRPCConfig grpc options
17+
// GRPCConfig grpc options
1818
type GRPCConfig struct {
1919
MaxRecvMsgSizeInMB int `default:"10"`
2020
MaxSendMsgSizeInMB int `default:"10"`
2121
}
2222

23-
//Config Server config
23+
// Config Server config
2424
type Config struct {
2525
Port string `default:"8080"`
2626
// Timeout represents graceful shutdown period. Defaults to 60 seconds.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/georgysavva/scany v0.2.9
1212
github.com/golang-migrate/migrate/v4 v4.15.2
1313
github.com/google/uuid v1.3.0
14-
github.com/gorilla/mux v1.8.0 // indirect
14+
github.com/gorilla/mux v1.8.0
1515
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
1616
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0
1717
github.com/hamba/avro v1.6.2
@@ -20,7 +20,7 @@ require (
2020
github.com/jhump/protoreflect v1.9.0
2121
github.com/newrelic/go-agent/v3 v3.12.0
2222
github.com/newrelic/go-agent/v3/integrations/nrgrpc v1.3.1
23-
github.com/odpf/salt v0.2.0
23+
github.com/odpf/salt v0.2.1
2424
github.com/pkg/errors v0.9.1
2525
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
2626
github.com/spf13/cobra v1.2.1

go.sum

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ github.com/Microsoft/hcsshim v0.9.2/go.mod h1:7pLA8lDk46WKDWlVsENo92gC0XFa8rbKfy
106106
github.com/Microsoft/hcsshim/test v0.0.0-20201218223536-d3e5debf77da/go.mod h1:5hlzMzRKMLyo42nCZ9oml8AdTlq/0cvIaBv6tK1RehU=
107107
github.com/Microsoft/hcsshim/test v0.0.0-20210227013316-43a75bb4edd3/go.mod h1:mw7qgWloBUl75W/gVH3cQszUg1+gUITj7D6NY7ywVnY=
108108
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
109+
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
109110
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
110111
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
111112
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
@@ -1005,10 +1006,8 @@ github.com/nishanths/predeclared v0.0.0-20200524104333-86fad755b4d3/go.mod h1:nt
10051006
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
10061007
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
10071008
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
1008-
github.com/odpf/salt v0.1.1-0.20220903111417-7d83984bc683 h1:9v+haUbG5BlhL+vuFmYOpkztPjtdxLiku790ABy38N8=
1009-
github.com/odpf/salt v0.1.1-0.20220903111417-7d83984bc683/go.mod h1:goInym7R4lIwV0jhfmjybtYvuiApEJ8Yuu8tBGzkQ9I=
1010-
github.com/odpf/salt v0.2.0 h1:yVo8sLuvxu6UYWtuMPupa3K5RaFfTeJEW5y3Wdlybxg=
1011-
github.com/odpf/salt v0.2.0/go.mod h1:WqWeaUg6BMPsR2wniV9XJpov2IHNw6X6WtiH8GGrwH0=
1009+
github.com/odpf/salt v0.2.1 h1:91TRXZ85XKeKUwq6jotcAD5eo40kP1GS3xZMvXK9xR0=
1010+
github.com/odpf/salt v0.2.1/go.mod h1:ZmDaHPtlwvOlluzv+qiC3qZj99hmfxlFRMqfqZx5IP8=
10121011
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
10131012
github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo=
10141013
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=

internal/api/ping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"google.golang.org/grpc/health/grpc_health_v1"
77
)
88

9-
//Check grpc health check
9+
// Check grpc health check
1010
func (s *API) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
1111
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
1212
}

internal/server/server.go

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@ package server
33
import (
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-
}

internal/store/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (e StorageErr) GRPCStatus() *status.Status {
5050
return status.New(codes.Unknown, e.Error())
5151
}
5252

53-
//WithErr convenience function to override sentinel errors
53+
// WithErr convenience function to override sentinel errors
5454
func (e StorageErr) WithErr(err error, name string) StorageErr {
5555
e.err = err
5656
e.name = name

0 commit comments

Comments
 (0)