TaskFlow/views/views.go

105 lines
2.7 KiB
Go
Raw Normal View History

2015-11-21 15:58:23 +08:00
package views
import (
"bufio"
"net/http"
"os"
"strings"
"text/template"
2016-01-18 10:28:08 +08:00
"time"
2016-02-01 21:45:04 +08:00
"github.com/thewhitetulip/Tasks/db"
2015-11-21 15:58:23 +08:00
)
var homeTemplate *template.Template
var deletedTemplate *template.Template
var completedTemplate *template.Template
var editTemplate *template.Template
var searchTemplate *template.Template
var templates *template.Template
2015-11-22 11:51:29 +08:00
var message string //message will store the message to be shown as notification
2015-11-21 15:58:23 +08:00
var err error
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("pending")
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
}
2016-01-18 10:28:08 +08:00
context.CSRFToken = "abcd"
2015-11-22 11:51:29 +08:00
message = ""
2016-01-18 09:02:15 +08:00
expiration := time.Now().Add(365 * 24 * time.Hour)
2016-01-18 10:28:08 +08:00
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
2016-01-18 09:02:15 +08:00
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("deleted")
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
2015-11-22 11:51:29 +08:00
if message != "" {
context.Message = message
message = ""
}
2015-11-21 15:58:23 +08:00
deletedTemplate.Execute(w, context)
2015-11-22 11:51:29 +08:00
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
2016-02-01 21:45:04 +08:00
context, err := db.GetTasks("completed")
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
2015-11-21 15:58:23 +08:00
completedTemplate.Execute(w, context)
2015-11-21 19:38:08 +08:00
} else {
2015-11-22 11:51:29 +08:00
message = "Method not allowed"
2015-11-21 19:38:08 +08:00
http.Redirect(w, r, "/", http.StatusFound)
2015-11-21 15:58:23 +08:00
}
}
//ServeStaticFunc is used to serve static files
//TODO: replace this with the http.FileServer
func ServeStaticFunc(w http.ResponseWriter, r *http.Request) {
path := "./public" + r.URL.Path
var contentType string
if strings.HasSuffix(path, ".css") {
contentType = "text/css"
} else if strings.HasSuffix(path, ".png") {
contentType = "image/png"
2016-01-07 22:55:56 +08:00
} else if strings.HasSuffix(path, ".js") {
2015-11-21 15:58:23 +08:00
contentType = "application/javascript"
} else {
contentType = "plain/text"
}
f, err := os.Open(path)
if err == nil {
defer f.Close()
w.Header().Add("Content Type", contentType)
br := bufio.NewReader(f)
br.WriteTo(w)
} else {
w.WriteHeader(404)
}
}