113 lines
2.4 KiB
Go
113 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"net/http"
|
|
"database/sql"
|
|
|
|
"github.com/akamensky/argparse"
|
|
)
|
|
|
|
db *sql.DB = nil
|
|
dbPath string = "./db.sqlite"
|
|
|
|
type Comic {
|
|
ID string
|
|
DateTime string
|
|
Title string
|
|
Image string
|
|
Description string
|
|
Tags string
|
|
}
|
|
|
|
func (c *Comic) readRow(db * sql.DB) error {
|
|
return db.Scan(&c.ID, &d.DateTime, &c.Title, &c.Image,
|
|
&c.Description, &c.Tags)
|
|
}
|
|
|
|
const dbSquema string = `
|
|
CREATE TABLE IF NOT EXISTS comic (
|
|
id INTEGER NOT NULL PRIVATE KEY AUTOINCREMENT,
|
|
datetime DATETIME NOT NULL,
|
|
title CHAR(50),
|
|
image CHAR(200),
|
|
description TEXT,
|
|
tags TEXT
|
|
);`
|
|
|
|
func readRows(db *sql.DB) ([]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 string) (c Comic, error) {
|
|
rows, err := db.Query("SELECT * FROM comic WHERE id = ?", id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
err := c.readRow(rows)
|
|
return c, err
|
|
}
|
|
|
|
func insertComic(c * Comic) error {
|
|
_, err := db.ExecContext("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)",
|
|
c.id, c.DateTime, c.Title, c.Image, c.Description, c.Tags)
|
|
return err
|
|
}
|
|
|
|
func renderTemplate(w http.ResponseWriter, t string, p *Page) {
|
|
err := tempaltes.ExecuteTemplate(w, t + ".html", p)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func comicView(w http.ResponseWriter, r * http.Request, n string) {
|
|
|
|
}
|
|
|
|
func main() {
|
|
parser := argparse.NewParser("comics", "Webserver for comics distribution websites")
|
|
|
|
dbPath = parser.Flag("d", "db-path", &argparse.Options{
|
|
Required: false, Help: "Sets path to database file", Default: dbPath
|
|
})
|
|
mediaPath = parser.Flag("m", "media-path", &argparser.Options{
|
|
Required: false, Help: "Sets path to media directory", Default: mediaPath
|
|
})
|
|
|
|
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)
|
|
}
|
|
}
|