How to setup golang backend and react frontend in a monorepo
source link: https://dev.to/ynwd/how-to-setup-golang-backend-and-react-frontend-in-a-monorepo-3api
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Posted on Oct 20
How to setup golang backend and react frontend in a monorepo
Previously, we have set up a frontend module using react and typescript in a monorepo.
Next, we will use the golang server to serve the built webapp.
.
├── go.mod
├── go.sum
├── main.go
├── package.json
└── web
├── components
└── modules
└── root
├── build
│ ├── asset-manifest.json
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ ├── robots.txt
│ └── static
│ ├── css
│ │ ├── main.33a5a96b.chunk.css
│ │ └── main.33a5a96b.chunk.css.map
│ └── js
│ ├── 2.59f16c8b.chunk.js
│ ├── 2.59f16c8b.chunk.js.LICENSE.txt
│ ├── 2.59f16c8b.chunk.js.map
│ ├── 3.93db3793.chunk.js
│ ├── 3.93db3793.chunk.js.map
│ ├── main.f7ff0158.chunk.js
│ ├── main.f7ff0158.chunk.js.LICENSE.txt
│ ├── main.f7ff0158.chunk.js.map
│ ├── runtime-main.08d49f3a.js
│ └── runtime-main.08d49f3a.js.map
└── package.json
clone repo: https://github.com/ynwd/monorepo/tree/typescript
create services folder
mkdir -p internal/services
init golang app
go mod init github.com/ynwd/monorepo
downlod fastrex package
go get github.com/fastrodev/fastrex
this will generate go.mod file
module github.com/ynwd/monorepo
go 1.17
require github.com/fastrodev/fastrex v0.0.0-20211008073151-687f0b90ec18 // indirect
create golang app entry point
/* main.go */
package main
import (
"github.com/fastrodev/fastrex"
)
func main() {
app := fastrex.New()
app.Template("web/modules/root/build/index.html")
app.Static("web/modules/root/build")
app.Get("/", func(req fastrex.Request, res fastrex.Response) {
err := res.Render()
if err != nil {
panic(err)
}
})
err := app.Listen(8080)
if err != nil {
panic(err)
}
}
build react root module
npm run build -w @fstr/root
run golang server
go run main.go
You can see the final source code here: https://github.com/ynwd/monorepo/tree/fastrex
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK