Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
Improvements to serving UI from the API service.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcourant committed Sep 2, 2021
1 parent 65099ca commit 06d2a97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ func (c *Controller) RegisterRoutes(app *iris.Application) {
})
}

app.Get("/health", c.getHealth)

app.Use(func(ctx iris.Context) {
log := c.log.WithFields(logrus.Fields{
"requestId": ctx.GetHeader("X-Request-Id"),
Expand All @@ -149,6 +147,8 @@ func (c *Controller) RegisterRoutes(app *iris.Application) {
})

app.PartyFunc(APIPath, func(p router.Party) {
p.Get("/health", c.getHealth)

p.Use(c.loggingMiddleware)
p.OnAnyErrorCode(func(ctx iris.Context) {
if err := ctx.GetErr(); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/internal/cmd/controllers_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"github.com/go-pg/pg/v10"
"github.com/gomodule/redigo/redis"
"github.com/monetr/rest-api/pkg/application"
"github.com/monetr/rest-api/pkg/billing"
"github.com/monetr/rest-api/pkg/config"
"github.com/monetr/rest-api/pkg/controller"
"github.com/monetr/rest-api/pkg/internal/plaid_helper"
"github.com/monetr/rest-api/pkg/internal/stripe_helper"
"github.com/monetr/rest-api/pkg/jobs"
"github.com/monetr/rest-api/pkg/metrics"
"github.com/monetr/rest-api/pkg/secrets"
"github.com/monetr/rest-api/pkg/ui"
"github.com/sirupsen/logrus"
stripe_client "github.com/stripe/stripe-go/v72/client"
)

func getControllers(
Expand All @@ -23,7 +25,7 @@ func getControllers(
job jobs.JobManager,
plaidClient plaid_helper.Client,
stats *metrics.Stats,
stripeClient *stripe_client.API,
stripe stripe_helper.Stripe,
cache *redis.Pool,
plaidSecrets secrets.PlaidSecretsProvider,
basicPaywall billing.BasicPayWall,
Expand All @@ -36,7 +38,7 @@ func getControllers(
job,
plaidClient,
stats,
stripeClient,
stripe,
cache,
plaidSecrets,
basicPaywall,
Expand Down
16 changes: 8 additions & 8 deletions pkg/ui/ui_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package ui

import (
"fmt"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/core/router"
Expand All @@ -26,24 +25,25 @@ func NewUIController() *UIController {

func (c *UIController) RegisterRoutes(app *iris.Application) {
app.PartyFunc("/", func(p router.Party) {
p.Any("/api", func(ctx *context.Context) {
p.Any("/api/*", func(ctx *context.Context) {
ctx.Next()
ctx.StatusCode(http.StatusNotFound)
return
})

routes := p.HandleDir("/", http.FS(builtUi), iris.DirOptions{
IndexName: "index.html",
})
fmt.Sprint(routes)

p.Get("/config.json", func(ctx *context.Context) {
ctx.JSONP(map[string]interface{}{
"apiUrl": "/api",
})
})

p.Get("/*", func(ctx *context.Context) {
fileHandler := iris.FileServer(http.FS(builtUi), iris.DirOptions{
IndexName: "index.html",
SPA: true,
})

app.Get("/*", func(ctx iris.Context) {
fileHandler(ctx)
})
})
}

0 comments on commit 06d2a97

Please sign in to comment.