34 lines
847 B
Go
34 lines
847 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
type Task struct {
|
||
|
ID int `json:"id"`
|
||
|
Title string `json:"title"`
|
||
|
Description string `json:"description"`
|
||
|
Completed bool `json:"completed"`
|
||
|
}
|
||
|
|
||
|
var tasks = []Task{
|
||
|
{ID: 1, Title: "Task 1", Description: "Description of Task 1", Completed: false},
|
||
|
{ID: 2, Title: "Task 2", Description: "Description of Task 2", Completed: true},
|
||
|
{ID: 3, Title: "Task 3", Description: "Description of Task 3", Completed: false},
|
||
|
}
|
||
|
|
||
|
func getTasksHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
json.NewEncoder(w).Encode(tasks)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
http.HandleFunc("/api/tasks", getTasksHandler)
|
||
|
|
||
|
port := 8080
|
||
|
println("Server is running on port " + strconv.Itoa(port))
|
||
|
http.ListenAndServe(":"+strconv.Itoa(port), nil)
|
||
|
}
|