commit 6edad6f3f3dc355d4a3c78968c2f81a13a7a9aac Author: José Carlos Cuevas Date: Thu Jun 25 14:46:18 2015 +0200 Initial commit of this experiment diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8291f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.swp +*.swo +/bin +/pkg +/src/github.com +/src/golang.org diff --git a/src/data/data.go b/src/data/data.go new file mode 100644 index 0000000..0991b40 --- /dev/null +++ b/src/data/data.go @@ -0,0 +1,159 @@ +package data + +import ( + "fmt" + "log" + "time" + "database/sql" +) + +const human_time_layout = "Mon Jan 2 15:04:05" +const time_layout = "2006-01-02 15:04:05" + +const database_command = "CREATE TABLE IF NOT EXISTS post (id INTEGER AUTO_INCREMENT PRIMARY KEY, title VARCHAR (255), body TEXT, author VARCHAR(120), date DATETIME)" + +func InitDatabase(con *sql.DB) { + _, err := con.Exec(database_command) + + if err != nil { + log.Print(err) + return + } +} + +// Post struct and methods + +type Post struct { + ID int + Title string + Body string + Author string + PostDate time.Time + isSaved bool +} + +func (p *Post) Save(con *sql.DB) { + var statement string + var err error + var res sql.Result + + if !p.isSaved { + statement = "INSERT INTO post(title, body, author, date) VALUES (?,?,?,?)" + + } else { + statement = "UPDATE post SET title=?, body=?, author=?, date=? WHERE id = ?" + + } + + insert, err := con.Prepare(statement) + + if err != nil { + log.Printf("Error preparing statement: %v", err) + return + } + + p.PostDate = time.Now() + + if !p.isSaved { + res, err = insert.Exec(p.Title, p.Body, p.Author, p.PostDate.Format(time_layout)) + } else { + res, err = insert.Exec(p.Title, p.Body, p.Author, p.PostDate.Format(time_layout), p.ID) + } + + if err != nil { + fmt.Printf("Error inserting post into database: %v", err) + return + } + + lastID, err := res.LastInsertId() + + if err != nil { + log.Printf("Error obtaining ID for inserted row: %v", err) + return + } + + p.ID = int(lastID) + p.isSaved = true +} + +func (p Post) String() string { + return fmt.Sprintf("

%v

%v

By %v, %v", p.Title, p.Body, p.Author, p.PostDate.Format(human_time_layout)) +} + +func GetPost(con *sql.DB, id int) Post { + var err error + row := con.QueryRow("SELECT title, body, author, date FROM post WHERE id = ?", id) + + if err != nil { + log.Printf("Error preparing fetch statement: %v", err) + } + + var date string + post := new(Post) + err = row.Scan(&post.Title, &post.Body, &post.Author, date) + + post.PostDate, err = time.Parse(time_layout, date) + post.isSaved = true + + if err != nil { + log.Printf("Error parsing date: %v \n - got %v", err, date) + } + + return *post +} + +func checkIndex(index []int, idx int) bool { + result := false + + for count := 0; count < len(index); count++ { + if index[count] == idx { + result = true + break + } + } + + return result +} + +func GetPostList(con *sql.DB, position, limit int) []Post { + var err error + conn := *con + posts := make([]Post, 0) + + rows, err := conn.Query("SELECT id, title, body, author, date FROM post LIMIT ?,?", position, limit) + + if err != nil { + log.Print(err) + } + + defer rows.Close() + + var post *Post + var date string + index := make([]int, 0) + + for rows.Next() { + post = new(Post) + err := rows.Scan(&post.ID, &post.Title, &post.Body, &post.Author, &date) + + post.PostDate, err = time.Parse(time_layout, date) + post.isSaved = true + + if err != nil { + log.Printf("Error parsing date: %v \n - got %v", err, date) + } + + if !checkIndex(index, post.ID) { + posts = append(posts, *post) + index = append(index, post.ID) + } + } + + err = rows.Err() + + if err != nil { + log.Print(err) + } + + return posts +} diff --git a/src/server.go b/src/server.go new file mode 100644 index 0000000..f4ebcdc --- /dev/null +++ b/src/server.go @@ -0,0 +1,79 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/labstack/echo" + mw "github.com/labstack/echo/middleware" + + "database/sql" + _ "github.com/go-sql-driver/mysql" + + "data" +) + +var con *sql.DB + +func createPosts(con *sql.DB) { + var post1, post2, post3 data.Post + + post1.Title = "Post 1" + post1.Body = "

Lorem ipsum, dolor sit amet

" + post1.Author = "Reset Reboot" + + post1.Save(con) + + post2.Title = "Echo, Golang framework" + post2.Body = "

Parapapapapa, the rapper, wawawawa

" + post2.Author = "Reset Reboot" + + post2.Save(con) + + post3.Title = "Python still rocks, anyway" + post3.Body = "

import antigravity
# Fly!

" + post3.Author = "Reset Reboot" + + post3.Save(con) +} + +// Handler +func hello(c *echo.Context) error { + var text, temp string + + posts := data.GetPostList(con, 0, 10) + + for _, post := range posts { + temp = fmt.Sprintf("%v", post) + text = text + "
" + temp + } + + return c.HTML(http.StatusOK, "

Blog posts:

" + text + "") +} + +func main() { + var err error + // Echo instance + e := echo.New() + e.SetDebug(true) + + // Middleware + e.Use(mw.Logger()) + e.Use(mw.Recover()) + + con, err = sql.Open("mysql", "golang:golang@tcp(localhost:3306)/golang") + if err != nil { + log.Fatal(err) + } + + data.InitDatabase(con) + + // createPosts(con) + + // Routes + e.Get("/", hello) + + // Start server + e.Run(":1323") +}