added sql database setup, templates and logging

This commit is contained in:
TheLastBilly 2023-05-17 23:33:16 -04:00
parent 2d66daa1da
commit d8163724dc
3 changed files with 101 additions and 34 deletions

5
go.mod
View File

@ -2,4 +2,7 @@ module tildegit.com/drevil/comics
go 1.19
require github.com/akamensky/argparse v1.4.0
require (
github.com/akamensky/argparse v1.4.0
github.com/mattn/go-sqlite3 v1.14.16
)

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/akamensky/argparse v1.4.0 h1:YGzvsTqCvbEZhL8zZu2AiA5nq805NZh75JNj4ajn1xc=
github.com/akamensky/argparse v1.4.0/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

128
main.go
View File

@ -1,20 +1,25 @@
package main
import (
"fmt"
"os"
"log"
"os"
"fmt"
"net/url"
"net/http"
"database/sql"
"html/template"
"path/filepath"
"github.com/akamensky/argparse"
_ "github.com/mattn/go-sqlite3"
)
db *sql.DB = nil
dbPath string = "./db.sqlite"
var db *sql.DB = nil
var mediaPath *string = nil
var templatesPath *string = nil
type Comic {
ID string
type Comic struct {
ID int
DateTime string
Title string
Image string
@ -22,14 +27,14 @@ type Comic {
Tags string
}
func (c *Comic) readRow(db * sql.DB) error {
return db.Scan(&c.ID, &d.DateTime, &c.Title, &c.Image,
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 PRIVATE KEY AUTOINCREMENT,
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
datetime DATETIME NOT NULL,
title CHAR(50),
image CHAR(200),
@ -37,8 +42,8 @@ CREATE TABLE IF NOT EXISTS comic (
tags TEXT
);`
func readRows(db *sql.DB) ([]Comic, error) {
comics := []Comic
func readRows(db *sql.Rows) ([]Comic, error) {
comics := []Comic{}
for db.Next() {
c := Comic{}
err := c.readRow(db)
@ -61,52 +66,109 @@ func allComics() ([]Comic, error) {
return readRows(rows)
}
func getComic(id string) (c Comic, error) {
func getComic(id string) (Comic, error) {
c := Comic{}
rows, err := db.Query("SELECT * FROM comic WHERE id = ?", id)
if err != nil {
return nil, err
return c, err
}
defer rows.Close()
err := c.readRow(rows)
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)
_, err := db.Exec("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 index(w http.ResponseWriter, r * http.Request) {
url.Parse(r.URL.Path)
log.Println(r.URL.Path)
returnPlainText(w, "Hello World!\n")
}
func comicView(w http.ResponseWriter, r * http.Request, n string) {
// 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)
}
}()
if err != nil {
return
}
err = t.Execute(w, nil)
}
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 main() {
var err error
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)
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)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err := db.Exec(dbSquema)
_, err = db.Exec(dbSquema)
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)))
}