tagliatelle

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 31d90c03604603dc31007e87bb5ef6a3e1d3d927
parent c67e546756dc1240a8c3c6fc89e17609799bdf4c
Author: breadcat <breadcat@users.noreply.github.com>
Date:   Fri, 13 Feb 2026 17:03:04 +0000

Split remaining main.go code

Diffstat:
Ainclude-db.go | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude-routes.go | 26++++++++++++++++++++++++++
Ainclude-templates.go | 37+++++++++++++++++++++++++++++++++++++
Mmain.go | 93+++++++++++++++----------------------------------------------------------------
4 files changed, 132 insertions(+), 76 deletions(-)

diff --git a/include-db.go b/include-db.go @@ -0,0 +1,52 @@ +package main + +import ( + "database/sql" + + _ "github.com/mattn/go-sqlite3" +) + +// InitDatabase opens the database connection and creates tables if needed +func InitDatabase(dbPath string) (*sql.DB, error) { + db, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, err + } + + if err := createTables(db); err != nil { + db.Close() + return nil, err + } + + return db, nil +} + +// createTables creates all necessary database tables +func createTables(db *sql.DB) error { + schema := ` + CREATE TABLE IF NOT EXISTS files ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + filename TEXT, + path TEXT, + description TEXT DEFAULT '' + ); + CREATE TABLE IF NOT EXISTS categories ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT UNIQUE + ); + CREATE TABLE IF NOT EXISTS tags ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + category_id INTEGER, + value TEXT, + UNIQUE(category_id, value) + ); + CREATE TABLE IF NOT EXISTS file_tags ( + file_id INTEGER, + tag_id INTEGER, + UNIQUE(file_id, tag_id) + ); + ` + + _, err := db.Exec(schema) + return err +} diff --git a/include-routes.go b/include-routes.go @@ -0,0 +1,26 @@ +package main + +import ( + "net/http" +) + +// RegisterRoutes sets up all HTTP routes for the application +func RegisterRoutes() { + // Page handlers + http.HandleFunc("/", listFilesHandler) + http.HandleFunc("/add", uploadHandler) + http.HandleFunc("/add-yt", ytdlpHandler) + http.HandleFunc("/admin", adminHandler) + http.HandleFunc("/bulk-tag", bulkTagHandler) + http.HandleFunc("/cbz/", cbzViewerHandler) + http.HandleFunc("/file/", fileRouter) + http.HandleFunc("/search", searchHandler) + http.HandleFunc("/tag/", tagFilterHandler) + http.HandleFunc("/tags", tagsHandler) + http.HandleFunc("/thumbnails/generate", generateThumbnailHandler) + http.HandleFunc("/untagged", untaggedFilesHandler) + http.HandleFunc("/upload-url", uploadFromURLHandler) + // Static file serving + http.Handle("/uploads/", http.StripPrefix("/uploads/", http.FileServer(http.Dir(config.UploadDir)))) + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) +} diff --git a/include-templates.go b/include-templates.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "html/template" + "strings" +) + +// InitTemplates loads and parses all HTML templates with helper functions +func InitTemplates() (*template.Template, error) { + return template.New("").Funcs(template.FuncMap{ + "add": func(a, b int) int { return a + b }, + "sub": func(a, b int) int { return a - b }, + "hasAnySuffix": func(s string, suffixes ...string) bool { + for _, suf := range suffixes { + if strings.HasSuffix(strings.ToLower(s), suf) { + return true + } + } + return false + }, + "dict": func(values ...interface{}) (map[string]interface{}, error) { + if len(values)%2 != 0 { + return nil, fmt.Errorf("dict requires an even number of args") + } + dict := make(map[string]interface{}, len(values)/2) + for i := 0; i < len(values); i += 2 { + key, ok := values[i].(string) + if !ok { + return nil, fmt.Errorf("dict keys must be strings") + } + dict[key] = values[i+1] + } + return dict, nil + }, + }).ParseGlob("templates/*.html") +} diff --git a/main.go b/main.go @@ -2,14 +2,10 @@ package main import ( "database/sql" - "fmt" "html/template" "log" "net/http" "os" - "strings" - - _ "github.com/mattn/go-sqlite3" ) var ( @@ -19,93 +15,38 @@ var ( ) func main() { + // Load configuration if err := loadConfig(); err != nil { log.Fatalf("Failed to load config: %v", err) } + // Initialize database var err error - db, err = sql.Open("sqlite3", config.DatabasePath) + db, err = InitDatabase(config.DatabasePath) if err != nil { - log.Fatal(err) + log.Fatalf("Failed to initialize database: %v", err) } defer db.Close() - _, err = db.Exec(` - CREATE TABLE IF NOT EXISTS files ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - filename TEXT, - path TEXT, - description TEXT DEFAULT '' - ); - CREATE TABLE IF NOT EXISTS categories ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - name TEXT UNIQUE - ); - CREATE TABLE IF NOT EXISTS tags ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - category_id INTEGER, - value TEXT, - UNIQUE(category_id, value) - ); - CREATE TABLE IF NOT EXISTS file_tags ( - file_id INTEGER, - tag_id INTEGER, - UNIQUE(file_id, tag_id) - ); - `) - if err != nil { - log.Fatal(err) - } - + // Create necessary directories os.MkdirAll(config.UploadDir, 0755) os.MkdirAll("static", 0755) - tmpl = template.Must(template.New("").Funcs(template.FuncMap{ - "add": func(a, b int) int { return a + b }, - "sub": func(a, b int) int { return a - b }, - "hasAnySuffix": func(s string, suffixes ...string) bool { - for _, suf := range suffixes { - if strings.HasSuffix(strings.ToLower(s), suf) { - return true - } - } - return false - }, - "dict": func(values ...interface{}) (map[string]interface{}, error) { - if len(values)%2 != 0 { - return nil, fmt.Errorf("dict requires an even number of args") - } - dict := make(map[string]interface{}, len(values)/2) - for i := 0; i < len(values); i += 2 { - key, ok := values[i].(string) - if !ok { - return nil, fmt.Errorf("dict keys must be strings") - } - dict[key] = values[i+1] - } - return dict, nil - }, - }).ParseGlob("templates/*.html")) - - http.HandleFunc("/", listFilesHandler) - http.HandleFunc("/add", uploadHandler) - http.HandleFunc("/add-yt", ytdlpHandler) - http.HandleFunc("/upload-url", uploadFromURLHandler) - http.HandleFunc("/file/", fileRouter) - http.HandleFunc("/tags", tagsHandler) - http.HandleFunc("/tag/", tagFilterHandler) - http.HandleFunc("/untagged", untaggedFilesHandler) - http.HandleFunc("/search", searchHandler) - http.HandleFunc("/bulk-tag", bulkTagHandler) - http.HandleFunc("/admin", adminHandler) - http.HandleFunc("/thumbnails/generate", generateThumbnailHandler) - http.HandleFunc("/cbz/", cbzViewerHandler) + // Initialize templates + tmpl, err = InitTemplates() + if err != nil { + log.Fatalf("Failed to load templates: %v", err) + } - http.Handle("/uploads/", http.StripPrefix("/uploads/", http.FileServer(http.Dir(config.UploadDir)))) - http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + // Register all routes + RegisterRoutes() + // Start server log.Printf("Server started at http://localhost%s", config.ServerPort) log.Printf("Database: %s", config.DatabasePath) log.Printf("Upload directory: %s", config.UploadDir) - http.ListenAndServe(config.ServerPort, nil) + + if err := http.ListenAndServe(config.ServerPort, nil); err != nil { + log.Fatalf("Server failed to start: %v", err) + } }