我正在处理 PartyCon 的服务器端部分项目。它是在 Google App Engine 平台上用 Golang 编写的。我刚刚实现了一些在本地完美运行的新功能。但是,部署时,我无法丰富 console.go 脚本。
这是我的 app.yaml 配置(抱歉,这是 stackoverflow 显示 yaml 文件的方式):
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
还有我的 console.go 文件:
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
}
知道为什么会这样吗?提前致谢:)
最佳答案
对于 Go 应用程序,set the script handler到“_go_app”。例如:
handlers:
url: /api/.*
script: _go_app
AppEngine 将对 Go 应用程序的所有请求分派(dispatch)到单个已编译的可执行文件。这与 Python 不同,在 Python 中,您可以为每个处理程序指定不同的脚本。
关于google-app-engine - Go GAE 应用程序在本地工作,部署后我得到 404/nothing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29262947/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R