我需要go语言代码来编写两个静态Dashboard页面,并能够对mysql数据库中的contain表进行增删改查。第一个页面中能够输入user表的用户id,用户名,密码,然后跳转到第二个页面,展示用户id所对应的contain表中所有匹配的contain的contain的id,contain是否在运行,最后一次更新的时间,所需要的镜像id,占用的cpu核数,占用内存大小及暴露的端口号的信息并能够对记录进行增删改查。
时间: 2024-03-27 14:40:37 浏览: 39
好的,以下是一个简单的示例代码,你可以根据实际情况进行修改:
```go
package main
import (
"database/sql"
"fmt"
"html/template"
"log"
"net/http"
_ "github.com/go-sql-driver/mysql"
)
type User struct {
ID int
Username string
Password string
}
type Contain struct {
ID int
UserID int
Running bool
LastUpdated string
ImageID string
CPUCores int
Memory int
Port int
}
func main() {
db, err := sql.Open("mysql", "username:password@tcp(localhost:3306)/database_name")
if err != nil {
log.Fatal(err)
}
defer db.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
// Insert new user
username := r.FormValue("username")
password := r.FormValue("password")
stmt, err := db.Prepare("INSERT INTO user(username, password) VALUES(?, ?)")
if err != nil {
log.Fatal(err)
}
res, err := stmt.Exec(username, password)
if err != nil {
log.Fatal(err)
}
lastID, err := res.LastInsertId()
if err != nil {
log.Fatal(err)
}
http.Redirect(w, r, fmt.Sprintf("/contain?id=%d", lastID), http.StatusSeeOther)
} else {
// Display user form
tmpl := template.Must(template.ParseFiles("user.html"))
tmpl.Execute(w, nil)
}
})
http.HandleFunc("/contain", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "Missing user ID", http.StatusBadRequest)
return
}
// Get user information
var user User
err := db.QueryRow("SELECT * FROM user WHERE id = ?", id).Scan(&user.ID, &user.Username, &user.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == "POST" {
// Insert new contain
running := r.FormValue("running") == "on"
lastUpdated := r.FormValue("last_updated")
imageID := r.FormValue("image_id")
cpuCores := r.FormValue("cpu_cores")
memory := r.FormValue("memory")
port := r.FormValue("port")
stmt, err := db.Prepare("INSERT INTO contain(user_id, running, last_updated, image_id, cpu_cores, memory, port) VALUES(?, ?, ?, ?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
_, err = stmt.Exec(id, running, lastUpdated, imageID, cpuCores, memory, port)
if err != nil {
log.Fatal(err)
}
}
// Get contains information
rows, err := db.Query("SELECT * FROM contain WHERE user_id = ?", id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
var contains []Contain
for rows.Next() {
var contain Contain
err := rows.Scan(&contain.ID, &contain.UserID, &contain.Running, &contain.LastUpdated, &contain.ImageID, &contain.CPUCores, &contain.Memory, &contain.Port)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
contains = append(contains, contain)
}
err = rows.Err()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Display contain information
tmpl := template.Must(template.ParseFiles("contain.html"))
data := struct {
User User
Contains []Contain
}{
User: user,
Contains: contains,
}
tmpl.Execute(w, data)
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
```
其中,`user.html`和`contain.html`是两个HTML模板文件,用于渲染用户表单和包含表信息。你需要根据实际需求来编写这两个模板文件。
另外,为了使代码更安全,你需要根据实际情况来修改数据库连接字符串,以及对数据库操作的语句。
阅读全文