Skip to content

Commit

Permalink
serve swagger static files as part of go binary
Browse files Browse the repository at this point in the history
  • Loading branch information
aradwann committed Jan 3, 2024
1 parent cc65989 commit 3238f53
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ protoc:
--grpc-gateway_out=pb --grpc-gateway_opt paths=source_relative \
--openapiv2_out=doc/swagger --openapiv2_opt=allow_merge=true,merge_file_name=eenergy \
proto/*.proto
statik -src=./doc/swagger -dest=./doc


evans:
evans --host localhost --port 9090 -r repl
Expand Down
14 changes: 14 additions & 0 deletions doc/statik/statik.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1
github.com/jackc/pgx/v5 v5.5.1
github.com/o1egl/paseto v1.0.0
github.com/rakyll/statik v0.1.7
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
go.uber.org/mock v0.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"os"

db "github.com/aradwann/eenergy/db/store"
_ "github.com/aradwann/eenergy/doc/statik"
"github.com/aradwann/eenergy/gapi"
"github.com/aradwann/eenergy/pb"
"github.com/aradwann/eenergy/util"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/rakyll/statik/fs"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -94,8 +97,12 @@ func runGatewayServer(config util.Config, store db.Store) {
mux := http.NewServeMux()
mux.Handle("/", grpcMux)

fs := http.FileServer(http.Dir("./doc/swagger"))
mux.Handle("/swagger/", http.StripPrefix("/swagger/", fs))
statikFs, err := fs.New()
if err != nil {
log.Fatalf("cannot create statik fs: %s", err)
}
swaggerHandler := http.StripPrefix("/swagger/", http.FileServer(statikFs))
mux.Handle("/swagger/", swaggerHandler)

listener, err := net.Listen("tcp", config.HTTPServerAddress)
if err != nil {
Expand Down

0 comments on commit 3238f53

Please sign in to comment.