comics/comics.go

309 lines
7.1 KiB
Go

package main
import (
"os"
"log"
"fmt"
"strconv"
"strings"
//"net/url"
"net/http"
"database/sql"
"html/template"
"path/filepath"
"github.com/akamensky/argparse"
_ "github.com/mattn/go-sqlite3"
)
var errlog *log.Logger
var db *sql.DB = nil
var mediaPath *string = nil
var templatesPath *string = nil
type Comic struct {
ID int
DateTime string
Title string
Image string
Description string
Tags string
}
func (c *Comic) readRow(db * sql.Rows) error {
return db.Scan(&c.ID, &c.DateTime, &c.Title, &c.Image,
&c.Description, &c.Tags)
}
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
);`
func readRows(db *sql.Rows) ([]Comic, error) {
comics := []Comic{}
for db.Next() {
c := Comic{}
err := c.readRow(db)
if err != nil {
return comics, nil
}
comics = append(comics, c)
}
return comics, nil
}
func allComics() ([]Comic, error) {
rows, err := db.Query("SELECT * FROM comic")
if err != nil {
return nil, err
}
defer rows.Close()
return readRows(rows)
}
func getComic(id int) (Comic, error) {
c := Comic{}
if id < 0 {
id = 0
}
rows, err := db.Query("SELECT * FROM comic WHERE id = ?", id)
if err != nil {
return c, err
}
defer rows.Close()
err = c.readRow(rows)
return c, err
}
func insertComic(c * Comic) error {
_, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)",
c.ID, c.DateTime, c.Title, c.Image, c.Description, c.Tags)
return err
}
func indexView(w http.ResponseWriter, r * http.Request) {
var err error
path := strings.TrimPrefix(r.URL.Path, "/")
if len(path) >= 5 {
path = path[:5]
}
defer func() {
if err != nil {
errlog.Println(err)
return500(w,r)
return
}
}()
c := Comic{
Title: "Test Comic",
}
context := struct {
Comic *Comic
Previous int
Next int
Title string
}{
Comic: &c,
Previous: 0,
Next: 0,
Title: "Black Ram Comics",
}
i := 0
if len(path) > 0{
i, err = strconv.Atoi(path)
if err != nil {
return
}
c, err = getComic(i)
if err != nil {
return404(w,r)
err = nil
return
}
context.Comic = &c
context.Title = "BRC: " + c.Title
}
_, err = getComic(i-1)
if err == nil {
context.Previous = i - 1
}
_, err = getComic(i+1)
if err == nil {
context.Next = i + 1
}
t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
if err != nil {
errlog.Println(err)
return
}
tmp := new(strings.Builder)
err = t.Execute(tmp, &context)
if err != nil {
return
}
_, err = w.Write([]byte(tmp.String()))
}
func latestView(w http.ResponseWriter, r * http.Request) {
returnPlainText(w, "latest")
}
func randomView(w http.ResponseWriter, r * http.Request) {
returnPlainText(w, "random")
}
// 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)
}
}()
t, err := template.ParseFiles(filepath.Join(*templatesPath, "error.html"))
if err != nil {
return
}
c := struct {
Code string
Title string
}{
Code: msg,
Title: "BRC: " + msg,
}
err = t.Execute(w, &c)
}
func return404(w http.ResponseWriter, r * http.Request) {
returnError(w, r, http.StatusNotFound)
}
func return500(w http.ResponseWriter, r * http.Request) {
returnError(w, r, http.StatusInternalServerError)
}
func newComic(title string, image string, description string, tags string) error {
_, err := db.Exec("INSERT INTO comic(title, image, description, tags) VALUES(?, ?, ?, ?);",
title, image, description, tags)
return err
}
func main() {
var err error
errlog = log.New(log.Writer(), "[ERROR] ", log.Flags())
parser := argparse.NewParser("comics", "Webserver for comics distribution websites")
dbPath := parser.String("d", "db-path", &argparse.Options{
Required: false, Help: "Sets path to database file", Default: "./db.sqlite"})
mediaPath = parser.String("m", "media-path", &argparse.Options{
Required: false, Help: "Sets path to media directory", Default: "./media/"})
templatesPath = parser.String("t", "templates-path", &argparse.Options{
Required: false, Help: "Sets path to templates directory", Default: "./templates/"})
address := parser.String("a", "address", &argparse.Options{
Required: false, Help: "Defines the address the web server will listen to", Default: "127.0.0.1"})
port := parser.Int("p", "port", &argparse.Options{
Required: false, Help: "Defines the port the web server will listen to", Default: 8080})
publish := parser.Flag("u", "publish", &argparse.Options{
Required: false, Help: "Creates new commics. Needs both -i and -l", Default: false})
title := parser.String("l", "title", &argparse.Options{
Required: false, Help: "Title for new comic", Default: ""})
image := parser.String("i", "image", &argparse.Options{
Required: false, Help: "Image path for new comic", Default: ""})
err = parser.Parse(os.Args)
if err != nil {
log.Fatal(err)
}
log.Println("using database path \"" + *dbPath + "\"")
db, err = sql.Open("sqlite3", *dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(dbSquema)
if err != nil {
log.Fatal(err)
}
if *publish {
if len(*image) < 1 {
panic("missing -i")
}
if len(*title) < 1 {
panic("missing -l")
}
err = newComic(*title, *image, "", "")
if err != nil {
log.Fatal(err)
}
log.Println(fmt.Sprintf("comic \"%s\" created", *title))
return
}
// views
http.HandleFunc("/", indexView)
http.HandleFunc("/latest", latestView)
http.HandleFunc("/random", randomView)
// errors
http.HandleFunc("/404", return404)
http.HandleFunc("/500", return500)
// files
tPath := string(filepath.Join(*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\"", *mediaPath))
fs = http.FileServer(http.Dir(*mediaPath))
http.Handle("/media/", http.StripPrefix("/media/", fs))
uri := fmt.Sprintf("%s:%d", *address, *port)
log.Println("listening to http://" + uri)
log.Fatal(http.ListenAndServe(uri, logRequest(http.DefaultServeMux)))
}