-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
174 lines (147 loc) · 4.22 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/mattn/go-sqlite3"
)
type FeatureSwitch struct {
ID int `json:"id"`
Name string `json:"name"`
Value bool `json:"value"`
}
var db *sql.DB
func main() {
var err error
db, err = sql.Open("sqlite3", "./feature_switches.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
createTable()
http.HandleFunc("/", handleResources)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func createTable() {
sqlStmt := `
CREATE TABLE IF NOT EXISTS feature_switches (
id INTEGER PRIMARY KEY,
name TEXT,
value INTEGER
);
`
_, err := db.Exec(sqlStmt)
if err != nil {
log.Fatal(err)
}
}
func handleFeatureSwitchs(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
getFeatureSwitchs(w, r)
case "POST":
createFeatureSwitch(w, r)
case "PUT":
updateFeatureSwitch(w, r)
case "DELETE":
deleteFeatureSwitch(w, r)
default:
http.Error(w, "Invalid request method.", http.StatusMethodNotAllowed)
}
}
func getFeatureSwitchs(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT id, name, value FROM feature_switches")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
resources := []FeatureSwitch{}
for rows.Next() {
var id int
var name string
err := rows.Scan(&id, &name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resources = append(resources, FeatureSwitch{ID: id, Name: name})
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(resources)
}
func createResource(w http.ResponseWriter, r *http.Request) {
var resource FeatureSwitch
err := json.NewDecoder(r.Body).Decode(&resource)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := db.Exec("INSERT INTO resources(name) VALUES(?)", resource.Name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id, err := result.LastInsertId()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
resource.ID = int(id)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(resource)
}
func updateResource(w http.ResponseWriter, r *http.Request) {
var resource FeatureSwitch
err := json.NewDecoder(r.Body).Decode(&resource)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := db.Exec("UPDATE resources SET name = ? WHERE id = ?", resource.Name, id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
http.Error(w, "FeatureSwitch not found.", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
}
func deleteResource(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
result, err := db.Exec("DELETE FROM resources WHERE id = ?", id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
http.Error(w, "FeatureSwitch not found.", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
}