duanjianl183188 2015-03-25 17:50
浏览 34

Go GAE应用程序可在本地运行,部署后我一无所获404

I'm working on a serverside part of a PartyCon project. It is written in Golang on Google App Engine platform. I have just implemented a few new things which work perfectly LOCALLY. However, when deployed, I cannot rich console.go script.

Here is my app.yaml configuration (sorry, this is how stackoverflow shows yaml files):

application: party-serverside version: alpha-1 runtime: go
api_version: go1

handlers:

#handlers for api
- url: /api/.*   
  script: api/api.go

#handlers for console and webpage routing
- url: /redirect   
  script: redirecter/redirecter.go

- url: /admin_console/choose   
  script: admin_console/choose.go

- url: /post-request   
  script: webpage/post-request.go

- url: /console   
  script: console/console.go

 #handlers for static files
 - url: /css/console.css   
   static_files: console/page/css/console.css   upload: console/page/css/console.css

- url: /console/page   
  static_dir: console/page

- url: /   
  static_files: webpage/index.html   
  upload: webpage/index.html

- url: /   
  static_dir: webpage

- url: /css   
  static_dir: webpage/css

- url: /js   
  static_dir: webpage/js

- url: /img   
  static_dir: webpage/img

- url: /fonts   
  static_dir: webpage/fonts

And my console.go file:

package console

import (
    "appengine"
    "appengine/user"
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
    "html/template"
    "io/ioutil"
    "net/http"
    "strconv"
    "time"
)

//for deployment
var dbConnectString string = "****************************"

//for local testing
//var dbConnectString string = "root@/party"

func init() {
    http.HandleFunc("/console", consoleHandler)
}

func consoleHandler(w http.ResponseWriter, r *http.Request) {
    redirectIfNeeded(w, r)
    c := appengine.NewContext(r)
    u := user.Current(c)

    logoutUrl, e := user.LogoutURL(c, "/redirect")

    if e != nil {
        panic(e)
    }

    email := u.Email
    data := WebpageData{LogoutUrl: logoutUrl, UserName: email, NewPartyUrl: "/console/newparty"}

    template := template.Must(template.New("template").Parse(generateUnsignedHtml(u)))

    err := template.Execute(w, data)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }

}

func generateUnsignedHtml(u *user.User) string {
    firstPart := fileValue("./console/page/firstPart.html")
    table := generateTable(u)
    secondPart := fileValue("./console/page/secondPart.html")

    html := firstPart + table + secondPart
    return html
}

func generateTable(u *user.User) string {
    con, e := sql.Open("mysql", dbConnectString)
    if e != nil {
        panic(e)
    }
    defer con.Close()

    var parties []Party
    partyRows, err := con.Query("select id, name, datetime, host, location from parties where author='" + u.Email + "';")
    if err != nil {
        panic(err)
    }

    var id int
    var name string
    var datetime string
    var host string
    var location string

    for partyRows.Next() {
        partyRows.Scan(&id, &name, &datetime, &host, &location)
        parties = append(parties, Party{Id: id, Name: name, DatetimeString: datetime, Host: host, Location: location})
    }

    html := ""
    for i, party := range parties {
        actionsHtml := "<a href=\"/console/edit?id=" + strconv.Itoa(party.Id) + "\" class=\"uk-button uk-button-primary editButton\">Edit</a> <a href=\"/console/delete?id=" + strconv.Itoa(party.Id) + "\" class=\"uk-button uk-button-danger\">Delete</a>"
        html += "<tr>" + makeTd(strconv.Itoa(i+1)) + makeTd(party.Name) + makeTd(party.DatetimeString) + makeTd(party.Host) + makeTd(party.Location) + makeTd(actionsHtml) + "</tr>"
    }

    html += "</table>"
    return html
}

func makeTd(content string) string {
    return "<td>" + content + "</td>"
}

func redirectIfNeeded(w http.ResponseWriter, r *http.Request) {
    expire := time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC)
    cookie := &http.Cookie{Name: "ACSID", Value: "", Expires: expire, HttpOnly: true}
    http.SetCookie(w, cookie)
    cookie2 := &http.Cookie{Name: "SACSID", Value: "", Expires: expire, HttpOnly: true}
    http.SetCookie(w, cookie2)

    c := appengine.NewContext(r)
    u := user.Current(c)
    if u == nil {
        url, err := user.LoginURL(c, r.URL.String())
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        w.Header().Set("Location", url)
        w.WriteHeader(http.StatusFound)
        return
    }

    con, e := sql.Open("mysql", dbConnectString)
    if e != nil {
        panic(e)
    }
    defer con.Close()

    //check whether user is admin
    admRows, error := con.Query("select email from admin_users;")
    if error != nil {
        panic(error)
    }
    var email string
    isAdmin := false
    for admRows.Next() {
        admRows.Scan(&email)
        if email == u.Email {
            isAdmin = true
        }
    }

    //check if he is validated user
    validRows, error2 := con.Query("select email from party_validated_users;")
    if error2 != nil {
        panic(error2)
    }
    email = ""
    isValidated := false
    for validRows.Next() {
        validRows.Scan(&email)
        if email == u.Email {
            isValidated = true
        }
    }

    var url string

    if user.IsAdmin(c) || isAdmin {
        //user is declared as admin in db or is admin of gae app
        //we are allready here
        url = "/console"
    } else if isValidated {
        //user is validated
        //we are allready here
        url = "/console"
    } else {
        //user is not validated yet
        url = "/redirect"
        w.Header().Set("Location", url)
        w.WriteHeader(http.StatusFound)
    }
}

func fileValue(path string) string {
    content, err := ioutil.ReadFile(path)
    if err != nil {
        panic(err)
    }
    return string(content)
}

type WebpageData struct {
    LogoutUrl   string
    UserName    string
    NewPartyUrl string
}

type Party struct {
    Id             int
    Name           string
    DatetimeString string
    Host           string
    Location       string
}

Any idea why this happens? Thanks in advance :)

  • 写回答

1条回答 默认 最新

  • doutao4480 2015-03-25 19:18
    关注

    For Go applications, set the script handler to "_go_app". For example:

    handlers:
        url: /api/.* 
        script: _go_app
    

    AppEngine dispatches all requests for Go applications to a single compiled executable. This is different from Python where you can specify a different script for each handler.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?