Как развернуть React (Frontend) и Go (BE) к героку? - PullRequest
0 голосов
/ 23 июня 2019

Я пытаюсь развернуть приложение React + Go в Heroku, но я пытаюсь понять, что я делаю неправильно.

Структура приложения выглядит следующим образом:

my-app
|
|__ build
|
|___ public
|    |-- manifest.json
|    |-- index.html
|    |-- favicon.ico
|
|___ server (Go)
|    |__ api
|    |__ db
|    |__ utils
|
|___ src (React)
|    |__ assets
|    |__ components
|        |__app
|        |  |-- app.js
|    |-- index.js
|
|-- .babelrc
|-- go.mod (Go modules)
|-- go.sum
|-- index.go
|-- package.json
|-- Procfile
|-- webpack.config
|-- webpack.development.js
|-- webpack.production.js

index.go

package main

import (
    "fmt"
    "net/http"
    "os"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./build")))
}

func GetPort() string {
    var port = os.Getenv("PORT")
    // Set a default port if there is nothing in the environment
    if port == "" {
        port = "4000"
        fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port)
    }
    return ":" + port
}

Как видно, бэкэнд очень просткоторая обслуживает статические страницы внешнего интерфейса в каталоге сборки.

package.json

{
  "name": "fullstack-course",
  "main": "index.go",
  "scripts": {
    "client": "rm -rf build/ && webpack --env.mode production",
    "server": "rm -rf index && go build -o index",
    "start": "./index",
    "heroku-postbuild": "yarn run client && yarn run server"
  },
  "engines": {
    "node": "10.x",
    "npm": "6.9.x",
    "yarn": "1.x"
  },
  "dependencies": {...}
}

Что касается:

"client": "rm -rf build/ && webpack --env.mode production",

Сценарий client очищает сборку, а затем запускает webpack.producton.js для связывания внешнего интерфейса

Что касается:

"server": "rm -rf index && go build -o index",

server удаляет скомпилированный файл с именем index и снова строит индекс.

Чтобы запустить сервер, мы просто делаем "start": "./index",

Heroku Buildpacks

  1. heroku / nodejs
  2. heroku / go

Журнал Heroku

При нажатии на Heroku, Не удается построить со следующим журналом

-----> Node.js app detected

-----> Creating runtime environment

       NPM_CONFIG_LOGLEVEL=error
       NODE_ENV=production
       NODE_MODULES_CACHE=false
       NODE_VERBOSE=false

-----> Installing binaries
       engines.node (package.json):  10.x
       engines.npm (package.json):   6.9.x
       engines.yarn (package.json):  1.x

       Resolving node version 10.x...
       Downloading and installing node 10.16.0...
       Bootstrapping npm 6.9.x (replacing 6.9.0)...
       npm 6.9.x installed
       Resolving yarn version 1.x...
       Downloading and installing yarn (1.16.0)...
       Installed yarn 1.16.0

-----> Restoring cache
       Caching has been disabled because NODE_MODULES_CACHE=false

-----> Installing dependencies
       Installing node modules (yarn.lock)
       yarn install v1.16.0
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@1.2.9: The platform "linux" is incompatible with this module.
       info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       [4/4] Building fresh packages...
       Done in 21.23s.

-----> Build
       Running heroku-postbuild (yarn)
       yarn run v1.16.0
       $ yarn run client && yarn run server
       $ rm -rf build/ && webpack --env.mode production
       Hash: e3957a7f35b32ffdafe0
       Version: webpack 4.34.0
       Time: 6283ms
       Built at: 06/14/2019 4:43:46 PM
                                                Asset       Size  Chunks             Chunk Names
                                          favicon.ico   3.78 KiB          [emitted]  
                                           index.html  582 bytes          [emitted]  
                                        manifest.json  306 bytes          [emitted]  
                              resources-manifest.json  147 bytes          [emitted]  
                                    service-worker.js   21 bytes          [emitted]  
             static/css/main.b3bdf9a060b617f0a8a6.css   56 bytes       0  [emitted]  main
               static/js/main.2404dde5082f7f65e664.js   3.21 KiB       0  [emitted]  main
       static/js/vendors~main.64517854689ee0e9248d.js    135 KiB       1  [emitted]  vendors~main
       Entrypoint main = static/js/vendors~main.64517854689ee0e9248d.js static/css/main.b3bdf9a060b617f0a8a6.css static/js/main.2404dde5082f7f65e664.js
        [8] (webpack)/buildin/global.js 472 bytes {1} [built]
       [12] ./node_modules/react-redux/es/index.js + 18 modules 29.8 KiB {1} [built]
            |    19 modules
       [20] (webpack)/buildin/harmony-module.js 573 bytes {1} [built]
       [22] ./src/index.js + 5 modules 5.26 KiB {0} [built]
            | ./src/index.js 483 bytes [built]
            | ./src/store.js 1.29 KiB [built]
            | ./src/reducers/index.js 274 bytes [built]
            | ./src/reducers/initial.js 607 bytes [built]
            |     + 2 hidden modules
           + 20 hidden modules
       Child html-webpack-plugin for "index.html":
            1 asset
           Entrypoint undefined = index.html
           [0] ./node_modules/html-webpack-plugin/lib/loader.js!./public/index.html 1.9 KiB {0} [built]
           [2] (webpack)/buildin/global.js 472 bytes {0} [built]
           [3] (webpack)/buildin/module.js 497 bytes {0} [built]
               + 1 hidden module
       Child mini-css-extract-plugin node_modules/css-loader/dist/cjs.js!src/components/app/App.css:
           Entrypoint mini-css-extract-plugin = *
              2 modules
       $ rm -rf index && go build -o index
/bin/sh: 1: go: not found
error Command failed with exit code 127.
       info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 127.
       info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
-----> Build failed

       We're sorry this build is failing! You can troubleshoot common issues here:
       https://devcenter.heroku.com/articles/troubleshooting-node-deploys

       If you're stuck, please submit a ticket so we can help:
       https://help.heroku.com/

       Love,
       Heroku

 !     Push rejected, failed to compile Node.js app.
 !     Push failed

Я понятия не имею, почему Heroku не может запустить команду go.Пожалуйста, кто-нибудь может помочь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...