aboutsummaryrefslogtreecommitdiffstats
path: root/todolist/webapp.go
blob: dcece3a6ba96ea1152da0ed9011ec698ca1b25aa (plain)
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
package todolist

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/julienschmidt/httprouter"
)

const (
	VERSION = "0.5.2"
	S3URL   = "https://s3.amazonaws.com/todolist-local/" + VERSION
)

type Webapp struct {
	Router *httprouter.Router
}

func NewWebapp() *Webapp {
	return &Webapp{Router: setupRoutes()}
}

func (w *Webapp) Run() {
	log.Fatal(http.ListenAndServe(":7890", w.Router))
}

func setupRoutes() *httprouter.Router {
	router := httprouter.New()
	router.GET("/", IndexScaffold)
	router.OPTIONS("/todos", TodoOptions)
	router.GET("/todos", GetTodos)
	router.POST("/todos", SaveTodos)
	router.NotFound = http.HandlerFunc(RedirectScaffold)
	return router
}

func IndexScaffold(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	template := `
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="https://bootswatch.com/flatly/bootstrap.min.css">
    <title>Todolist</title>
    <link href="` + urlFor("main.css") + `" rel="stylesheet">
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript" src="` + urlFor("common.js") + `"></script>
    <script type="text/javascript" src="` + urlFor("main.js") + `"></script>
  </body>
</html>
	`
	fmt.Fprintf(w, template)
}

func RedirectScaffold(w http.ResponseWriter, r *http.Request) {
	template := `
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <link rel="stylesheet" href="https://bootswatch.com/flatly/bootstrap.min.css">
    <title>Todolist</title>
    <link href="` + urlFor("main.css") + `" rel="stylesheet">
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript" src="` + urlFor("common.js") + `"></script>
    <script type="text/javascript" src="` + urlFor("main.js") + `"></script>
  </body>
</html>
	`
	fmt.Fprintf(w, template)
}

func urlFor(file string) string {
	return S3URL + "/" + file
}

func RedirectToIndex(w http.ResponseWriter, r *http.Request) {
	http.Redirect(w, r, S3URL+r.URL.Path, 301)
}

func GetTodos(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	app := NewApp()
	app.Load()
	json, _ := json.Marshal(app.TodoList.Data)
	fmt.Fprintf(w, string(json))
}
func TodoOptions(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	w.Header().Set("Access-Control-Allow-Headers", "*")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	fmt.Fprintf(w, "")
}

func SaveTodos(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	w.Header().Set("Access-Control-Allow-Headers", "*")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	decoder := json.NewDecoder(r.Body)
	var todos []*Todo
	err := decoder.Decode(&todos)
	if err != nil {
		log.Fatal("encountered an error parsing json, ", err)
	}
	app := NewApp()
	app.TodoStore.Save(todos)
}