Skip to content
Permalink
Browse files

feat: server now serves static files too

  • Loading branch information...
moul committed May 16, 2019
1 parent 3239df4 commit aa7ca764f4b8de98585648a4c805136d4e726037
Showing with 51 additions and 3 deletions.
  1. +22 −0 contrib/firebase/index.html
  2. +1 −0 go.mod
  3. +2 −0 go.sum
  4. +2 −0 server/cmd_server.go
  5. +24 −3 server/server.go
@@ -0,0 +1,22 @@
<html>
<head>
</head>
<body>
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.0.2/firebase-auth.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "AIzaSyBzLrepNGhispjYaJqX3xWIO9KZpZdvZCk",
authDomain: "pathwar-pwn.firebaseapp.com",
databaseURL: "https://pathwar-pwn.firebaseio.com",
projectId: "pathwar-pwn",
storageBucket: "pathwar-pwn.appspot.com",
messagingSenderId: "1009405509165",
appId: "1:1009405509165:web:89aaba3d48ccec7f"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
</body>
</html>
1 go.mod
@@ -12,6 +12,7 @@ require (
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.3.3 // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-sql-driver/mysql v1.4.1
github.com/gobuffalo/packr/v2 v2.0.0-rc.15
github.com/gogo/gateway v1.0.0
2 go.sum
@@ -44,6 +44,8 @@ github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-chi/chi v4.0.2+incompatible h1:maB6vn6FqCxrpz4FqWdh4+lwpyZIQS7YEAUcHlgXVRs=
github.com/go-chi/chi v4.0.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
@@ -19,6 +19,7 @@ type serverOptions struct {
HTTPBind string
JWTKey string
WithReflection bool
WebDir string
}

func (opts serverOptions) String() string {
@@ -38,6 +39,7 @@ func (cmd *serverCommand) LoadDefaultOptions() error { return viper.Unmarshal(&c
func (cmd *serverCommand) ParseFlags(flags *pflag.FlagSet) {
flags.StringVar(&cmd.opts.GRPCBind, "grpc-bind", ":9111", "gRPC server address")
flags.StringVar(&cmd.opts.HTTPBind, "http-bind", ":8000", "HTTP server address")
flags.StringVar(&cmd.opts.WebDir, "web-dir", "", "Static Files Directory")
flags.StringVar(&cmd.opts.JWTKey, "jwt-key", "", "JWT secure key")
flags.BoolVarP(&cmd.opts.WithReflection, "grpc-reflection", "", false, "enable gRPC reflection")
if err := viper.BindPFlags(flags); err != nil {
@@ -5,7 +5,10 @@ import (
"crypto/rand"
"net"
"net/http"
"time"

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/gogo/gateway"
"github.com/gogo/protobuf/gogoproto"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
@@ -35,6 +38,7 @@ func server(opts *serverOptions) error {
}

func startHTTPServer(ctx context.Context, opts *serverOptions) error {
// configure gateway mux
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &gateway.JSONPb{
EmitDefaults: false,
@@ -47,10 +51,27 @@ func startHTTPServer(ctx context.Context, opts *serverOptions) error {
if err := RegisterServerHandlerFromEndpoint(ctx, gwmux, opts.GRPCBind, grpcOpts); err != nil {
return err
}

// configure chi router
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(5 * time.Second))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
// gateway mux
r.Mount("/api", gwmux)
// static files
if opts.WebDir != "" {
fs := http.StripPrefix("/", http.FileServer(http.Dir(opts.WebDir)))
r.Get("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}

// start HTTP server
zap.L().Info("starting HTTP server", zap.String("bind", opts.HTTPBind))
mux := http.NewServeMux()
mux.Handle("/api/", gwmux)
return http.ListenAndServe(opts.HTTPBind, mux)
return http.ListenAndServe(opts.HTTPBind, r)
}

func startGRPCServer(ctx context.Context, opts *serverOptions) error {

0 comments on commit aa7ca76

Please sign in to comment.
You can’t perform that action at this time.