127 lines
2.6 KiB
Go
127 lines
2.6 KiB
Go
package cethttp
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/senaduka/cetvorke/database"
|
|
)
|
|
|
|
func indexHandler(w http.ResponseWriter, r *http.Request) {
|
|
links, err := database.GetRecentLinks()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := "<h1>IslamBosna</h1>\n<h2>Najnoviji članci</h2>\n"
|
|
|
|
for _, link := range links {
|
|
response += "<p>" + link.HTMLLink() + "</p>\n"
|
|
}
|
|
|
|
response = html5Page(response)
|
|
|
|
w.Write([]byte(response))
|
|
}
|
|
|
|
func postHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
fmt.Println(vars)
|
|
postID, err := strconv.Atoi(vars["postID"])
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
post, err := database.GetPost(postID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := post.HTMLPage()
|
|
|
|
response = html5Page(response)
|
|
|
|
w.Write([]byte(response))
|
|
}
|
|
|
|
func archiveHandler(w http.ResponseWriter, r *http.Request) {
|
|
links, title, err := database.GetAllYearLinks()
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := "<h1>" + title + "</h1>\n"
|
|
|
|
for _, link := range links {
|
|
response += "<p>" + link.HTMLArchiveLink() + "</p>\n"
|
|
}
|
|
|
|
response = html5Page(response)
|
|
|
|
w.Write([]byte(response))
|
|
}
|
|
|
|
func yearHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
year, err := strconv.Atoi(vars["year"])
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
links, title, err := database.GetMonthLinksByYear(year)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := "<h1>" + title + "</h1>\n"
|
|
|
|
for _, link := range links {
|
|
response += "<p>" + link.HTMLArchiveMonthLink() + "</p>\n"
|
|
}
|
|
|
|
response = html5Page(response)
|
|
|
|
w.Write([]byte(response))
|
|
}
|
|
|
|
func monthHandler(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
year, err := strconv.Atoi(vars["year"])
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
month, err := strconv.Atoi(vars["month"])
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
links, title, err := database.GetLinksByMonth(year, month)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
response := "<h1>" + title + "</h1>\n"
|
|
|
|
for _, link := range links {
|
|
response += "<p>" + link.HTMLLink() + "</p>\n"
|
|
}
|
|
|
|
response = html5Page(response)
|
|
w.Write([]byte(response))
|
|
}
|