diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2023-12-08 05:42:24 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2023-12-08 05:42:24 +0100 |
commit | bfbdf29ca3a60798c5267e88de5577a3faead9e9 (patch) | |
tree | c6acf5ee04749b5957901253aa0271b4279710e2 /main.go | |
parent | 941022bdd6c69de93f38992205b02ed95c6e8eb0 (diff) | |
download | gallery-bfbdf29ca3a60798c5267e88de5577a3faead9e9.tar.gz gallery-bfbdf29ca3a60798c5267e88de5577a3faead9e9.tar.xz gallery-bfbdf29ca3a60798c5267e88de5577a3faead9e9.zip |
More basic stuff
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 62 |
1 files changed, 57 insertions, 5 deletions
@@ -3,11 +3,21 @@ package main import ( "database/sql" "log" + "net" + "net/http" "os" + "path/filepath" + "time" _ "github.com/mattn/go-sqlite3" ) +// TODO: Perhaps maintain the DB, and the gallery directory, as globals. + +func openDB(gd string) (*sql.DB, error) { + return sql.Open("sqlite3", filepath.Join(gd, "gallery.db")) +} + // init GD - initialize a "gallery directory" that contains gallery.sqlite, // images, thumbs. func cmdInit(args []string) { @@ -16,19 +26,61 @@ func cmdInit(args []string) { } gd := args[0] - - db, err := sql.Open("sqlite3", gd+"/foo.db") + db, err := openDB(gd) if err != nil { log.Fatalln(err) } - // TODO: Figure out a go generate to create a Go file to create the db. - _ = db + if _, err = db.Query(initializeSQL); err != nil { + log.Fatalln(err) + } + if err := os.MkdirAll(filepath.Join(gd, "images"), 0777); err != nil { + log.Fatalln(err) + } + if err := os.MkdirAll(filepath.Join(gd, "thumbs"), 0777); err != nil { + log.Fatalln(err) + } } // run GD ADDRESS - run a web UI against GD on ADDRESS func cmdRun(args []string) { - // TODO: Use "public" from CWD for files. + if len(args) != 2 { + log.Fatalln("usage: GD ADDRESS") + } + + gd := args[0] + db, err := openDB(gd) + if err != nil { + log.Fatalln(err) + } + + _ = db + address := args[1] + + http.Handle("/", http.FileServer(http.Dir("public"))) + // TODO: These subdirectories should be indirect + // (skip the hash subpath, don't require the .webp suffix). + http.Handle("/images", + http.FileServer(http.Dir(filepath.Join(gd, "images")))) + http.Handle("/thumbs", + http.FileServer(http.Dir(filepath.Join(gd, "thumbs")))) + + host, port, err := net.SplitHostPort(address) + if err != nil { + log.Println(err) + } else if host == "" { + log.Println("http://" + net.JoinHostPort("localhost", port)) + } else { + log.Println("http://" + address) + } + + s := &http.Server{ + Addr: address, + ReadTimeout: 60 * time.Second, + WriteTimeout: 60 * time.Second, + MaxHeaderBytes: 32 << 10, + } + log.Fatalln(s.ListenAndServe()) } // import GD ROOT... - add files to the "entry" table |