如何处理会议

时间:2019-03-29 21:10:15

标签: angular google-app-engine go google-cloud-platform

我正在尝试构建一个应用程序,该应用程序是托管在Google App Engine上的Go后端,Angular前端,如果您没有会话或会话的登录状态为= 1,则会强制您登录/ login。

我还试图对几乎所有内容使用App Engine的app.yaml路由。

我不确定这有可能吗?

目录结构:

/myapp/app.yaml
/myapp/server/main.go
/myapp/client/(ANGULAR)

app.yaml(摘自:here)     应用:myapp     版本:1     运行时:go111     #api_version:go1     主要:./服务器

- url: /go/.* #Anything that goes to the golang app
  script: _go_app

# Routing for bundles to serve directly
- url: /((?:inline|main|polyfills|styles|vendor)\.[a-z0-9]+\.bundle\.js)
  secure: always
  redirect_http_response_code: 301
  static_files: client/app/dist/\1
  upload: client/app/dist/.*

# Routing for a prod styles.bundle.css to serve directly
- url: /(styles\.[a-z0-9]+\.bundle\.css)
  secure: always
  redirect_http_response_code: 301
  static_files: client/app/dist/\1
  upload: client/app/dist/.*

# Routing for typedoc, assets and favicon.ico to serve directly
- url: /((?:assets|docs)/.*|favicon\.ico)
  secure: always
  redirect_http_response_code: 301
  static_files: client/app/dist/\1
  upload: client/app/dist/.*

# Any other requests are routed to index.html for angular to handle so we don't need hash URLs
- url: /.*
  #secure: always
  redirect_http_response_code: 301
  static_files: client/app/dist/index.html
  upload: client/app/dist/index\.html
  #http_headers:
  #  Strict-Transport-Security: max-age=31536000; includeSubDomains
  #  X-Frame-Options: DENY

因此,到/ go的路由将充当api ... CRUD的内容。其他所有内容都将归于Angular。

那么我如何检查是否有会议?我怀疑在app.yaml中是否可能。如果不拨打/ go呼叫,则没有真正的服务器告诉它是否存在会话。

那么,我是否不可能以这种方式这样做?我是否需要使用Go的路由,以便每个呼叫都可以进行会话检查?

1 个答案:

答案 0 :(得分:2)

是的,您猜对了。标记为静态的文件/文件夹与您的Go应用(通过Google的内容传输网络)分开提供,因此无法知道Go应用的会话ID和状态。

这对您有问题吗?通常,静态文件(例如HTML,CSS和JavaScript文件)可以在未经授权/身份验证的情况下交付,它们不会带来安全风险。

如果您不想将静态文件设为“公开”,则必须使用Go应用来提供这些文件。不要将其标记为静态,并使用Go的标准库的任何文件服务机制(例如http.FileServer()http.ServeFile()http.ServeContent())。使用中间件模式检查会话是否存在,如果存在,则仅调用文件服务器。

(或实施自己提供静态内容的服务,您可以在自己的处理程序中做您想做/需要做的一切。)

例如,将Go中的“受保护”文件映射到/protected,并将某些“真实”静态文件(由Google自动提供)映射到/static,它看起来可能像这样:

app.yaml

- url: /protected/.*
  script: _go_app

- url: /static
  static_dir: static

然后在您的Go源码中,您可以提供“受保护”文件,如下所示:

func init() {
    fileHandler := http.StripPrefix("/protected",
        http.FileServer(http.Dir("protected")))

    http.HandleFunc("/protected/", func(w http.ResponseWriter, r *http.Request) {
        // You may check if a session exists here
        sessExists := ...

        if !sessExists {
            http.Error(w, "you must login first", http.StatusUnauthorized)
            return
        }

        // Serve the requested file:
        fileHandler.ServeHTTP(w, r)
    })
}

上面的init()函数注册一个处理程序,该处理程序处理以/protected/为前缀的路径,如果存在会话(该逻辑属于您),它将调用提供{ {1}}文件夹。所提供的文件是从路径中提取的,protected前缀已去除。例如。路径/protected将指定/protected/secret.txt文件。