PracowniaProgramowania/backend/tut2a.go
2018-11-15 01:52:44 +01:00

40 lines
824 B
Go

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
//struktura artykuly
type Article struct {
Title string `json:"Title"`
Desc string `json:"desc"`
Content string `json:"content"`
}
type Articles []Article
func allArticles(w http.ResponseWriter, r *http.Request) {
articles := Articles{
Article{Title: "Test Title", Desc: "Test Description", Content: "Hello World"}, //json i szesceinne nawiasy!, przecinek na koniec
}
fmt.Println("Endpoint Hit: All Articles Endpoint")
json.NewEncoder(w).Encode(articles)
}
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Homepage Enpoint Hit")
}
func handleRequests() {
http.HandleFunc("/", homePage)
http.HandleFunc("/articles", allArticles)
log.Fatal(http.ListenAndServe(":8081", nil))
}
func main() {
handleRequests()
}