Designing our web app
The design phase is the most important in any software project as one small mistake in designing becomes very costly to fix that later. In most projects, there are a few solutions applied to mistakes in design, they are just glued together until it becomes impossible to go forward. The term is called technical debt.
Our webapp is going to be a todo list manager for multiple users. It'll have the following features:
Ability to add/delete/modify task.
Ability to mark tasks as complete/incomplete.
Display the tasks as a one column list.
Ability to switch modes between pending/completed/deleted tasks.
Display notifications like note added/deleted/archived.
Search for text and highlight the text in the search results page.
Login/Sign up.
The Design
Translating our design to API, we get the following.
URL | Method | Description |
---|---|---|
/add/ | POST | add new task |
/ | GET | show pending tasks |
/complete/ | GET | show completed tasks |
/deleted/ | GET | show deleted tasks |
/edit/ |
POST | edit post |
/edit/ |
GET | show the edit page |
/trash/ |
POST | trash post to recycle bin |
/delete/ |
POST | permanently delete post |
/complete/ |
POST | mark post as complete |
/login/ | POST | do the login |
/login/ | GET | show login page |
/logout/ | POST | log the user out |
/restore/ |
POST | restore that task |
/update/ |
POST | update task |
/change/ | GET | will allow changing password |
/register/ | GET | show the register page |
/register/ | POST | will add entries into database |
file ~/main/main.go
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/complete/", CompleteTaskFunc)
http.HandleFunc("/delete/", DeleteTaskFunc)
http.HandleFunc("/deleted/", ShowTrashTaskFunc)
http.HandleFunc("/trash/", TrashTaskFunc)
http.HandleFunc("/edit/", EditTaskFunc)
http.HandleFunc("/completed/", ShowCompleteTasksFunc)
http.HandleFunc("/restore/", RestoreTaskFunc)
http.HandleFunc("/add/", AddTaskFunc)
http.HandleFunc("/update/", UpdateTaskFunc)
http.HandleFunc("/search/", SearchTaskFunc)
http.HandleFunc("/login", GetLogin)
http.HandleFunc("/register", PostRegister)
http.HandleFunc("/admin", HandleAdmin)
http.HandleFunc("/add_user", PostAddUser)
http.HandleFunc("/change", PostChange)
http.HandleFunc("/logout", HandleLogout)
http.HandleFunc("/", ShowAllTasksFunc)
http.Handle("/static/", http.FileServer(http.Dir("public")))
log.Print("running on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
Create all functions we mentioned above and make the necessary changes as per one definition that we show below in this file.
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
var message string
if r.Method == "GET" {
message = "all pending tasks GET"
} else {
message = "all pending tasks POST"
}
w.Write([]byte(message))
}
After you create these functions run the server as below,
[Tasks] $ go build
[Tasks] $ ./Tasks
In your browser type localhost:8080
and type all these URLs and see what message you get.
Homework
Check the documentation for http.ResponseWriter
and http.Request
objects and get to know all the variables/functions/constants for http package and these two which we mentioned.