forked from OrgGo/Tasks
removed dependency on httprouter
This commit is contained in:
parent
564f8ad878
commit
b7d14fbcbb
224
main.go
224
main.go
|
@ -5,12 +5,13 @@ package main
|
||||||
* License: MIT
|
* License: MIT
|
||||||
**/
|
**/
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/thewhitetulip/Tasks/db"
|
||||||
"github.com/thewhitetulip/Tasks/viewmodels"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -25,13 +26,14 @@ var templates *template.Template
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
defer viewmodels.Close()
|
defer db.Close()
|
||||||
var allFiles []string
|
var allFiles []string
|
||||||
files, err := ioutil.ReadDir("./templates")
|
templatesDir := "./public/templates/"
|
||||||
|
files, err := ioutil.ReadDir(templatesDir)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
filename := file.Name()
|
filename := file.Name()
|
||||||
if strings.HasSuffix(filename, ".html") {
|
if strings.HasSuffix(filename, ".html") {
|
||||||
allFiles = append(allFiles, "./templates/"+filename)
|
allFiles = append(allFiles, templatesDir+filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,113 +48,146 @@ func main() {
|
||||||
searchTemplate = templates.Lookup("search.html")
|
searchTemplate = templates.Lookup("search.html")
|
||||||
completedTemplate = templates.Lookup("completed.html")
|
completedTemplate = templates.Lookup("completed.html")
|
||||||
|
|
||||||
router := httprouter.New()
|
http.HandleFunc("/", ShowAllTasksFunc)
|
||||||
router.GET("/", ShowAllTasks)
|
http.HandleFunc("/complete/", CompleteTaskFunc)
|
||||||
router.GET("/complete/:id", CompleteTask)
|
http.HandleFunc("/delete/", DeleteTaskFunc)
|
||||||
router.GET("/delete/:id", DeleteTask)
|
http.HandleFunc("/deleted/", ShowTrashTaskFunc)
|
||||||
router.GET("/deleted/", ShowTrashTask)
|
http.HandleFunc("/trash/", TrashTaskFunc)
|
||||||
router.GET("/trash/:id", TrashTask)
|
http.HandleFunc("/edit/", EditTaskFunc)
|
||||||
router.GET("/edit/:id", EditTask)
|
http.HandleFunc("/completed/", ShowCompleteTasksFunc)
|
||||||
router.GET("/complete/", ShowCompleteTasks)
|
http.HandleFunc("/restore/", RestoreTaskFunc)
|
||||||
router.GET("/restore/:id", RestoreTask)
|
http.HandleFunc("/add/", AddTaskFunc)
|
||||||
router.POST("/add/", AddTask)
|
http.HandleFunc("/update/", UpdateTaskFunc)
|
||||||
router.POST("/update/", UpdateTask)
|
http.HandleFunc("/search/", SearchTaskFunc)
|
||||||
router.POST("/search/", SearchTask)
|
//http.HandleFunc("/static/", ServeStaticFunc)
|
||||||
router.NotFound = http.FileServer(http.Dir("public"))
|
http.Handle("/static/", http.FileServer(http.Dir("public")))
|
||||||
fmt.Println("running on 8080")
|
fmt.Println("running on 8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", router))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowAllTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
|
||||||
context := viewmodels.GetTasks("pending") //true when you want non deleted notes
|
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
homeTemplate.Execute(w, context)
|
if r.Method == "GET" {
|
||||||
}
|
context := db.GetTasks("pending") //true when you want non deleted notes
|
||||||
|
homeTemplate.Execute(w, context)
|
||||||
func ShowTrashTask(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
|
||||||
context := viewmodels.GetTasks("trashed") //false when you want deleted notes
|
|
||||||
deletedTemplate.Execute(w, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SearchTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
||||||
r.ParseForm()
|
|
||||||
query := r.Form.Get("query")
|
|
||||||
context := viewmodels.SearchTask(query)
|
|
||||||
searchTemplate.Execute(w, context)
|
|
||||||
}
|
|
||||||
|
|
||||||
func AddTask(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
||||||
r.ParseForm()
|
|
||||||
title := r.Form.Get("title")
|
|
||||||
content := r.Form.Get("content")
|
|
||||||
truth := viewmodels.AddTask(title, content)
|
|
||||||
if truth == true {
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowCompleteTasks(w http.ResponseWriter, r *http.Request, parm httprouter.Params) {
|
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
|
||||||
context := viewmodels.GetTasks("complete") //false when you want finished notes
|
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
completedTemplate.Execute(w, context)
|
if r.Method == "GET" {
|
||||||
}
|
context := db.GetTasks("trashed") //false when you want deleted notes
|
||||||
|
deletedTemplate.Execute(w, context)
|
||||||
func EditTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
|
||||||
id, err := strconv.Atoi(param.ByName("id"))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
} else {
|
|
||||||
task := viewmodels.GetTaskById(id)
|
|
||||||
editTemplate.Execute(w, task)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CompleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
//SearchTaskFunc is used to handle the /search/ url, handles the search function
|
||||||
id, err := strconv.Atoi(param.ByName("id"))
|
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
if r.Method == "POST" {
|
||||||
fmt.Println(err)
|
r.ParseForm()
|
||||||
|
query := r.Form.Get("query")
|
||||||
|
context := db.SearchTask(query)
|
||||||
|
searchTemplate.Execute(w, context)
|
||||||
} else {
|
} else {
|
||||||
viewmodels.CompleteTask(id)
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
||||||
id := param.ByName("id")
|
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if id == "all" {
|
if r.Method == "POST" {
|
||||||
viewmodels.DeleteAll()
|
r.ParseForm()
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
title := r.Form.Get("title")
|
||||||
} else {
|
content := r.Form.Get("content")
|
||||||
id, err := strconv.Atoi(id)
|
truth := db.AddTask(title, content)
|
||||||
if err != nil {
|
if truth != nil {
|
||||||
fmt.Println(err)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
} else {
|
} else {
|
||||||
viewmodels.DeleteTask(id)
|
fmt.Println(err)
|
||||||
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TrashTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
|
||||||
id, err := strconv.Atoi(param.ByName("id"))
|
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "GET" {
|
||||||
|
context := db.GetTasks("complete") //false when you want finished notes
|
||||||
|
completedTemplate.Execute(w, context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
|
||||||
|
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("deleting ", id)
|
task := db.GetTaskById(id)
|
||||||
viewmodels.TrashTask(id)
|
editTemplate.Execute(w, task)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
|
||||||
|
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "GET"{
|
||||||
|
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
err := db.CompleteTask(id)
|
||||||
|
if err!=nil{
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Println("redirecting to home")
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//DeleteTaskFunc is used to
|
||||||
|
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "GET"{
|
||||||
|
id := r.URL.Path[len("/delete/"):]
|
||||||
|
if id == "all" {
|
||||||
|
db.DeleteAll()
|
||||||
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
} else {
|
||||||
|
id, err := strconv.Atoi(id)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
db.DeleteTask(id)
|
||||||
|
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TrashTaskFunc is used to populate the "/trash/" URL
|
||||||
|
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
} else {
|
||||||
|
db.TrashTask(id)
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func RestoreTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
|
||||||
id, err := strconv.Atoi(param.ByName("id"))
|
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
} else {
|
} else {
|
||||||
viewmodels.RestoreTask(id)
|
db.RestoreTask(id)
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTask(w http.ResponseWriter, r *http.Request, param httprouter.Params) {
|
//UpdateTaskFunc is used to update a task, handes "/update/" URL
|
||||||
|
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
id, err := strconv.Atoi(r.Form.Get("id"))
|
id, err := strconv.Atoi(r.Form.Get("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -160,6 +195,33 @@ func UpdateTask(w http.ResponseWriter, r *http.Request, param httprouter.Params)
|
||||||
}
|
}
|
||||||
title := r.Form.Get("title")
|
title := r.Form.Get("title")
|
||||||
content := r.Form.Get("content")
|
content := r.Form.Get("content")
|
||||||
viewmodels.UpdateTask(id, title, content)
|
db.UpdateTask(id, title, content)
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ServeStaticFunc is used to serve static files
|
||||||
|
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"
|
||||||
|
} else if strings.HasSuffix(path, ".png") {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@
|
||||||
<!-- class="active" -->
|
<!-- class="active" -->
|
||||||
</li>
|
</li>
|
||||||
<li class="sidebar-item">
|
<li class="sidebar-item">
|
||||||
<a href="/complete/"><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
|
<a href="/completed/"><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
|
||||||
</li>
|
</li>
|
||||||
<li class="sidebar-item">
|
<li class="sidebar-item">
|
||||||
<a href="/deleted/"><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
|
<a href="/deleted/"><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
|
||||||
|
|
Loading…
Reference in New Issue