Skip to content

Commit e50d84f

Browse files
committed
you're doing great
1 parent 4c4316f commit e50d84f

File tree

12 files changed

+776
-0
lines changed

12 files changed

+776
-0
lines changed

000_temp/88-whole-enchilada/main.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"html/template"
5+
"net/http"
6+
_ "github.com/lib/pq"
7+
"database/sql"
8+
"log"
9+
"fmt"
10+
)
11+
12+
type customer struct {
13+
ID int
14+
First string
15+
}
16+
17+
var tpl *template.Template
18+
var db *sql.DB
19+
20+
func init() {
21+
var err error
22+
23+
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
24+
25+
connStr := "dbname=thanksgiving user=turkey password=gravy host=localhost port=5432 sslmode=disable"
26+
db, err = sql.Open("postgres", connStr)
27+
if err != nil {
28+
log.Fatal("**** NO OPEN ****", err)
29+
}
30+
31+
err = db.Ping()
32+
if err != nil {
33+
log.Fatal("**** NO PING WORKING ****", err)
34+
} else {
35+
fmt.Println("Ping successful")
36+
}
37+
}
38+
39+
func main() {
40+
defer db.Close()
41+
42+
http.HandleFunc("/", index)
43+
http.Handle("/assets/", http.StripPrefix("/assets", http.FileServer(http.Dir("./resources"))))
44+
http.ListenAndServe(":80", nil)
45+
}
46+
47+
func index(w http.ResponseWriter, r *http.Request) {
48+
rows, err := db.Query("SELECT * FROM customers;")
49+
if err != nil {
50+
log.Fatal(err)
51+
}
52+
defer rows.Close()
53+
54+
xc := []customer{}
55+
56+
for rows.Next() {
57+
c := customer{}
58+
rows.Scan(&c.ID, &c.First)
59+
xc = append(xc, c)
60+
}
61+
62+
tpl.ExecuteTemplate(w, "index.gohtml", xc)
63+
}
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport"
6+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>HOME</title>
9+
</head>
10+
<body>
11+
12+
<h1>HOME</h1>
13+
14+
{{range .}}
15+
<h1>ID - {{.ID}} .......... FIRST - {{.First}}</h1>
16+
{{end}}
17+
18+
19+
<img src="/assets/img/skyf.jpeg" alt="">
20+
</body>
21+
</html>

000_temp/88-whole-enchilada/texan

8.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)