-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (124 loc) · 3.42 KB
/
main.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"dumbflix/database"
"dumbflix/pkg/mysql"
"dumbflix/routes"
"fmt"
"net/http"
"github.com/gorilla/mux"
// "encoding/json"
)
func main() {
// initial DB
mysql.DatabaseInit()
// run migration
database.RunMigration()
// On http (API)
r := mux.NewRouter()
routes.RouteInit(r.PathPrefix("/api/v1").Subrouter())
// r.HandleFunc("/", HomeTodos).Methods("GET")
// r.HandleFunc("/todos", FindTodos).Methods("GET")
// r.HandleFunc("/todo/{id}", GetTodo).Methods("GET")
// r.HandleFunc("/todo", CreateTodo).Methods("POST")
// r.HandleFunc("/todo/{id}", UpdateTodo).Methods("PATCH")
// r.HandleFunc("/todo/{id}", DeleteTodo).Methods("DELETE")
fmt.Println("server running localhost:5000")
http.ListenAndServe("localhost:5000", r)
}
// type Todos struct {
// Id string `json:"id"`
// Title string `json:"title"`
// IsDone bool `json:"isDone"`
// }
// var todos = []Todos{
// {
// Id: "1",
// Title: "Cuci tangan",
// IsDone: true,
// },
// {
// Id: "2",
// Title: "Jaga jarak",
// IsDone: false,
// },
// }
// func HomeTodos(w http.ResponseWriter, r *http.Request){
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// w.Write([]byte("Hello World"))
// }
// func FindTodos(w http.ResponseWriter, r *http.Request){
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode(todos)
// }
// func GetTodo(w http.ResponseWriter, r *http.Request){
// w.Header().Set("Content-Type", "application/json")
// params := mux.Vars(r)
// id := params["id"]
// var todoData Todos
// var isGetTodo = false
// for _, todo := range todos {
// if id == todo.Id {
// isGetTodo = true
// todoData = todo
// }
// }
// if isGetTodo == false {
// w.WriteHeader(http.StatusNotFound)
// json.NewEncoder(w).Encode("ID: " + id + " not found")
// return
// }
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode(todoData)
// }
// func CreateTodo(w http.ResponseWriter, r *http.Request){
// var data Todos
// json.NewDecoder(r.Body).Decode(&data)
// todos = append(todos, data)
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode(todos)
// }
// func UpdateTodo(w http.ResponseWriter, r *http.Request){
// params := mux.Vars(r)
// id := params["id"]
// var data Todos
// var isGetTodo = false
// json.NewDecoder(r.Body).Decode(&data)
// for idx, todo := range todos {
// if id == todo.Id {
// isGetTodo = true
// todos[idx] = data
// }
// }
// if isGetTodo == false {
// w.WriteHeader(http.StatusNotFound)
// json.NewEncoder(w).Encode("ID: " + id + " not found")
// return
// }
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode(todos)
// }
// func DeleteTodo(w http.ResponseWriter, r *http.Request){
// params := mux.Vars(r)
// id := params["id"]
// var isGetTodo = false
// var index = 0
// for idx, todo := range todos {
// if id == todo.Id {
// isGetTodo = true
// index = idx
// }
// }
// if isGetTodo == false {
// w.WriteHeader(http.StatusNotFound)
// json.NewEncoder(w).Encode("ID: " + id + " not found")
// return
// }
// todos = append(todos[:index], todos[index+1:]...)
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// json.NewEncoder(w).Encode("ID: " + id + " delete success")
// }