comics/main.go

175 lines
4.3 KiB
Go
Raw 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"
"net/url"
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/akamensky/argparse"
_ "github.com/mattn/go-sqlite3"
2023-05-17 06:41:21 +00:00
)
var db *sql.DB = nil
var mediaPath *string = nil
var templatesPath *string = nil
2023-05-17 06:41:21 +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
}
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 (
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
);`
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)
}
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 {
return c, err
2023-05-17 06:41:21 +00:00
}
defer rows.Close()
err = c.readRow(rows)
2023-05-17 06:41:21 +00:00
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)
2023-05-17 06:41:21 +00:00
return err
}
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 {
return
2023-05-17 06:41:21 +00:00
}
err = t.Execute(w, nil)
2023-05-17 06:41:21 +00:00
}
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 main() {
var err error
2023-05-17 15:37:08 +00:00
parser := argparse.NewParser("comics", "Webserver for comics distribution websites")
2023-05-17 15:37:08 +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()
_, err = db.Exec(dbSquema)
2023-05-17 15:37:08 +00:00
if err != nil {
log.Fatal(err)
}
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
}