Add html viewing
This commit is contained in:
145
cethttp/cethttp.go
Normal file
145
cethttp/cethttp.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package cethttp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/senaduka/cetvorke/database"
|
||||
)
|
||||
|
||||
func StartServer(waitgroup *sync.WaitGroup) {
|
||||
defer waitgroup.Done()
|
||||
|
||||
r := mux.NewRouter()
|
||||
|
||||
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)
|
||||
|
||||
err := http.ListenAndServe(":8080", 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))
|
||||
}
|
||||
88
cethttp/template.go
Normal file
88
cethttp/template.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package cethttp
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const html5Template = `
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>IslamBosna</title>
|
||||
<style>
|
||||
html, body {
|
||||
background-color: #171714;
|
||||
color: #f9f5c3;
|
||||
font-family: sans-serif;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
header {
|
||||
width: 100%;
|
||||
padding-top: 0.5em;
|
||||
padding-bottom: 0.5em;
|
||||
font-family: sans-serif;
|
||||
font-size: 3em;
|
||||
background-color: #0f1115;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
header div, content div {
|
||||
max-width: 800px;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
a:link, a:visited {
|
||||
color: #f9f5c3;
|
||||
}
|
||||
header a:link {
|
||||
color: #f9f5c3;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
header a:visited {
|
||||
color: #f9f5c3;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #e6fbfb;
|
||||
}
|
||||
</style>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<header>
|
||||
<div>
|
||||
☙ <a href="/">IslamBosna</a>
|
||||
</div>
|
||||
</header>
|
||||
<content>
|
||||
<div>
|
||||
{{.}}
|
||||
</div>
|
||||
<div>
|
||||
<br><br>
|
||||
<h2>Ostalo</h2>
|
||||
<a href="/a/">Arhiva</a><br>
|
||||
<a href="gemini://www.islambosna.ba/">Gemini stranica</a>
|
||||
</div>
|
||||
</content>
|
||||
<footer>
|
||||
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
func html5Page(html string) string {
|
||||
t1 := template.New("html5Template")
|
||||
t1, _ = t1.Parse(html5Template)
|
||||
result := new(strings.Builder)
|
||||
// execute template with html as data into result string io writer stream
|
||||
t1.Execute(result, template.HTML(html))
|
||||
return result.String()
|
||||
}
|
||||
Reference in New Issue
Block a user