added nsfw banner

This commit is contained in:
drevil 2024-05-08 23:04:13 -04:00
parent ed7ee74924
commit fcdb14701e
3 changed files with 96 additions and 27 deletions

View File

@ -2,19 +2,20 @@ package main
import (
"os"
"log"
"fmt"
"flag"
"errors"
"strconv"
"strings"
"math/rand"
"net/http"
"database/sql"
"html/template"
"path/filepath"
"log"
"fmt"
"time"
"flag"
"errors"
"strconv"
"strings"
"math/rand"
"net/http"
"database/sql"
"html/template"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"
)
var errlog *log.Logger = nil
@ -24,6 +25,8 @@ var options Options = Options{}
var NoSuchComicErr Err = Err{msg: "no such comic found"}
const nsfwOkExipires uint = 24 * 60 * 60
const dbSquema string = `
CREATE TABLE IF NOT EXISTS comic (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@ -81,6 +84,8 @@ type Context struct {
UserName string
SiteURL string
Message string
Rights string
Source template.HTML
}
type Err struct {
@ -199,9 +204,33 @@ func executeTemplate(w http.ResponseWriter, name string, data interface{}) error
return err
}
func getDefaultContext( c * Comic) Context {
return Context{
Comics: nil,
Comic: c,
Title: "Black Ram Comics",
Rights: fmt.Sprintf("© %d drevil. All rights reserved.", time.Now().Year()),
Source: "The source code for this website can be found at <a href=\"https://tildegit.org/drevil/comics\">tildegit.org/drevil/comics</a>",
}
}
func comicView(w http.ResponseWriter, r * http.Request) {
var err error
c := &Comic{}
context := getDefaultContext(c)
ck, err := r.Cookie("nsfw_ok")
if err != nil || ck == nil || ck.Value != "true" {
context.Title = "!! NSFW AHEAD !!"
err = executeTemplate(w, "nsfw_banner", &context)
if err != nil {
errlog.Println(err)
return404(w,r)
}
return
}
if len(r.URL.Path) > 1 && r.URL.Path[len(r.URL.Path)-1] == '/' {
r.URL.Path = r.URL.Path[len(r.URL.Path)-2:]
}
@ -223,13 +252,6 @@ func comicView(w http.ResponseWriter, r * http.Request) {
}
}()
c := &Comic{}
context := Context{
Comics: nil,
Comic: c,
Title: "Black Ram Comics",
}
i := 1
if len(path) > 0{
i, err = strconv.Atoi(path)
@ -238,7 +260,6 @@ func comicView(w http.ResponseWriter, r * http.Request) {
}
}
context.Current = i
comics, err := allComics()
if err != nil {
@ -282,6 +303,15 @@ func comicView(w http.ResponseWriter, r * http.Request) {
err = executeTemplate(w, "comic", &context)
}
func nsfwOk(w http.ResponseWriter, r * http.Request) {
http.SetCookie(w, &http.Cookie{
Name: "nsfw_ok",
Value: "true",
Expires: time.Now().Add(time.Duration(nsfwOkExipires) * time.Second).Truncate((time.Second)),
})
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func allView(w http.ResponseWriter, r * http.Request) {
var err error
@ -294,10 +324,8 @@ func allView(w http.ResponseWriter, r * http.Request) {
}
} ()
context := Context{
Comics: nil,
Title: "All Issues",
}
context := getDefaultContext(nil)
context.Title = "All Issues"
comics, err := allComics()
if err != nil {
@ -495,6 +523,7 @@ func main() {
// views
http.HandleFunc("/", comicView)
http.HandleFunc("/nsfw_ok", nsfwOk)
http.HandleFunc("/latest", latestView)
http.HandleFunc("/first", firstView)
http.HandleFunc("/random", randomView)

View File

@ -84,8 +84,8 @@
{{ end }}
<footer>
<ul>
<li>© 2023 drevil. All rights reserved.</li>
<li>The source code for this website can be found at <a href="https://tildegit.org/drevil/comics">tildegit.org/drevil/comics</a></li>
<li>{{ .Rights }}</li>
<li>{{ .Source }}</li>
</ul>
</footer>
</div>

View File

@ -0,0 +1,40 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="/image/png" href="/static/img/favicon.png"/>
<link rel="stylesheet" href="/static/css/footer.css">
<link rel="stylesheet" href="/static/css/comic.css">
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/bar.css">
<link rel="stylesheet" href="/static/css/top.css">
<link rel="stylesheet" href="/static/css/all.css">
<title>{{ .Title }}</title>
</head>
<body>
<div class="top">
<div class="title">
<a href="/">
<h1>{{ .Title }}</h1>
</a>
</div>
<div class="bar">
<ul>
<li><a href="/nsfw_ok">LET ME IN!</a></li>
<li><a href="https://spongebob.fandom.com/wiki/Weenie_Hut_Jr%27s">GET ME OUT OF HERE!</a></li>
</ul>
</div>
</div>
<div class="footer">
<footer>
<ul>
<li>{{ .Rights }}</li>
<li>{{ .Source }}</li>
</ul>
</footer>
</div>
</body>
</html>