0

私はGinフレームワークを使用しています。ローカル開発モード: goapp serve すべて正常に動作します。

func init() {
    route := gin.Default()
    route.LoadHTMLGlob("../*/views/**/*.html")
    ...
}

しかし、デプロイ後:

パニック: html/テンプレート: パターンが一致するファイルがありません:../*/views/**/*.html

わかった。私は試します:

func init() {
    route := gin.Default()
    dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
    route.LoadHTMLGlob(dir + "/../*/views/**/*.html")
    ...
}

同じ結果です。

私はディレクトリを取得しようとします:

...
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
    ...
}
c.String(http.StatusOK, "Dir: ", dir)

c.String(http.StatusOK, "\nOK")
res, err := filepath.Glob(dir + "/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v\n\n", res, err))

c.String(http.StatusOK, "Dirs:")
res, err = filepath.Glob(dir + "/**/*")
c.String(http.StatusOK, fmt.Sprintf("%v | %v", res, err))
...

結果:

ディレクトリ: %!(EXTRA string=/base/data/home/apps/tmp-LEIYJC/_ah) OK[/base/data/home/apps/tmp-LEIYJC/_ah/exe] |

ディレクトリ:[] |

おっと。私は何を間違えましたか?

更新:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: ../static/images

- url: /css
  static_dir: ../static/css

- url: /js
  static_dir: ../static/js

- url: /fonts
  static_dir: ../static/fonts

- url: /.*
  script: _go_app

app.yaml をサブディレクトリに配置しましたが、別の問題は発生しませんでした: [ https://groups.google.com/forum/#!topic/google-appengine-go/dNhqV6PBqVc

フォルダ構造:

app/
    app.go
    app.yaml
static/
    ...
frontend/
controllers/
    UserController.go
    ...
models/
    UserModel.go
    ...
views/
        home/
            *.html
        user/
            *.html
        anotherfolder/
            *.html
backend/
controllers/
    MainController.go
    ...
models/
    SomeModel.go
    ...
views/
        main/
            *.html
        anotherfolder/
            *.html
...
4

2 に答える 2

2

次の解決策を見つけました。

構造を再編成します。

app/
    static/
        ...
    frontend/
        views/
           home/
               *.html
           user/
               *.html
           anotherfolder/
               *.html
    backend/
        views/
            main/
                *.html
            anotherfolder/
                *.html
    app.go
    app.yaml

frontend/
    controllers/
        UserController.go
        ...
    models/
        UserModel.go
        ...
backend/
    controllers/
        MainController.go
    ...
    models/
        SomeModel.go
    ...
...

変化する:

app.yaml

runtime: go
api_version: go1

handlers:
- url: /images
  static_dir: static/images

- url: /css
  static_dir: static/css

- url: /js
  static_dir: static/js

- url: /fonts
  static_dir: static/fonts

- url: /.*
  script: _go_app

それは本当に奇妙なecision GAEです。重複インポートのパニック メッセージが表示されたため、アプリケーションのルート ディレクトリに app.yaml を配置できません。また、テンプレートをルート ディレクトリに配置することはできません。これは、app.yaml の親ディレクトリにのみ適用される範囲内にないためです。

于 2016-05-26T09:52:26.030 に答える
1

私が間違っていなければ、すべてのディレクトリは app.yaml が存在するルート ディレクトリ内にあるはずです。だから、あなたはこのようなものが欲しいでしょう。application_readable: trueまた、アプリケーションで静的ファイル ディレクトリにアクセスする場合は、そのディレクトリ定義に追加する必要があります。次の例を参照してください。お役に立てれば。

app/
    app.go
    app.yaml
    static/
        ...
    frontend/views/
        home/
        *.html
    anotherfolder/
        *.html

ディレクトリ定義の例:

- url: /s
  static_dir: s/
  application_readable: true
于 2016-05-25T20:26:46.223 に答える