package main
import (
"fmt"
"log"
"net/http"
"encoding/json"
"github.com/gorilla/mux"
)
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 := []Article{
Article{Title: "Hello", Desc: "Article Description", Content: "Article Content"},
Article{Title: "Hello 22", Desc: "Article Description", Content: "Article Content"},
}
fmt.Println("Endpoint Hit: All Articles Endpoint")
json.NewEncoder(w).Encode(Articles)
}
func homePage(w http.ResponseWriter, r *http.Request ) {
fmt.Fprintf(w, "Homepage Endpoint Hit")
}
func testPostArticle(w http.ResponseWriter, r *http.Request ) {
fmt.Fprintf(w, "test post endpoint worked")
}
func handleRequest() {
myRouter :=mux.NewRouter().StrictSlash(true)
myRouter.HandleFunc("/", homePage);
myRouter.HandleFunc("/articles", allArticles).Methods("GET")
myRouter.HandleFunc("/articles", testPostArticle).Methods("POST")
log.Fatal(http.ListenAndServe(":8081", myRouter))
}
func main() {
handleRequest()
}