Last active
May 24, 2021 03:26
-
-
Save deep-diver/868668d9ad8a582c0888f5a88f84de6c to your computer and use it in GitHub Desktop.
RESTful API 서버 만들기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"sort" | |
"github.com/gorilla/mux" | |
) | |
type Student struct { | |
Id int | |
Name string | |
Age int | |
Score int | |
} | |
var students map[int]Student | |
var lastId int | |
type Students []Student | |
func (s Students) Len() int { | |
return len(s) | |
} | |
func (s Students) Swap(i, j int) { | |
s[i], s[j] = s[j], s[i] | |
} | |
func (s Students) Less(i, j int) bool { | |
return s[i].Id < s[j].Id | |
} | |
func MakeWebHandler() http.Handler { | |
mux := mux.NewRouter() | |
mux.HandleFunc("/students", GetStudentListHandler).Methods("GET") | |
students = make(map[int]Student) | |
students[1] = Student{1, "aaa", 16, 87} | |
students[2] = Student{2, "bbb", 18, 98} | |
lastId = 2 | |
return mux | |
} | |
func GetStudentListHandler(w http.ResponseWriter, r *http.Request) { | |
list := make(Students, 0) | |
for _, student := range students { | |
list = append(list, student) | |
} | |
sort.Sort(list) | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(list) | |
} | |
func main() { | |
http.ListenAndServe(":3000", MakeWebHandler()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"sort" | |
"github.com/gorilla/mux" | |
) | |
type Student struct { | |
Id int | |
Name string | |
Age int | |
Score int | |
} | |
var students map[int]Student | |
var lastId int | |
type Students []Student | |
func (s Students) Len() int { | |
return len(s) | |
} | |
func (s Students) Swap(i, j int) { | |
s[i], s[j] = s[j], s[i] | |
} | |
func (s Students) Less(i, j int) bool { | |
return s[i].Id < s[j].Id | |
} | |
func MakeWebHandler() http.Handler { | |
mux := mux.NewRouter() | |
mux.HandleFunc("/students", GetStudentListHandler).Methods("GET") | |
mux.HandleFunc("/student/{id:[0-9]+}", GetStudentHandler).Methods("GET") | |
students = make(map[int]Student) | |
students[1] = Student{1, "aaa", 16, 87} | |
students[2] = Student{2, "bbb", 18, 98} | |
lastId = 2 | |
return mux | |
} | |
func GetStudentHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
id, _ := strconv.Atoi(vars["id"]) | |
student, ok := students[id] | |
if !ok { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(student) | |
} | |
func GetStudentListHandler(w http.ResponseWriter, r *http.Request) { | |
list := make(Students, 0) | |
for _, student := range students { | |
list = append(list, student) | |
} | |
sort.Sort(list) | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(list) | |
} | |
func main() { | |
http.ListenAndServe(":3000", MakeWebHandler()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"sort" | |
"github.com/gorilla/mux" | |
"strconv" | |
) | |
type Student struct { | |
Id int | |
Name string | |
Age int | |
Score int | |
} | |
var students map[int]Student | |
var lastId int | |
type Students []Student | |
func (s Students) Len() int { | |
return len(s) | |
} | |
func (s Students) Swap(i, j int) { | |
s[i], s[j] = s[j], s[i] | |
} | |
func (s Students) Less(i, j int) bool { | |
return s[i].Id < s[j].Id | |
} | |
func MakeWebHandler() http.Handler { | |
mux := mux.NewRouter() | |
mux.HandleFunc("/students", GetStudentListHandler).Methods("GET") | |
mux.HandleFunc("/student/{id:[0-9]+}", GetStudentHandler).Methods("GET") | |
mux.HandleFunc("/student", PostStudentHandler).Methods("POST") | |
students = make(map[int]Student) | |
students[1] = Student{1, "aaa", 16, 87} | |
students[2] = Student{2, "bbb", 18, 98} | |
lastId = 2 | |
return mux | |
} | |
func PostStudentHandler(w http.ResponseWriter, r *http.Request) { | |
var student Student | |
err := json.NewDecoder(r.Body).Decode(&student) | |
if err != nil { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
lastId++ | |
student.Id = lastId | |
students[lastId] = student | |
w.WriteHeader(http.StatusCreated) | |
} | |
func GetStudentHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
id, _ := strconv.Atoi(vars["id"]) | |
student, ok := students[id] | |
if !ok { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(student) | |
} | |
func GetStudentListHandler(w http.ResponseWriter, r *http.Request) { | |
list := make(Students, 0) | |
for _, student := range students { | |
list = append(list, student) | |
} | |
sort.Sort(list) | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(list) | |
} | |
func main() { | |
http.ListenAndServe(":3000", MakeWebHandler()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"sort" | |
"github.com/gorilla/mux" | |
"strconv" | |
) | |
type Student struct { | |
Id int | |
Name string | |
Age int | |
Score int | |
} | |
var students map[int]Student | |
var lastId int | |
type Students []Student | |
func (s Students) Len() int { | |
return len(s) | |
} | |
func (s Students) Swap(i, j int) { | |
s[i], s[j] = s[j], s[i] | |
} | |
func (s Students) Less(i, j int) bool { | |
return s[i].Id < s[j].Id | |
} | |
func MakeWebHandler() http.Handler { | |
mux := mux.NewRouter() | |
mux.HandleFunc("/students", GetStudentListHandler).Methods("GET") | |
mux.HandleFunc("/student/{id:[0-9]+}", GetStudentHandler).Methods("GET") | |
mux.HandleFunc("/student", PostStudentHandler).Methods("POST") | |
mux.HandleFunc("/student/{id:[0-9]+}", DeleteStudentHandler).Methods("DELETE") | |
students = make(map[int]Student) | |
students[1] = Student{1, "aaa", 16, 87} | |
students[2] = Student{2, "bbb", 18, 98} | |
lastId = 2 | |
return mux | |
} | |
func DeleteStudentHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
id, _ := strconv.Atoi(vars["id"]) | |
student, ok := students[id] | |
if !ok { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
delete(students, id) | |
w.WriteHeader(http.StatusOK) | |
} | |
func PostStudentHandler(w http.ResponseWriter, r *http.Request) { | |
var student Student | |
err := json.NewDecoder(r.Body).Decode(&student) | |
if err != nil { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
lastId++ | |
student.Id = lastId | |
students[lastId] = student | |
w.WriteHeader(http.StatusCreated) | |
} | |
func GetStudentHandler(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
id, _ := strconv.Atoi(vars["id"]) | |
student, ok := students[id] | |
if !ok { | |
w.WriteHeader(http.StatusNotFound) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(student) | |
} | |
func GetStudentListHandler(w http.ResponseWriter, r *http.Request) { | |
list := make(Students, 0) | |
for _, student := range students { | |
list = append(list, student) | |
} | |
sort.Sort(list) | |
w.WriteHeader(http.StatusOK) | |
w.Header().Set("Content-Type", "application/json") | |
json.NewEncoder(w).Encode(list) | |
} | |
func main() { | |
http.ListenAndServe(":3000", MakeWebHandler()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"net/http" | |
"net/http/httptest" | |
"testing" | |
"strings" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestJsonHandler(t *testing.T) { | |
assert := assert.New(t) | |
res := httptest.NewRecorder() | |
req := httptest.NewRequest("GET", "/students", nil) | |
mux := MakeWebHandler() | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusOK, res.Code) | |
var list []Student | |
err := json.NewDecoder(res.Body).Decode(&list) | |
assert.Nil(err) | |
assert.Equal(2, len(list)) | |
assert.Equal("aaa", list[0].Name) | |
assert.Equal("bbb", list[1].Name) | |
} | |
func TestJsonHandler2(t *testing.T) { | |
assert := assert.New(t) | |
var student Student | |
mux := MakeWebHandler() | |
res := httptest.NewRecorder() | |
req := httptest.NewRequest("GET", "/student/1", nil) | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusOK, res.Code) | |
err := json.NewDecoder(res.Body).Decode(&student) | |
assert.Nil(err) | |
assert.Equal("aaa", student.Name) | |
res = httptest.NewRecorder() | |
req = httptest.NewRequest("GET", "/student/2", nil) | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusOK, res.Code) | |
err = json.NewDecoder(res.Body).Decode(&student) | |
assert.Nil(err) | |
assert.Equal("bbb", student.Name) | |
} | |
func TestJsonHandler3(t *testing.T) { | |
assert := assert.New(t) | |
var student Student | |
mux := MakeWebHandler() | |
res := httptest.NewRecorder() | |
req := httptest.NewRequest("POST", "/student", | |
strings.NewReader(`{"id":0, "Name":"ccc", "Age":15, "Score":78}`)) | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusCreated, res.Code) | |
res = httptest.NewRecorder() | |
req = httptest.NewRequest("GET", "/student/3", nil) | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusOK, res.Code) | |
err := json.NewDecoder(res.Body).Decode(&student) | |
assert.Nil(err) | |
assert.Equal("ccc", student.Name) | |
} | |
func TestJsonHandler4(t *testing.T) { | |
assert := assert.New(t) | |
mux := MakeWebHandler() | |
res := httptest.NewRecorder() | |
req := httptest.NewRequest("DELETE", "/student/1", nil) | |
mux.ServeHTTP(res, req) | |
assert.Equal(http.StatusOK, res.Code) | |
res = httptest.NewRecorder() | |
req = httptest.NewRequest("GET", "/students", nil) | |
mux.ServeHTTP(res, req) | |
assert.Equal(httptest.StatusOK, res.Code) | |
var list []Student | |
err := json.NewDecoder(res.Body).Decode(&list) | |
assert.Nil(err) | |
assert.Equal(1, len(list)) | |
assert.Equal("bbb", list[0].Name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment