- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我正在使用herokuapp为我托管golang应用程序,我试图制作一个注册表单以使人们可以注册并成为用户,但是我的代码遇到错误:
我有2个主要文件,main.go和store.go,它们处理服务器内容,然后有一个/ assets文件夹,其中包含我的静态html(包括注册页面)。
main.go中的方法:
type User struct {
username string `json:"username"`
gender bool `json:"gender"`
age int `json:"age"`
password string `json:"password"`
email string `json:"email"`
}
func newRouter() *mux.Router {
r := mux.NewRouter()
r.HandleFunc("/user", getUserHandler).Methods("GET")
r.HandleFunc("/user", createUserHandler).Methods("POST")
r.HandleFunc("/forms/login", loginUserHandler).Methods("POST")
r.HandleFunc("/forms/signup", createUserHandler).Methods("POST")
//ALL PAGE FUNCTIONS HERE
r.HandleFunc("/", handler).Methods("GET")
//Declare static file directory
staticFileDirectory := http.Dir("./assets/")
staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory))
r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET")
return r
}
func main() {
router := newRouter()
portEnv := os.Getenv("PORT")
port := ":" + portEnv
http.ListenAndServe(port, router)
url := os.Getenv("DATABASE_URL")
db, err := sql.Open("postgres", url)
if err != nil {
log.Fatalf("Connection error: %s", err.Error())
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
log.Fatalf("Ping error: %s", err.Error())
panic(err)
}
InitStore(&dbStore{db: db})
}
func createUserHandler(w http.ResponseWriter, r *http.Request) {
user := User{}
//Send all data as HTML form Data so parse form
err := r.ParseForm()
if err != nil {
fmt.Println(fmt.Errorf("Error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
//Get the information about the user from user info
user.username = r.Form.Get("username")
// user.gender, _ = strconv.ParseBool(r.Form.Get("gender"))
user.age = 16
user.password = r.Form.Get("password")
cpassword := r.Form.Get("cpassword")
user.email = r.Form.Get("email")
if(user.password != cpassword) {
http.Redirect(w, r, "/assets/signup.html", http.StatusSeeOther)
return
}
user.password = hashAndSalt([]byte(user.password))
//Append existing list of users with a new entry
err = store.CreateUser(&user)
if err != nil {
log.Println(err)
fmt.Println(err)
}
//Set Cookie with username
addCookie(w, "username", user.username)
http.Redirect(w, r, "/assets/", http.StatusFound)
}
type Store interface {
CreateUser(user *User) error
GetUsers()([]*User, error)
}
func (store *dbStore) CreateUser(user *User) error {
_, err := store.db.Query("INSERT INTO users(username,age,password,email) VALUES ($1,$2,$3,$4);",user.username,user.age,user.password,user.email)
return err
}
var store dbStore
func InitStore(s dbStore) {
store = s
}
2020-02-07T04:58:18.077322+00:00 app[web.1]: 2020/02/07 04:58:18 http: panic serving (MYIP):18930: runtime error: invalid memory address or nil pointer dereference
2020-02-07T04:58:18.077334+00:00 app[web.1]: goroutine 13 [running]:
2020-02-07T04:58:18.077337+00:00 app[web.1]: net/http.(*conn).serve.func1(0xc00008f400)
2020-02-07T04:58:18.077339+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/net/http/server.go:1769 +0x139
2020-02-07T04:58:18.077341+00:00 app[web.1]: panic(0x725720, 0xa29b80)
2020-02-07T04:58:18.077343+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/runtime/panic.go:522 +0x1b5
2020-02-07T04:58:18.077346+00:00 app[web.1]: database/sql.(*DB).conn(0x0, 0x7fdf40, 0xc000016048, 0xa2a301, 0x0, 0x0, 0xc000055920)
2020-02-07T04:58:18.077348+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/database/sql/sql.go:1080 +0x3a
2020-02-07T04:58:18.077350+00:00 app[web.1]: database/sql.(*DB).query(0x0, 0x7fdf40, 0xc000016048, 0x798f41, 0x44, 0xc000055a30, 0x4, 0x4, 0x1, 0xc00010acc0, ...)
2020-02-07T04:58:18.077352+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/database/sql/sql.go:1513 +0x66
2020-02-07T04:58:18.077355+00:00 app[web.1]: database/sql.(*DB).QueryContext(0x0, 0x7fdf40, 0xc000016048, 0x798f41, 0x44, 0xc000055a30, 0x4, 0x4, 0x6d5085, 0x0, ...)
2020-02-07T04:58:18.077357+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/database/sql/sql.go:1495 +0xd1
2020-02-07T04:58:18.077359+00:00 app[web.1]: database/sql.(*DB).Query(...)
2020-02-07T04:58:18.077362+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/database/sql/sql.go:1509
2020-02-07T04:58:18.077364+00:00 app[web.1]: main.(*dbStore).CreateUser(0xa34758, 0xc000055b50, 0x20, 0xc00010acc0)
2020-02-07T04:58:18.077366+00:00 app[web.1]: /tmp/tmp.N9S5bJfo59/.go/src/github.com/InsanityMatrix/SocialFoot/store.go:19 +0x1a3
2020-02-07T04:58:18.077369+00:00 app[web.1]: main.createUserHandler(0x7fd680, 0xc0000f4460, 0xc00012ce00)
2020-02-07T04:58:18.077371+00:00 app[web.1]: /tmp/tmp.N9S5bJfo59/.go/src/github.com/InsanityMatrix/SocialFoot/main.go:114 +0x367
2020-02-07T04:58:18.077373+00:00 app[web.1]: net/http.HandlerFunc.ServeHTTP(0x79e1a0, 0x7fd680, 0xc0000f4460, 0xc00012ce00)
2020-02-07T04:58:18.077375+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/net/http/server.go:1995 +0x44
2020-02-07T04:58:18.077378+00:00 app[web.1]: github.com/InsanityMatrix/SocialFoot/vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000f0000, 0x7fd680, 0xc0000f4460, 0xc00012cc00)
2020-02-07T04:58:18.077380+00:00 app[web.1]: /tmp/tmp.N9S5bJfo59/.go/src/github.com/InsanityMatrix/SocialFoot/vendor/github.com/gorilla/mux/mux.go:210 +0xe3
2020-02-07T04:58:18.077382+00:00 app[web.1]: net/http.serverHandler.ServeHTTP(0xc000084a90, 0x7fd680, 0xc0000f4460, 0xc00012cc00)
2020-02-07T04:58:18.077384+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/net/http/server.go:2774 +0xa8
2020-02-07T04:58:18.077386+00:00 app[web.1]: net/http.(*conn).serve(0xc00008f400, 0x7fdf00, 0xc000020a40)
2020-02-07T04:58:18.077388+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/net/http/server.go:1878 +0x851
2020-02-07T04:58:18.077391+00:00 app[web.1]: created by net/http.(*Server).Serve
2020-02-07T04:58:18.077393+00:00 app[web.1]: /app/tmp/cache/go1.12.12/go/src/net/http/server.go:2884 +0x2f4
最佳答案
除非有错误,否则函数http.ListenAndServe()
不会返回。它侦听HTTP连接并为其提供服务。因此,main中的数据库初始化代码永远不会运行,并且HTTP连接正在使用nil数据库连接。
将http.ListenAndServe
移动到main()
的底部进行修复。
关于postgresql - 数据库连接在Golang中出错了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60107358/
我的 postgresql 有问题,我复制了所有文件,然后将其删除。然后,我安装了新的,问题就解决了。现在可以将旧文件和文件导入新文件吗? 最佳答案 如果它们是相同的主要版本(即 9.0 到 9.0.
我想使用 Postgresql 9.2.2 来存储我的应用程序的数据。我不得不构建一个应该基于数据库级别的触发器(当数据库启动时,这个触发器将被触发并执行。),当 postgresql 服务器启动时是
我已经使用下面的查询从 Postgresql 目录表中获取 Sequence 对象的完整信息 select s.sequence_name, s.start_value, s.minimum_valu
Postgres 版本:9.3.4 我需要执行驻留在远程数据库中的函数。该函数根据给定的参数返回一个统计数据表。 我实际上只是在我的本地数据库中镜像该函数,以使用我的数据库角色和授权来锁定对该函数的访
我在 CentOS 7 上,我正在尝试解决“PG::ConnectionBad: FATAL: Peer authentication failed for user”错误。 所以我已经想出我应该更改
我写了一个触发器函数,在触发器表列名上循环,我从具有不同列的不同表调用该函数。该函数将列名插入到数组中并在它们上循环,以便将值插入到另一个模式和表中。 函数和触发器创建脚本: DROP TRIGGER
PostgreSQL 的默认空闲连接超时是多少,我运行了 show idle_in_transaction_session_timeout 查询并返回了 0,但是值 0 表示此选项被禁用,但我想知道默
我需要将十六进制值存储到数据库表中,谁能推荐我需要用于属性的数据类型? 提前致谢 最佳答案 您可以使用bytea 来存储十六进制格式。更多信息 can be found in the postgres
我有一个具有复合主键的(大)表,由 5 列(a、b、c、d、e)组成。 我想高效地选择具有其中两列 (a + e) 的所有行到给定值。 在 PostgreSQL 中,我需要索引吗?或者数据库会使用主键
在阅读 PostreSQL (13) 文档时,我遇到了 this页面,其中列出了不同日期时间类型的存储大小。 除其他外,它指出: Name Storag
我有两个大整数的巨大表(500 000 000 行)。两列都被单独索引。我正在使用语法批量插入此表: INSERT into table (col1, col2) VALUES(x0, y0), (x
有一台 CentOS7 Linux 机器正在运行(不是由我管理;拥有有限的权限)。 请求在其中设置 PostgreSQL。 刚刚从 CentOS 存储库安装了 PostgreSQL: sudo yum
我在 Ubuntu 18.04 上安装了 Postgresql 10,但不知何故坏了,不会重新启动。我可以重新安装它而不破坏它的数据库,以便我可以再次访问数据库吗? pg_dump 不起作用。 最佳答
我想在 UNIX 中使用 crontab 自动备份 PostgreSQL 数据库。我已经尝试过,但它会创建 0 字节备份。 我的 crontab 条目是: 24 * * * * /home/desk
我已经完成了PG服务器的安装。我希望能够使用 pgAdmin 远程连接到它,但不断收到服务器不听错误。 could not connect to server: Connection refused
Oracle 支持波斯历但需要知道 PostgreSQL 是否支持波斯历? 如果是,那么我们如何在 PostgreSQL 中将默认日历类型设置为 Persian 而不是 Gregorian(在 Ora
假设我们有一个带有表的 SQL 数据库 Person以及访问它的几个应用程序。出于某种原因,我们想修改 Person表以向后不兼容的方式。 保持兼容性的一种潜在解决方案是将表重命名为 User并创建一
我使用 PostgreSQL 中的模式来组织我庞大的会计数据库。每年年底,我都会通过为下一年创建一个新模式来进行协调过程。 新模式的文件是否与旧模式物理分离?或者所有模式一起存储在硬盘上? 这对我来说
我正在尝试使用配置文件中的以下配置参数调整 PostgreSQL 服务器: autovacuum_freeze_max_age = 500000000 autovacuum_max_workers =
我的数据包含数据库列中的表情符号,即 message_text ------- 🙂 😀 Hi 😀 我只想查询包含表情符号的数据的行。在 postgres 中是否有一种简单的方法可以做到这一点?
我是一名优秀的程序员,十分优秀!