Skip to content

Commit

Permalink
add build tag for embedding app in backend binary
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinfuchs committed May 11, 2023
1 parent 1a88f1f commit 4ee6249
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ cd ../embedg-server
go run main.go migrate postgres up

# Start the development server (optional)
go run main.go server
go run --tags embedapp main.go server

# Build and include the frontend files in the backend binary
go build --tags embedapp

# Build without including the frontend files in the backend binary (you need to serve yourself)
go build
```

Expand Down Expand Up @@ -96,7 +99,6 @@ postgres:

app:
public_url: "http://localhost:5173/app"
server_static: true # Set to false if you don't want the serve the frontend files (you will need a HTTP server like Nginx to host it instead)

api:
# Make sure to add {public_url}/auth/callback to the OAuth2 Redirect URLs of your application in the Discord dev portal
Expand Down
3 changes: 3 additions & 0 deletions embedg-app/embed.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build embedapp
// +build embedapp

package embedgapp

import "embed"
Expand Down
8 changes: 8 additions & 0 deletions embedg-app/noembed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !embedapp
// +build !embedapp

package embedgapp

import "embed"

var DistFS embed.FS
1 change: 1 addition & 0 deletions embedg-app/package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package embedgapp
14 changes: 6 additions & 8 deletions embedg-server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ func Serve() {

RegisterRoutes(app, &stores{pg, bot})

if viper.GetBool("app.serve_static") {
app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(embedgapp.DistFS),
Browse: false,
NotFoundFile: "dist/index.html",
PathPrefix: "/dist",
}))
}
app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(embedgapp.DistFS),
Browse: false,
NotFoundFile: "dist/index.html",
PathPrefix: "/dist",
}))

go bot.Start()

Expand Down
1 change: 0 additions & 1 deletion embedg-server/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func setupDefaults() {
v.SetDefault("postgres.password", "")

v.SetDefault("app.public_url", "http://localhost:5173/app")
v.SetDefault("app.serve_static", true)

// API defaults
v.SetDefault("api.host", "localhost")
Expand Down

0 comments on commit 4ee6249

Please sign in to comment.