Compare commits

...

2 Commits

Author SHA1 Message Date
TheLastBilly e3b29eef24 added manager window 2023-05-21 15:58:29 -04:00
TheLastBilly 6b4ada6545 removed argparse 2023-05-20 14:20:23 -04:00
7 changed files with 255 additions and 64 deletions

127
comics.go
View File

@ -1,26 +1,26 @@
package main
import (
"os"
"log"
"fmt"
"flag"
"strconv"
"strings"
"math/rand"
//"net/url"
"net/http"
"database/sql"
"html/template"
"path/filepath"
"github.com/akamensky/argparse"
_ "github.com/mattn/go-sqlite3"
)
var errlog *log.Logger
var errlog *log.Logger = nil
var db *sql.DB = nil
var mediaPath *string = nil
var templatesPath *string = nil
var options Options = Options{}
var NoSuchComicErr Err = Err{msg: "no such comic found"}
const dbSquema string = `
CREATE TABLE IF NOT EXISTS comic (
@ -32,6 +32,18 @@ CREATE TABLE IF NOT EXISTS comic (
tags TEXT
);`
type Options struct {
DBPath string
MediaPath string
TemplatesPath string
Address string
Port int
Publish bool
Title string
ImagePath string
RunManager bool
}
type Comic struct {
ID int
DateTime string
@ -46,6 +58,7 @@ type Context struct {
No int
Title string
Date string
Image string
}
Comic *Comic
Current int
@ -61,12 +74,26 @@ type Err struct {
msg string
}
func (o * Options) Parse() error {
flag.StringVar(&o.DBPath, "d", "./db.sqlite", "Sets path to database file")
flag.StringVar(&o.MediaPath, "m", "./media/", "Sets path to media directory")
flag.StringVar(&o.TemplatesPath, "t", "./templates/", "Sets path to templates directory")
flag.StringVar(&o.Address, "a", "127.0.0.1", "Defines the address the web server will listen to")
flag.IntVar(&o.Port, "p", 8080, "Defines the port the web server will listen to")
flag.BoolVar(&o.Publish, "u", false, "Creates new commics. Needs both -i and -l")
flag.StringVar(&o.Title, "l", "", "Title for new comic")
flag.StringVar(&o.ImagePath, "i", "", "Image path for new comic")
flag.BoolVar(&o.RunManager, "r", false, "Runs the content manager instead of the site")
flag.Parse()
return nil
}
func (e * Err) Error() string {
return e.msg
}
var NoSuchComicErr Err = Err{msg: "no such comic found"}
func (e * Err) With(i string) Err {
return Err{msg:fmt.Sprintf("%s with %s", e.msg, i)}
}
@ -89,8 +116,6 @@ func getComic(id int) (*Comic, error) {
return c, err
}
log.Println(comics)
if len(comics) <= id {
return c, &nferr
}
@ -123,13 +148,14 @@ func allComics() ([]Comic, error) {
return readRows(rows)
}
func executeMainTemplate(w http.ResponseWriter, context * Context) error {
t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
func executeTemplate(w http.ResponseWriter, name string, data any) error {
t, err := template.ParseFiles(filepath.Join(options.TemplatesPath, fmt.Sprintf("%s.html", name)))
if err != nil {
return err
}
}
tmp := new(strings.Builder)
err = t.Execute(tmp, context)
err = t.Execute(tmp, data)
if err != nil {
return err
}
@ -200,7 +226,7 @@ func comicView(w http.ResponseWriter, r * http.Request) {
context.Next = i + 1
}
err = executeMainTemplate(w, &context)
err = executeTemplate(w, "comic", &context)
}
func allView(w http.ResponseWriter, r * http.Request) {
@ -230,14 +256,16 @@ func allView(w http.ResponseWriter, r * http.Request) {
No int
Title string
Date string
Image string
}{
Title: comic.Title,
No: i+1,
Date: comic.DateTime,
Image: comic.Image,
})
}
err = executeMainTemplate(w, &context)
err = executeTemplate(w, "comic", &context)
}
func latestView(w http.ResponseWriter, r * http.Request) {
@ -309,7 +337,7 @@ func returnError(w http.ResponseWriter, r * http.Request, status int) {
}
}()
t, err := template.ParseFiles(filepath.Join(*templatesPath, "error.html"))
t, err := template.ParseFiles(filepath.Join(options.TemplatesPath, "error.html"))
if err != nil {
return
}
@ -341,33 +369,11 @@ 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)
}
options.Parse()
log.Println("using database path \"" + *dbPath + "\"")
db, err = sql.Open("sqlite3", *dbPath)
log.Println("using database path \"" + options.DBPath + "\"")
db, err = sql.Open("sqlite3", options.DBPath)
if err != nil {
log.Fatal(err)
}
@ -378,18 +384,33 @@ func main() {
log.Fatal(err)
}
if *publish {
if len(*image) < 1 {
if options.Publish {
if len(options.ImagePath) < 1 {
panic("missing -i")
}
if len(*title) < 1 {
if len(options.Title) < 1 {
panic("missing -l")
}
err = newComic(*title, *image, "", "")
err = newComic(options.Title, options.ImagePath, "", "")
if err != nil {
log.Fatal(err)
}
log.Println(fmt.Sprintf("comic \"%s\" created", *title))
log.Println(fmt.Sprintf("comic \"%s\" created", options.Title))
return
}
// files
tPath := string(filepath.Join(options.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\"", options.MediaPath))
fs = http.FileServer(http.Dir(options.MediaPath))
http.Handle("/media/", http.StripPrefix("/media/", fs))
if options.RunManager {
log.Fatal(startManager(options.Address, options.Port, options.DBPath,
options.MediaPath))
return
}
@ -406,17 +427,7 @@ func main() {
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)
uri := fmt.Sprintf("%s:%d", options.Address, options.Port)
log.Println("listening to http://" + uri)
log.Fatal(http.ListenAndServe(uri, logRequest(http.DefaultServeMux)))
}

5
go.mod
View File

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

2
go.sum
View File

@ -1,4 +1,2 @@
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=

58
manager.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"log"
"fmt"
"net/http"
)
func managerIndexView(w http.ResponseWriter, r * http.Request) {
var err error
defer func() {
if err != nil {
errlog.Println(err)
return500(w,r)
err = nil
return
}
} ()
context := Context{
Comics: nil,
Title: "Black Ram Comics Manager",
}
comics, err := allComics()
if err != nil {
return
}
for i, comic := range comics {
context.Comics = append(context.Comics, struct {
No int
Title string
Date string
Image string
}{
Title: comic.Title,
No: i+1,
Date: comic.DateTime,
Image: comic.Image,
})
}
err = executeTemplate(w, "manager", context)
}
func managerEditView(w http.ResponseWriter, r * http.Request) {
}
func startManager(address string, port int, dbPath string, mediaPath string) error {
http.HandleFunc("/", managerIndexView)
http.HandleFunc("/edit/", managerEditView)
uri := fmt.Sprintf("%s:%d", address, port)
log.Println("listening to http://" + uri)
return http.ListenAndServe(uri, logRequest(http.DefaultServeMux))
}

54
templates/manager.html Normal file
View File

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="icon" type="/image/png" href="/static/img/favicon.png"/>
<link rel="stylesheet" href="/static/css/manager.css">
<link rel="stylesheet" href="/static/css/base.css">
<link rel="stylesheet" href="/static/css/top.css">
<title>Black Ram Comic Manager</title>
</head>
<body>
<div class="manager-top">
<div class="logging-buttom">
<a href="/logout">
<h1>User &ltLogout&gt</h1>
</a>
</div>
<div class="create-button">
<a href="/create">
<h1>Create</h1>
</a>
</div>
</div>
{{ if .Comics }}
<div class="manager-list">
<table>
<tr>
<th scope="col">&ltTitle&gt</th>
<th scope="col">&ltDate&gt</th>
<th scope="col">&ltImage&gt</th>
<th scope="col">&ltActions&gt</th>
</tr>
{{ range $comic := .Comics }}
<tr>
<td>{{ $comic.Title }}</td>
<td>{{ $comic.Date }}</td>
<td>
<a href="/media/{{ $comic.Image }}">
{{ $comic.Image }}
</a>
</td>
<td class="actions">
<a href="/edit/{{ $comic.No }}">Edit</a>
<a href="/remove/{{ $comic.No }}">Remove</a>
</td>
</tr>
{{ end }}
</table>
</div>
{{ end }}
</body>
</html>

View File

@ -93,6 +93,7 @@ pre {
}
a {
text-decoration: none;
color: #2484c1;
}
a:link {
text-decoration: none;

View File

@ -0,0 +1,72 @@
.manager-top {
width: 100%;
display: flex;
text-transform: uppercase;
font-size: 1em;
margin-top: 1em;
margin-bottom: 1em;
/*margin-left: 0.25em;*/
/*margin-right: 0.25em;*/
}
.manager-top h1 {
color: #282828;
font-weight: bold;
margin-top: 0;
margin-bottom: 0;
padding-left: 0.25em;
padding-right: 0.25em;
background-color: #ebdbb2;
}
.manager-top h1:hover {
color: #ebdbb2;
background-color: #282828;
}
.create-button {
margin-left: auto;
}
.manager-list {
font-weight: bold;
}
.manager-list .actions {
text-transform: uppercase;
text-align: center;
}
.manager-list th {
background-color: #ebdbb2;
color: #282828;
}
.manager-list td {
text-align: left;
border-bottom: solid 0.125em #ebdbb2;
padding: 0.2em 0;
padding-top: 0.4em;
}
.manager-list tr {
text-align: center;
}
.manager-list table th[scope="col"] {
text-transform: uppercase;
font-size: 30px;
}
.manager-list table th[scope="col"]:hover {
color: #ebdbb2;
background-color: #282828;
}
.manager-list table {
width: 100%;
}
.manager-list table, th, td {
border-color: #2484c1;
}