Skip to content

Commit

Permalink
Merge pull request #10 from connorwalsh/serve-static-page
Browse files Browse the repository at this point in the history
Serve Static Page
  • Loading branch information
c authored Feb 18, 2018
2 parents e16d99d + 3ed4e7d commit 0aa15bd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ version: '2'
services:
dev_server:
env_file: .env
environment:
- DEV_ENV=true
build:
context: ./server/
volumes:
Expand Down
22 changes: 21 additions & 1 deletion server/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"database/sql"
"fmt"
"os"
"path"

"github.com/connorwalsh/new-yorken-poesry-magazine/server/env"
"github.com/gocraft/web"
)

Expand All @@ -16,14 +18,16 @@ const (

type API struct {
*Logger
Config *env.Config
Version string
Router *web.Router
db *sql.DB
}

func NewAPI(db *sql.DB) *API {
func NewAPI(config *env.Config, db *sql.DB) *API {
api := API{
Logger: NewLogger(os.Stdout),
Config: config,
Version: API_VERSION,
db: db,
}
Expand Down Expand Up @@ -81,4 +85,20 @@ func (a *API) BuildRouter() {

// TODO
Get(dashPrefix+"/user/:"+API_ID_PATH_PARAM, a.GetUser)

// set serve static middleware only if in production
if a.Config.DevEnv == false {
currentRoot, err := os.Getwd()
if err != nil {
panic(err)
}

// add static server middleware
a.Router.Middleware(
web.StaticMiddleware(
path.Join(currentRoot, "client/build"),
web.StaticOption{IndexFile: "index.html"},
),
)
}
}
9 changes: 8 additions & 1 deletion server/core/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ func NewPlatform() *Platform {
p.Setup()

// construct API and pass it the db connection handle set within Connect ---^
p.Api = NewAPI(p.db)
p.Api = NewAPI(p.config, p.db)

// print out server configuration
if p.config.DevEnv {
p.Info("Server running in Development mode")
} else {
p.Info("Server running in Production mode")
}

return p
}
Expand Down
5 changes: 3 additions & 2 deletions server/env/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const (

// server conf
type Config struct {
Port string `env:"SERVER_PORT" envDefault:"8080"`
DB DB
DevEnv bool `env:"DEV_ENV" envDefault:"false"`
Port string `env:"SERVER_PORT" envDefault:"8080"`
DB DB
}

// DB conf
Expand Down

0 comments on commit 0aa15bd

Please sign in to comment.