This repository has been archived by the owner on Sep 13, 2018. It is now read-only.
/
main.go
120 lines (96 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/render"
"net/http"
// "fmt"
"database/sql"
_ "github.com/lib/pq"
// "github.com/coopernurse/gorp"
)
func SetupDB() *sql.DB {
db, err := sql.Open("postgres", "dbname=devtalks-dev sslmode=disable")
PanicIf(err)
return db
}
func PanicIf(err error){
if err != nil {
panic(err)
}
}
type Video struct {
Id int64
Title string
}
func main() {
m := martini.Classic()
m.Map(SetupDB())
m.Use(render.Renderer(render.Options{
Layout: "layout",
}))
m.Use(martini.Static("assets"))
m.Get("/", func (ren render.Render, r * http.Request, db *sql.DB) {
rows, err := db.Query("SELECT * from videos")
PanicIf(err)
defer rows.Close()
videos := []Video{}
for rows.Next() {
v := Video{}
err := rows.Scan(&v.Id, &v.Title)
PanicIf(err)
videos = append(videos, v)
// fmt.Fprintf(rw, "Title: %s", title)
}
ren.HTML(200, "videos", videos)
})
m.Run()
}
// type Page struct {
// Title string
// }
// type Response map[string]interface{}
// // convert the Response map to string
// func (r Response) String() (s string) {
// b, err := json.Marshal(r)
// if err != nil {
// s = ""
// return
// }
// s = string(b)
// return
// }
// // func viewHandler(w http.ResponseWriter, req *http.Request) {
// // p := &Page{Title: "title"}
// // t, _ := template.ParseFiles("views/app.html")
// // t.Execute(res, p)
// // }
// func homeHandler(w http.ResponseWriter, req *http.Request) {
// fmt.Println("homeHandler")
// filePath := "public/index.html"
// content, err := ioutil.ReadFile(filePath)
// if err != nil {
// }
// fmt.Fprintf(w, string(content))
// return
// }
// func jsonHandler(w http.ResponseWriter, req *http.Request) {
// w.Header().Set("Content-Type", "application/json")
// // cast to string and pass it to response
// fmt.Fprint(w, Response{"success": true, "message": "Hello!"})
// return
// }
// func staticHandler(w http.ResponseWriter, req *http.Request) {
// fmt.Println("staticHandler")
// http.ServeFile(w, req, req.URL.Path[1:])
// // http.FileServer(http.Dir("public"))
// }
// func main() {
// r := mux.NewRouter()
// // http.Handle("/", homeHandler)
// r.HandleFunc("/", homeHandler)
// r.HandleFunc("/api/v1/videos", jsonHandler)
// http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public/"))))
// http.Handle("/", r)
// err := http.ListenAndServe(":8090", nil)
// fmt.Println(err)
// }