Commits

Dobrosław Żybort  committed 7a108dc

Display rows from db

  • Participants
  • Parent commits b296b61

Comments (0)

Files changed (2)

 package main
 
 import (
+	"database/sql"
+	_ "github.com/ziutek/mymysql/godrv"
 	"html/template"
 	"net/http"
+	"time"
 )
 
 //=============================================================================
 
+type Paste struct {
+	ID         int64 // auto_increment
+	Datetime   time.Time
+	Author     string
+	Protection int
+	Syntax     string
+	URL        string
+	Lifespan   int
+	IP         string
+	Data       string
+}
+
+func NewPaste() *Paste {
+	return &Paste{
+		Datetime: time.Now().UTC(),
+		Syntax:   "plaintext",
+	}
+}
+
+//=============================================================================
+
 var index = template.Must(template.ParseFiles(
 	"templates/_base.html",
 	"templates/index.html",
 ))
 
 func hello(w http.ResponseWriter, req *http.Request) {
-	if err := index.Execute(w, nil); err != nil {
+
+	con, err := sql.Open("mymysql", "pastebingo"+"/"+"root"+"/"+"12345")
+	defer con.Close()
+
+	rows, err := con.Query("select author, data, datetime from pastes where paste_id=?", 1)
+	if err != nil {
+		panic(err)
+	}
+	defer rows.Close()
+
+	pastes := []Paste{}
+
+	for rows.Next() {
+		paste := Paste{}
+		err := rows.Scan(&paste.Author, &paste.Data, &paste.Datetime)
+		if nil != err {
+			panic(err)
+		}
+
+		pastes = append(pastes, paste)
+	}
+
+	if err := index.Execute(w, pastes); err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
 	}
 }

File templates/index.html

 	<h1>Pastebin</h1>
 	
 	<ul class="guest">
-		<li>...</li>
+		{{ range . }}
+		<li>
+			<blockquote>{{ .Data }}</blockquote>
+			<p>- <cite>{{ .Author }}</cite></p>
+			<p>- <cite>{{ .Datetime }}</cite></p>
+		</li>
+		{{end }}
 	</ul>
 {{ end }}