در قسمت قبل با استفاده از کتابخانه net/http
یک api ساده ایجاد کردیم.
در این قسمت به پیاده سازی یک سرور http برای مدیریت لیست TODO های خود میپردازیم و از چهار متد GET, POST, DELET and PATCH استفاده میکنیم.
در این پروژه از دیتابیس استفاده نمیشود. روش ذخیره سازی اطلاعات درون متغییر هاست دلیل استفاده نکردن از یک دیتابیس تمرکز این قسمت روی کتابخانه و پروتکل http است.
در ادامه به ایجاد سرور خود میپردازیم.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
// todo struct with json tags
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
// just work as a DB for us!
var todos []Todo
// get all the items in the Todo list and write it. GET
func getTodos(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(todos)
}
// append a new data in array. POST
func addTodo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var todo Todo
json.NewDecoder(r.Body).Decode(&todo)
// NOTE: this isn't a good way to set ids in production!
todo.ID = len(todos) + 1
todos = append(todos, todo)
json.NewEncoder(w).Encode(todo)
}
// change the todo status. PATCH
func updateTodo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var todo Todo
json.NewDecoder(r.Body).Decode(&todo)
for i, t := range todos {
if t.ID == todo.ID {
todos[i].Status = todo.Status
json.NewEncoder(w).Encode(todos[i])
return
}
}
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"message": "TODO not found"})
}
// remove the TODO from array. DELETE
func deleteTodo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var todo Todo
json.NewDecoder(r.Body).Decode(&todo)
for i, t := range todos {
if t.ID == todo.ID {
todos = append(todos[:i], todos[i+1:]...)
json.NewEncoder(w).Encode(map[string]string{"message": "TODO deleted"})
return
}
}
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"message": "TODO not found"})
}
func main() {
// set routes
http.HandleFunc("/todos", getTodos)
http.HandleFunc("/todos/add", addTodo)
http.HandleFunc("/todos/update", updateTodo)
http.HandleFunc("/todos/delete", deleteTodo)
// start server
fmt.Println("Server starting at port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
بعد از اتمام نوشتن سرور با استفاده از یک کلاینت http
مثل postman سرور خود را به روش زیر تست میکنیم:
نکته: شما می توانید کلاینت خود را خودتان با استفاده از اموزش کلاینت http در قسمت های دیگر بنویسید!
add todo #
endpoint: localhost:8080/todos/add
method:POST
request:
{
"title":"todo1 test"
}
response:
{
"id": 1,
"title": "todo1 test",
"status": false
}
get todo’s #
endpoint: localhost:8080/todos
method:GET
request:
response:
[
{
"id": 1,
"title": "todo1 test",
"status": false
},
//...
]
update todo #
endpoint: localhost:8080/todos/update
method:PATCH
request:
{
"id":1,
"status":true
}
response:
{
"id": 1,
"title": "todo1 test",
"status": true
}
delete todo #
endpoint: localhost:8080/todos/delete
method:DELETE
request:
{
"id":1
}
response:
{
"message": "TODO deleted"
}