removed argparse

This commit is contained in:
TheLastBilly 2023-05-20 14:20:23 -04:00
parent 2cbe6018c3
commit 6b4ada6545
3 changed files with 47 additions and 51 deletions

View File

@ -1,9 +1,9 @@
package main
import (
"os"
"log"
"fmt"
"flag"
"strconv"
"strings"
"math/rand"
@ -13,14 +13,15 @@ import (
"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 +33,17 @@ 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
}
type Comic struct {
ID int
DateTime string
@ -61,12 +73,25 @@ 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.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 +114,6 @@ func getComic(id int) (*Comic, error) {
return c, err
}
log.Println(comics)
if len(comics) <= id {
return c, &nferr
}
@ -124,7 +147,7 @@ func allComics() ([]Comic, error) {
}
func executeMainTemplate(w http.ResponseWriter, context * Context) error {
t, err := template.ParseFiles(filepath.Join(*templatesPath, "comic.html"))
t, err := template.ParseFiles(filepath.Join(options.TemplatesPath, "comic.html"))
if err != nil {
return err
}
@ -309,7 +332,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 +364,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 +379,18 @@ 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
}
@ -407,16 +408,16 @@ func main() {
http.HandleFunc("/500", return500)
// files
tPath := string(filepath.Join(*templatesPath, "/static/"))
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\"", *mediaPath))
fs = http.FileServer(http.Dir(*mediaPath))
log.Println(fmt.Sprintf("using media path \"%s\"", options.MediaPath))
fs = http.FileServer(http.Dir(options.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=