50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
//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 testPostArticles(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintf(w, "Test POST endpoint worker")
|
|
}
|
|
|
|
func handleRequests() {
|
|
|
|
myRouter := mux.NewRouter().StrictSlash(true)
|
|
myRouter.HandleFunc("/", homePage)
|
|
myRouter.HandleFunc("/articles", allArticles).Methods("GET") //obsługa tylko GET
|
|
myRouter.HandleFunc("/articles", testPostArticles).Methods("POST") // -,- POST; wymagana nowa nazwa funkcji; ale ten sam url co wyzej ^
|
|
|
|
log.Fatal(http.ListenAndServe(":8081", myRouter))
|
|
}
|
|
|
|
func main() {
|
|
handleRequests()
|
|
}
|