comics/comics.go

509 lines
11 KiB
Go
Raw Permalink Normal View History

2023-05-17 15:37:08 +00:00
package main
2023-05-17 06:41:21 +00:00
import (
"os"
2023-05-17 15:37:08 +00:00
"log"
"fmt"
2023-05-20 18:20:23 +00:00
"flag"
"errors"
"strconv"
"strings"
"math/rand"
2023-05-17 06:41:21 +00:00
"net/http"
"database/sql"
"html/template"
"path/filepath"
2023-05-17 15:37:08 +00:00
_ "github.com/mattn/go-sqlite3"
2023-05-17 06:41:21 +00:00
)
2023-06-14 03:49:34 +00:00
var errlog *log.Logger = nil
var db *sql.DB = nil
2023-05-20 18:20:23 +00:00
2023-06-14 03:49:34 +00:00
var options Options = Options{}
2023-05-20 18:20:23 +00:00
var NoSuchComicErr Err = Err{msg: "no such comic found"}
2023-05-17 06:41:21 +00:00
2023-05-19 19:56:28 +00:00
const dbSquema string = `
CREATE TABLE IF NOT EXISTS comic (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
datetime DATETIME DEFAULT CURRENT_TIMESTAMP,
title CHAR(50),
image CHAR(200),
description TEXT,
tags TEXT
);`
2023-05-20 18:20:23 +00:00
type Options struct {
DBPath string
MediaPath string
TemplatesPath string
Address string
Port int
Publish bool
Title string
ImagePath string
2023-05-21 19:58:29 +00:00
RunManager bool
UploadSize int64
2023-06-14 03:49:34 +00:00
SiteURL string
SecretPath string
2023-07-01 23:18:25 +00:00
Username string
Password string
CreateUser bool
2023-07-04 00:11:29 +00:00
ManagerRoot string
2023-05-20 18:20:23 +00:00
}
type Comic struct {
2023-06-14 03:49:34 +00:00
ID int
DateTime string
Title string
Image string
Description string
Tags string
2023-05-17 06:41:21 +00:00
}
2023-05-19 19:40:15 +00:00
type Context struct {
Comics []struct{
2023-06-14 03:49:34 +00:00
No int
Title string
Date string
Image string
}
2023-07-01 23:18:25 +00:00
Comic *Comic
Current int
Previous int
Combo int
Next int
First int
Last int
Title string
UserName string
SiteURL string
Message string
2023-05-19 19:40:15 +00:00
}
type Err struct {
msg string
}
2023-05-20 18:20:23 +00:00
func (o * Options) Parse() error {
flag.StringVar(&o.DBPath, "d", "./db.sqlite", "Sets path to database file")
flag.StringVar(&o.MediaPath, "m", "./media/", "Sets path to media directory")
2023-06-14 03:49:34 +00:00
flag.StringVar(&o.TemplatesPath, "t", "./templates/", "Sets path to templates directory")
2023-05-20 18:20:23 +00:00
flag.StringVar(&o.Address, "a", "127.0.0.1", "Defines the address the web server will listen to")
flag.IntVar(&o.Port, "p", 8080, "Defines the port the web server will listen to")
flag.BoolVar(&o.Publish, "u", false, "Creates new commics. Needs both -i and -l")
flag.StringVar(&o.Title, "l", "", "Title for new comic")
flag.StringVar(&o.ImagePath, "i", "", "Image path for new comic")
2023-05-21 19:58:29 +00:00
flag.BoolVar(&o.RunManager, "r", false, "Runs the content manager instead of the site")
flag.Int64Var(&o.UploadSize, "z", 10, "Max upload size for posts")
2023-05-22 16:25:33 +00:00
flag.StringVar(&o.SiteURL, "0", "https://comics.blackram.works", "URL of the site once deployed")
2023-06-14 03:49:34 +00:00
flag.StringVar(&o.SecretPath, "s", "./secret.key", "Sets path to secret key file")
2023-07-01 23:18:25 +00:00
flag.StringVar(&o.Username, "w", "", "Username for the new user")
flag.StringVar(&o.Password, "q", "", "Password for the new user")
flag.BoolVar(&o.CreateUser, "c", false, "Creates a new user. Needs -w and -q")
2023-07-04 00:11:29 +00:00
flag.StringVar(&o.ManagerRoot, "o", "manager", "Manager root page")
2023-06-14 03:49:34 +00:00
2023-05-20 18:20:23 +00:00
flag.Parse()
2023-07-01 23:18:25 +00:00
if o.CreateUser {
o.RunManager = true
}
if _, err := os.Stat(o.TemplatesPath); errors.Is(err, os.ErrNotExist) {
log.Fatal(err)
}
if _, err := os.Stat(o.MediaPath); errors.Is(err, os.ErrNotExist) {
2023-06-14 03:49:34 +00:00
err := os.Mkdir(o.MediaPath, os.ModePerm)
if err != nil {
log.Fatal(err)
}
}
2023-05-20 18:20:23 +00:00
return nil
}
2023-05-19 19:40:15 +00:00
func (e * Err) Error() string {
return e.msg
}
2023-05-19 19:56:28 +00:00
func (e * Err) With(i string) Err {
return Err{msg:fmt.Sprintf("%s with %s", e.msg, i)}
}
2023-05-19 19:40:15 +00:00
func (c *Comic) readRow(db * sql.Rows) error {
2023-05-19 19:56:28 +00:00
return db.Scan(&c.ID, &c.DateTime, &c.Title, &c.Image,
2023-05-17 06:41:21 +00:00
&c.Description, &c.Tags)
}
func getComic(id int) (*Comic, error) {
id -= 1
c := new(Comic)
2023-05-19 19:56:28 +00:00
nferr := NoSuchComicErr.With(fmt.Sprintf("id \"%d\"", id))
if id < 0 {
2023-05-19 19:56:28 +00:00
return c, &nferr
}
2023-05-19 07:47:04 +00:00
comics, err := allComics()
2023-05-19 19:40:15 +00:00
if err != nil {
return c, err
}
2023-05-19 19:40:15 +00:00
2023-05-19 19:56:28 +00:00
if len(comics) <= id {
return c, &nferr
2023-05-19 19:40:15 +00:00
}
2023-05-19 07:47:04 +00:00
*c = comics[id]
return c, err
}
func readRows(rows *sql.Rows) ([]Comic, error) {
comics := []Comic{}
for rows.Next() {
2023-05-17 06:41:21 +00:00
c := Comic{}
err := c.readRow(rows)
2023-05-17 06:41:21 +00:00
if err != nil {
return comics, nil
}
comics = append(comics, c)
}
return comics, nil
}
func allComics() ([]Comic, error) {
2023-05-19 07:47:04 +00:00
rows, err := db.Query("SELECT * FROM comic ORDER BY datetime ASC")
2023-05-17 06:41:21 +00:00
if err != nil {
return nil, err
}
defer rows.Close()
return readRows(rows)
}
func executeTemplate(w http.ResponseWriter, name string, data interface{}) error {
2023-05-21 19:58:29 +00:00
t, err := template.ParseFiles(filepath.Join(options.TemplatesPath, fmt.Sprintf("%s.html", name)))
2023-05-19 19:40:15 +00:00
if err != nil {
return err
2023-05-21 19:58:29 +00:00
}
2023-05-19 19:40:15 +00:00
tmp := new(strings.Builder)
2023-05-21 19:58:29 +00:00
err = t.Execute(tmp, data)
2023-05-19 19:40:15 +00:00
if err != nil {
return err
}
_, err = w.Write([]byte(tmp.String()))
return err
2023-05-17 06:41:21 +00:00
}
func comicView(w http.ResponseWriter, r * http.Request) {
var err error
2023-07-04 00:11:29 +00:00
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:]
}
path := strings.TrimPrefix(r.URL.Path, "/")
2023-07-04 00:11:29 +00:00
if path == options.ManagerRoot {
http.Redirect(w, r, managerPath("/index"), http.StatusSeeOther)
return
}
if len(path) >= 5 {
path = path[:5]
}
defer func() {
if err != nil {
2023-05-19 03:26:18 +00:00
errlog.Println(err)
return404(w,r)
return
}
}()
2023-05-19 19:40:15 +00:00
c := &Comic{}
context := Context{
Comics: nil,
Comic: c,
Title: "Black Ram Comics",
}
2023-05-19 19:40:15 +00:00
i := 1
if len(path) > 0{
i, err = strconv.Atoi(path)
if err != nil {
return
}
}
2023-05-19 07:47:04 +00:00
context.Current = i
comics, err := allComics()
2023-05-19 19:40:15 +00:00
if err != nil {
return500(w,r)
err = nil
return
}
if len(comics) > 0 {
c, err = getComic(i)
if err != nil {
errlog.Println(err)
return404(w,r)
err = nil
return
}
2023-07-04 00:11:29 +00:00
u, err := c.getAuthor()
if err == nil {
context.UserName = u.Name
}
context.Comic = c
context.Title = fmt.Sprintf("Black Ram Comics: %s", c.Title)
} else {
context.Comic = nil
}
2023-05-19 19:40:15 +00:00
context.First = 1
context.Last = len(comics)
2023-05-19 07:47:04 +00:00
context.Previous = i
2023-05-19 19:40:15 +00:00
if i > 1 {
context.Previous = i - 1
}
2023-05-19 07:47:04 +00:00
context.Next = i
2023-05-19 19:56:28 +00:00
if i < (len(comics)) {
2023-05-19 07:47:04 +00:00
context.Next = i + 1
}
2023-05-21 19:58:29 +00:00
err = executeTemplate(w, "comic", &context)
2023-05-19 19:40:15 +00:00
}
func allView(w http.ResponseWriter, r * http.Request) {
var err error
defer func() {
if err != nil {
errlog.Println(err)
return500(w,r)
err = nil
return
}
} ()
context := Context{
Comics: nil,
Title: "All Issues",
2023-05-19 03:26:18 +00:00
}
2023-05-19 19:40:15 +00:00
comics, err := allComics()
if err != nil {
return
}
2023-05-19 19:40:15 +00:00
for i, comic := range comics {
context.Comics = append(context.Comics, struct {
No int
Title string
Date string
2023-05-21 19:58:29 +00:00
Image string
2023-05-19 19:40:15 +00:00
}{
Title: comic.Title,
No: i+1,
Date: comic.DateTime,
2023-05-21 19:58:29 +00:00
Image: comic.Image,
2023-05-19 19:40:15 +00:00
})
}
2023-05-21 19:58:29 +00:00
err = executeTemplate(w, "comic", &context)
2023-05-19 03:26:18 +00:00
}
func latestView(w http.ResponseWriter, r * http.Request) {
comics, err := allComics()
if err != nil {
return500(w, r)
}
if len(comics) < 1 {
return404(w, r)
} else {
2023-05-19 07:47:04 +00:00
r.URL.Path = fmt.Sprintf("/%d", len(comics)-1)
comicView(w, r)
}
}
func firstView(w http.ResponseWriter, r * http.Request) {
comics, err := allComics()
if err != nil {
return500(w, r)
}
if len(comics) < 1 {
return404(w, r)
} else {
2023-05-19 07:47:04 +00:00
r.URL.Path = fmt.Sprintf("/0")
comicView(w, r)
}
2023-05-19 03:26:18 +00:00
}
func randomView(w http.ResponseWriter, r * http.Request) {
comics, err := allComics()
if err != nil {
2023-05-19 07:47:04 +00:00
errlog.Println(err)
return500(w, r)
2023-05-19 07:47:04 +00:00
return
}
if len(comics) < 1 {
return404(w, r)
} else {
2023-05-19 19:40:15 +00:00
r.URL.Path = fmt.Sprintf("/%d", rand.Intn(len(comics))+1)
comicView(w, r)
}
}
// Taken from: https://gist.github.com/hoitomt/c0663af8c9443f2a8294
func logRequest(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s\n", r.RemoteAddr, r.Method, r.URL)
handler.ServeHTTP(w, r)
})
}
func returnPlainText(w http.ResponseWriter, t string) error {
w.Header().Set("Content-Type", "text/plain")
_, err := w.Write([]byte(t))
return err
}
func returnError(w http.ResponseWriter, r * http.Request, status int) {
var err error
msg := fmt.Sprintf("%d", status)
w.WriteHeader(status)
defer func() {
if err != nil {
returnPlainText(w, msg)
}
}()
2023-05-20 18:20:23 +00:00
t, err := template.ParseFiles(filepath.Join(options.TemplatesPath, "error.html"))
2023-05-17 06:41:21 +00:00
if err != nil {
return
2023-05-17 06:41:21 +00:00
}
c := struct {
Code string
Title string
}{
Code: msg,
Title: "BRC: " + msg,
}
err = t.Execute(w, &c)
2023-05-17 06:41:21 +00:00
}
2023-06-14 03:49:34 +00:00
func return401(w http.ResponseWriter, r * http.Request) {
returnError(w, r, http.StatusUnauthorized)
}
func return404(w http.ResponseWriter, r * http.Request) {
returnError(w, r, http.StatusNotFound)
}
2023-05-17 06:41:21 +00:00
func return500(w http.ResponseWriter, r * http.Request) {
returnError(w, r, http.StatusInternalServerError)
2023-05-17 06:41:21 +00:00
}
2023-05-17 15:37:08 +00:00
func newComic(title string, image string, description string, tags string) (int, error) {
res, err := db.Exec("INSERT INTO comic(title, image, description, tags) VALUES(?, ?, ?, ?);",
2023-05-19 04:26:54 +00:00
title, image, description, tags)
if err != nil {
return -1, nil
}
id := int64(0)
if id, err = res.LastInsertId(); err != nil {
return 0, err
}
return int(id), err
2023-05-19 04:26:54 +00:00
}
2023-05-22 01:17:23 +00:00
func deleteComic(id int) error {
_, err := db.Exec("DELETE FROM comic WHERE id = ?;", id)
return err
}
2023-05-17 15:37:08 +00:00
func main() {
var err error
2023-05-19 03:26:18 +00:00
errlog = log.New(log.Writer(), "[ERROR] ", log.Flags())
2023-05-20 18:20:23 +00:00
options.Parse()
2023-05-20 18:20:23 +00:00
log.Println("using database path \"" + options.DBPath + "\"")
db, err = sql.Open("sqlite3", options.DBPath)
2023-05-17 15:37:08 +00:00
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(dbSquema)
2023-05-17 15:37:08 +00:00
if err != nil {
log.Fatal(err)
}
2023-05-20 18:20:23 +00:00
if options.Publish {
if len(options.ImagePath) < 1 {
2023-05-19 04:26:54 +00:00
panic("missing -i")
}
2023-05-20 18:20:23 +00:00
if len(options.Title) < 1 {
2023-05-19 04:26:54 +00:00
panic("missing -l")
}
_, err = newComic(options.Title, options.ImagePath, "", "")
2023-05-19 04:26:54 +00:00
if err != nil {
log.Fatal(err)
}
2023-05-20 18:20:23 +00:00
log.Println(fmt.Sprintf("comic \"%s\" created", options.Title))
2023-05-19 04:26:54 +00:00
return
}
2023-05-21 19:58:29 +00:00
// files
tPath := string(filepath.Join(options.TemplatesPath, "/static/"))
log.Println(fmt.Sprintf("using templates path \"%s\"", tPath))
fs := http.FileServer(http.Dir(tPath))
http.Handle("/static/", http.StripPrefix("/static/", fs))
log.Println(fmt.Sprintf("using media path \"%s\"", options.MediaPath))
fs = http.FileServer(http.Dir(options.MediaPath))
http.Handle("/media/", http.StripPrefix("/media/", fs))
2023-07-01 23:18:25 +00:00
// errors
http.HandleFunc("/401", return401)
http.HandleFunc("/404", return404)
http.HandleFunc("/500", return500)
// manager
2023-05-21 19:58:29 +00:00
if options.RunManager {
2023-07-01 23:18:25 +00:00
err = startManager(options.Address, options.Port, options.DBPath, options.MediaPath)
if err != nil {
log.Fatal(err)
}
2023-05-21 19:58:29 +00:00
}
// views
http.HandleFunc("/", comicView)
2023-05-19 03:26:18 +00:00
http.HandleFunc("/latest", latestView)
http.HandleFunc("/first", firstView)
2023-05-19 03:26:18 +00:00
http.HandleFunc("/random", randomView)
2023-05-19 07:47:04 +00:00
http.HandleFunc("/about", firstView)
2023-05-19 19:40:15 +00:00
http.HandleFunc("/all", allView)
2023-05-19 07:47:04 +00:00
http.HandleFunc("/blog", firstView)
2023-05-20 18:20:23 +00:00
uri := fmt.Sprintf("%s:%d", options.Address, options.Port)
log.Println("listening to http://" + uri)
log.Fatal(http.ListenAndServe(uri, logRequest(http.DefaultServeMux)))
2023-05-17 15:37:08 +00:00
}