added views hash map, added getComicNumberFromURL
This commit is contained in:
parent
fcdb14701e
commit
51d1d4a3f8
115
comics.go
115
comics.go
|
@ -214,23 +214,31 @@ func getDefaultContext( c * Comic) Context {
|
|||
}
|
||||
}
|
||||
|
||||
func getComicNumberFromURL(r * http.Request, prefix string) (int, error) {
|
||||
var err error
|
||||
|
||||
path := strings.TrimPrefix(r.URL.Path, prefix)
|
||||
if len(path) >= 5 {
|
||||
path = path[:5]
|
||||
}
|
||||
|
||||
i := 1
|
||||
if len(path) > 0 {
|
||||
i, err = strconv.Atoi(path)
|
||||
if err != nil {
|
||||
return i, err
|
||||
}
|
||||
}
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
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:]
|
||||
}
|
||||
|
@ -240,10 +248,6 @@ func comicView(w http.ResponseWriter, r * http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if len(path) >= 5 {
|
||||
path = path[:5]
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err != nil {
|
||||
errlog.Println(err)
|
||||
|
@ -251,15 +255,23 @@ func comicView(w http.ResponseWriter, r * http.Request) {
|
|||
return
|
||||
}
|
||||
}()
|
||||
|
||||
i := 1
|
||||
if len(path) > 0{
|
||||
i, err = strconv.Atoi(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
i, err := getComicNumberFromURL(r, "/")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ck, err := r.Cookie("nsfw_ok")
|
||||
if err != nil || ck == nil || ck.Value != "true" {
|
||||
context.Title = "!! NSFW AHEAD !!"
|
||||
context.Next = i
|
||||
err = executeTemplate(w, "nsfw_banner", &context)
|
||||
if err != nil {
|
||||
errlog.Println(err)
|
||||
return404(w,r)
|
||||
}
|
||||
return
|
||||
}
|
||||
context.Current = i
|
||||
|
||||
comics, err := allComics()
|
||||
if err != nil {
|
||||
|
@ -283,6 +295,10 @@ func comicView(w http.ResponseWriter, r * http.Request) {
|
|||
}
|
||||
context.Comic = c
|
||||
context.Title = fmt.Sprintf("Black Ram Comics: %s", c.Title)
|
||||
} else if i != 1 {
|
||||
return404(w,r)
|
||||
err = nil
|
||||
return
|
||||
} else {
|
||||
context.Comic = nil
|
||||
}
|
||||
|
@ -308,8 +324,15 @@ func nsfwOk(w http.ResponseWriter, r * http.Request) {
|
|||
Name: "nsfw_ok",
|
||||
Value: "true",
|
||||
Expires: time.Now().Add(time.Duration(nsfwOkExipires) * time.Second).Truncate((time.Second)),
|
||||
Path: "/",
|
||||
})
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
|
||||
path := "/"
|
||||
i, err := getComicNumberFromURL(r, "/nsfw_ok/")
|
||||
if err == nil {
|
||||
path = fmt.Sprintf("/%d", i)
|
||||
}
|
||||
http.Redirect(w, r, path, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func allView(w http.ResponseWriter, r * http.Request) {
|
||||
|
@ -506,8 +529,8 @@ func main() {
|
|||
log.Println(fmt.Sprintf("using media path \"%s\"", options.MediaPath))
|
||||
fs = http.FileServer(http.Dir(options.MediaPath))
|
||||
http.Handle("/media/", http.StripPrefix("/media/", fs))
|
||||
|
||||
|
||||
|
||||
|
||||
// errors
|
||||
http.HandleFunc("/401", return401)
|
||||
http.HandleFunc("/404", return404)
|
||||
|
@ -522,14 +545,38 @@ func main() {
|
|||
}
|
||||
|
||||
// views
|
||||
http.HandleFunc("/", comicView)
|
||||
http.HandleFunc("/nsfw_ok", nsfwOk)
|
||||
http.HandleFunc("/latest", latestView)
|
||||
http.HandleFunc("/first", firstView)
|
||||
http.HandleFunc("/random", randomView)
|
||||
http.HandleFunc("/about", firstView)
|
||||
http.HandleFunc("/all", allView)
|
||||
http.HandleFunc("/blog", firstView)
|
||||
views := map[string]interface{} {
|
||||
"/": comicView,
|
||||
"nsfw_ok": nsfwOk,
|
||||
"latest": latestView,
|
||||
"first": firstView,
|
||||
"random": randomView,
|
||||
"about": firstView,
|
||||
"all": allView,
|
||||
"blog": firstView,
|
||||
};
|
||||
indexView := func(w http.ResponseWriter, r * http.Request) {
|
||||
path := r.URL.Path
|
||||
|
||||
if path[0] == '/' && len(path) > 1 {
|
||||
path = path[1:]
|
||||
i := 0
|
||||
for ; i < len(path) && path[i] != '/'; i++ {
|
||||
continue
|
||||
}
|
||||
if i < len(path) {
|
||||
path = path[:i]
|
||||
}
|
||||
}
|
||||
|
||||
f, found := views[path]
|
||||
if !found {
|
||||
f, _ = views["/"]
|
||||
}
|
||||
|
||||
f.(func(w http.ResponseWriter, r * http.Request))(w, r)
|
||||
}
|
||||
http.HandleFunc("/", indexView)
|
||||
|
||||
uri := fmt.Sprintf("%s:%d", options.Address, options.Port)
|
||||
log.Println("listening to http://" + uri)
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
|
||||
<div class="bar">
|
||||
<ul>
|
||||
<li><a href="/nsfw_ok">LET ME IN!</a></li>
|
||||
<li><a href="/nsfw_ok/{{ .Next }}">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>
|
||||
|
|
Loading…
Reference in New Issue