2023-05-17 15:37:08 +00:00
|
|
|
package main
|
2023-05-17 06:41:21 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-18 03:33:16 +00:00
|
|
|
"os"
|
2023-05-17 15:37:08 +00:00
|
|
|
"log"
|
2023-05-18 03:33:16 +00:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2023-05-17 06:41:21 +00:00
|
|
|
"net/http"
|
|
|
|
"database/sql"
|
2023-05-18 03:33:16 +00:00
|
|
|
"html/template"
|
|
|
|
"path/filepath"
|
2023-05-17 15:37:08 +00:00
|
|
|
|
|
|
|
"github.com/akamensky/argparse"
|
2023-05-18 03:33:16 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2023-05-17 06:41:21 +00:00
|
|
|
)
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
var db *sql.DB = nil
|
|
|
|
var mediaPath *string = nil
|
|
|
|
var templatesPath *string = nil
|
2023-05-17 06:41:21 +00:00
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
type Comic struct {
|
|
|
|
ID int
|
2023-05-17 06:41:21 +00:00
|
|
|
DateTime string
|
|
|
|
Title string
|
|
|
|
Image string
|
|
|
|
Description string
|
|
|
|
Tags string
|
|
|
|
}
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
func (c *Comic) readRow(db * sql.Rows) error {
|
|
|
|
return db.Scan(&c.ID, &c.DateTime, &c.Title, &c.Image,
|
2023-05-17 06:41:21 +00:00
|
|
|
&c.Description, &c.Tags)
|
|
|
|
}
|
|
|
|
|
|
|
|
const dbSquema string = `
|
|
|
|
CREATE TABLE IF NOT EXISTS comic (
|
2023-05-18 03:33:16 +00:00
|
|
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
2023-05-17 06:41:21 +00:00
|
|
|
datetime DATETIME NOT NULL,
|
|
|
|
title CHAR(50),
|
|
|
|
image CHAR(200),
|
|
|
|
description TEXT,
|
|
|
|
tags TEXT
|
|
|
|
);`
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
func readRows(db *sql.Rows) ([]Comic, error) {
|
|
|
|
comics := []Comic{}
|
2023-05-17 06:41:21 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
func getComic(id string) (Comic, error) {
|
|
|
|
c := Comic{}
|
|
|
|
|
2023-05-17 06:41:21 +00:00
|
|
|
rows, err := db.Query("SELECT * FROM comic WHERE id = ?", id)
|
|
|
|
if err != nil {
|
2023-05-18 03:33:16 +00:00
|
|
|
return c, err
|
2023-05-17 06:41:21 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-05-18 03:33:16 +00:00
|
|
|
|
|
|
|
err = c.readRow(rows)
|
2023-05-17 06:41:21 +00:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func insertComic(c * Comic) error {
|
2023-05-18 03:33:16 +00:00
|
|
|
_, err := db.Exec("INSERT INTO comic VALUES (?, ?, ?, ?, ?, ?)",
|
|
|
|
c.ID, c.DateTime, c.Title, c.Image, c.Description, c.Tags)
|
2023-05-17 06:41:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
func index(w http.ResponseWriter, r * http.Request) {
|
|
|
|
url.Parse(r.URL.Path)
|
|
|
|
log.Println(r.URL.Path)
|
|
|
|
returnPlainText(w, "Hello World!\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
msg := string(status)
|
|
|
|
|
|
|
|
w.WriteHeader(status)
|
|
|
|
t, err := template.ParseFiles(filepath.Join(*templatesPath, msg + ".html"))
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
returnPlainText(w, msg)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-17 06:41:21 +00:00
|
|
|
if err != nil {
|
2023-05-18 03:33:16 +00:00
|
|
|
return
|
2023-05-17 06:41:21 +00:00
|
|
|
}
|
2023-05-18 03:33:16 +00:00
|
|
|
err = t.Execute(w, nil)
|
2023-05-17 06:41:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
func return404(w http.ResponseWriter, r * http.Request) {
|
|
|
|
returnError(w, r, http.StatusNotFound)
|
|
|
|
}
|
2023-05-17 06:41:21 +00:00
|
|
|
|
2023-05-18 03:33:16 +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 main() {
|
2023-05-18 03:33:16 +00:00
|
|
|
var err error
|
2023-05-17 15:37:08 +00:00
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
parser := argparse.NewParser("comics", "Webserver for comics distribution websites")
|
2023-05-17 15:37:08 +00:00
|
|
|
|
2023-05-18 03:33:16 +00:00
|
|
|
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})
|
|
|
|
|
|
|
|
err = parser.Parse(os.Args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("using database path \"" + *dbPath + "\"")
|
|
|
|
db, err = sql.Open("sqlite3", *dbPath)
|
2023-05-17 15:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
2023-05-18 03:33:16 +00:00
|
|
|
|
|
|
|
_, err = db.Exec(dbSquema)
|
2023-05-17 15:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-05-18 03:33:16 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/", index)
|
|
|
|
|
|
|
|
http.HandleFunc("/404", return404)
|
|
|
|
http.HandleFunc("/500", return500)
|
|
|
|
|
|
|
|
uri := fmt.Sprintf("%s:%d", *address, *port)
|
|
|
|
log.Println("listening to http://" + uri)
|
|
|
|
log.Fatal(http.ListenAndServe(uri, logRequest(http.DefaultServeMux)))
|
2023-05-17 15:37:08 +00:00
|
|
|
}
|