Editor half way through

This commit is contained in:
Senad Uka
2023-08-28 13:42:58 +02:00
parent 5c229d5e42
commit a3187bdcc1
10 changed files with 1272 additions and 122 deletions

View File

@@ -1,145 +1,41 @@
package cethttp
import (
"fmt"
"embed"
"io/fs"
"log"
"net/http"
"strconv"
"sync"
"github.com/gorilla/mux"
"github.com/senaduka/cetvorke/database"
)
//go:embed static/*
var staticFiles embed.FS
func StartServer(waitgroup *sync.WaitGroup) {
defer waitgroup.Done()
r := mux.NewRouter()
// Serve embedded static files
staticFS, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal("error:", err)
}
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
r.HandleFunc("/p/{postID}/{slug}", postHandler)
r.HandleFunc("/", indexHandler)
r.HandleFunc("/a/", archiveHandler)
r.HandleFunc("/y/{year}/", yearHandler)
r.HandleFunc("/y/{year}/m/{month}/", monthHandler)
r.HandleFunc("/editor/e/{postID}", editHandler)
r.HandleFunc("/editor/n/", newHandler)
r.HandeeFunc("/editor", editorHandler)
err := http.ListenAndServe(":8018", r)
err = http.ListenAndServe(":8018", r)
if err != nil {
log.Fatal("error:", err)
}
}
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))
}