Browse Source

Added project structure. Added database initialization and some basic API stuff

master
Noah Pederson 4 years ago
parent
commit
8d29dc8a16
16 changed files with 415 additions and 11 deletions
  1. +31
    -0
      .gitignore
  2. +1
    -0
      .idea/.name
  3. +22
    -0
      .idea/compiler.xml
  4. +3
    -0
      .idea/copyright/profiles_settings.xml
  5. +6
    -0
      .idea/encodings.xml
  6. +16
    -0
      .idea/libraries/GOPATH__gocomics_.xml
  7. +23
    -0
      .idea/misc.xml
  8. +8
    -0
      .idea/modules.xml
  9. +6
    -0
      .idea/vcs.xml
  10. +167
    -0
      database/database.go
  11. +14
    -0
      gocomics.go
  12. +10
    -0
      gocomics.iml
  13. +55
    -0
      models/comics.go
  14. +40
    -0
      scanner/comicscanner.go
  15. +6
    -0
      vendor/vendor.json
  16. +7
    -11
      web/api.go

+ 31
- 0
.gitignore View File

@@ -0,0 +1,31 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
comics

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

*.mdb
*.key
*.pem

+ 1
- 0
.idea/.name View File

@@ -0,0 +1 @@
GoComics

+ 22
- 0
.idea/compiler.xml View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

+ 3
- 0
.idea/copyright/profiles_settings.xml View File

@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>

+ 6
- 0
.idea/encodings.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

+ 16
- 0
.idea/libraries/GOPATH__gocomics_.xml View File

@@ -0,0 +1,16 @@
<component name="libraryTable">
<library name="GOPATH &lt;gocomics&gt;">
<CLASSES>
<root url="file://$PROJECT_DIR$/../../../github.com" />
<root url="file://$PROJECT_DIR$/../.." />
<root url="file://$PROJECT_DIR$/../../../golang.org" />
<root url="file://$PROJECT_DIR$/../../../9fans.net" />
<root url="file://$PROJECT_DIR$/../../../gopkg.in" />
</CLASSES>
<JAVADOC />
<SOURCES />
<excluded>
<root url="file://$PROJECT_DIR$" />
</excluded>
</library>
</component>

+ 23
- 0
.idea/misc.xml View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
<option name="mavenHome" value="Bundled (Maven 3)" />
</MavenGeneralSettings>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_3" default="false" assert-keyword="false" jdk-15="false" project-jdk-name="Go 1.6" project-jdk-type="Go SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

+ 8
- 0
.idea/modules.xml View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/gocomics.iml" filepath="$PROJECT_DIR$/gocomics.iml" />
</modules>
</component>
</project>

+ 6
- 0
.idea/vcs.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

+ 167
- 0
database/database.go View File

@@ -1 +1,168 @@
package database

import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
"git.chiefnoah.tech/chiefnoah/gocomics/models"
)

var db *sql.DB

func Init() {

var CREATE_USER_PROGRESS string = `CREATE TABLE IF NOT EXISTS "UserProgress" (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'ComicID' INTEGER,
'Read' INTEGER DEFAULT 0,
'Completed' INTEGER DEFAULT 0,
'DateLastRead' INTEGER DEFAULT 0,
'DateCompleted' INTEGER DEFAULT 0,
'LastReadPage' INTEGER DEFAULT 0,
'UserID' INTEGER,
FOREIGN KEY('ComicID') REFERENCES 'Comic'('ID'),
FOREIGN KEY('UserID') REFERENCES User(ID)
);`
var CREATE_USER string = `CREATE TABLE IF NOT EXISTS 'User' (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'Name' TEXT NOT NULL UNIQUE,
'Password' TEXT NOT NULL,
'APIKey' TEXT
);`
var CREATE_GENRES_BRIDGE string = `CREATE TABLE IF NOT EXISTS 'GenresBridge' (
'ComicID' INTEGER,
'GenreID' INTEGER,
FOREIGN KEY('ComicID') REFERENCES Comic(ID),
FOREIGN KEY('GenreID') REFERENCES Genres(ID)
);`
var CREATE_GENRES string = `CREATE TABLE IF NOT EXISTS 'Genres' (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'Genre' TEXT NOT NULL UNIQUE
);`
var CREATE_CREDIT string = `CREATE TABLE IF NOT EXISTS 'Credit' (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'Author' TEXT,
'Publisher' TEXT,
'Other' TEXT,
'ComicID' INTEGER,
FOREIGN KEY('ComicID') REFERENCES Comic(ID)
);`
var CREATE_COMIC_FILE string = `CREATE TABLE IF NOT EXISTS 'ComicFile' (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'RelativePath' TEXT NOT NULL,
'AbsolutePath' TEXT NOT NULL,
'Hash' TEXT NOT NULL UNIQUE,
'Filesize' INTEGER DEFAULT 0
);`
var CREATE_COMIC string = `CREATE TABLE IF NOT EXISTS "Comic" (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'Title' TEXT NOT NULL,
'Series' TEXT,
'IssueNumber' REAL DEFAULT 0.0,
'PageCount' INTEGER,
'ComicFileID' INTEGER,
'Volume' TEXT,
'DateAdded' INTEGER DEFAULT 0,
'PublishDate' INTEGER,
'Synopsis' TEXT,
'Rating' REAL DEFAULT 0.0,
'Status' TEXT,
FOREIGN KEY('ComicFileID') REFERENCES 'ComicFile'('ID')
);`
var CREATE_CHARACTERS_BRIDGE string = `CREATE TABLE IF NOT EXISTS 'CharactersBridge' (
'ComicID' INTEGER NOT NULL,
'CharacterID' INTEGER NOT NULL,
FOREIGN KEY('ComicID') REFERENCES Comic(ID),
FOREIGN KEY('CharacterID') REFERENCES Character(ID)
);`
var CREATE_CHARACTER string = `CREATE TABLE IF NOT EXISTS "Character" (
'ID' INTEGER PRIMARY KEY AUTOINCREMENT,
'Name' TEXT NOT NULL UNIQUE
);`
var CREATE_BOOKMARKS string = `CREATE TABLE IF NOT EXISTS 'Bookmarks' (
'ComicID' INTEGER,
'PageNumber' INTEGER NOT NULL,
FOREIGN KEY('ComicID') REFERENCES Comic(ID)
);`
var PRAGMAS string = `PRAGMA foreign_keys = ON; VACUUM`

db, err := sql.Open("sqlite3", "./library.mdb")
if err != nil {
log.Fatal("Unable to open database: ", err)
}

defer db.Close()

_, err = db.Exec(CREATE_USER_PROGRESS)
if err != nil {
log.Fatal(err)
}

_, err = db.Exec(CREATE_USER)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_GENRES_BRIDGE)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_GENRES)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_CREDIT)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_COMIC_FILE)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_COMIC)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_CHARACTERS_BRIDGE)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_CHARACTER)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(CREATE_BOOKMARKS)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec(PRAGMAS)
if err != nil {
log.Fatal(err)
}

log.Print("Done initializing database")

}

func AddComic(comic models.ComicInfo) bool {
db, err := sql.Open("sqlite3", "./library.mdb")
if err != nil {
log.Fatal("Unable to open database: ", err)
}

INSERT_COMIC_INFO := "INSERT OR IGNORE INTO comic_info(filesize, date_added, hash) VALUES(?, ?, ?);"
//INSERT_COMIC := "INSERT OR IGNORE INTO comic(parentId, comicInfoId, fileName, path) VALUES ((SELECT id FROM folder WHERE path = ?), ?, ?, ?)"

tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}

stmt, err := tx.Prepare(INSERT_COMIC_INFO)
if err != nil {
log.Fatal(err)
}

defer stmt.Close()

return false
}

+ 14
- 0
gocomics.go View File

@@ -1,9 +1,23 @@
package main

import (
"git.chiefnoah.tech/chiefnoah/gocomics/config"
"git.chiefnoah.tech/chiefnoah/gocomics/web"
"git.chiefnoah.tech/chiefnoah/gocomics/scanner"

"git.chiefnoah.tech/chiefnoah/gocomics/database"
)

//Let's get started!
func main() {

database.Init()

config := &config.ApiConfig{
false, false, ":3008", ":3000",
}
comicscanner.Scan("./comics")
web.Start(config)


}

+ 10
- 0
gocomics.iml View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="GO_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Go 1.6" jdkType="Go SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="GOPATH &lt;gocomics&gt;" level="project" />
</component>
</module>

+ 55
- 0
models/comics.go View File

@@ -0,0 +1,55 @@
package models


type ComicInfo struct {

ID int `json:"id"`
Title string `json:"title"`
Series string `json:"series"`
IssueNumber float32 `json:"issue_number"`
PageCount int `json:"page_count"`
Credits credit `json:"credits"`
Volume string `json:"volume"`
Genres []string `json:"genres"`
DateAdded int `json:"date_added"`
PublishDate int `json:"publish_date"`
Synopsis string `json:"synopsis"`
Characters []string `json:"characters"`
Rating float32 `json:"rating"`
Status string `json:"status"`
Bookmarks []int `json:"bookmarks"`
Other []string `json:"other"` ////Other tags are formatted "[tagname]:[tag]" semicolon delimited
}

type ComicFile struct {
ID int `json:"id"`
RelativePath string `json:"relative_path"`
AbsolutePath string `json:"absolute_path"`
Hash string `json:"hash"` //MD5 hash
FileSize int `json:"filesize"`
}

type credit struct {
Author string `json:"author"`
Artist string `json:"artist"`
Publisher string `json:"publisher"`
Other string `json:"other"`

}

type User struct {
ID int `json:"id"`
Name string `json:"name"`
Password string `json:"-"`
APIKeys []string `json:"api_keys"`
}

type UserProgress struct {
ID int `json:"id"`
ComicInfoID int `json:"comic_info_id"`
Read bool `json:"read"`
Completed bool `json:"completed"`
DateLastRead int `json:"date_last_read"`
DateCompleted int `json:"date_completed"`
LastReadPage int `json:"last_read_page"`
}

+ 40
- 0
scanner/comicscanner.go View File

@@ -0,0 +1,40 @@
package comicscanner

import (
//"github.com/fsnotify/fsnotify"
"path"
"strings"
"path/filepath"
"os"
"fmt"
"crypto/md5"
"io/ioutil"
)

func Scan(f string) error {
err := filepath.Walk(f, visit)
if err != nil {
fmt.Printf("walk error: %v\n", err)
return err
}
return nil
}

func visit(p string, f os.FileInfo, e error) error {

fmt.Printf("Visited: %s\n", p)
if strings.EqualFold(path.Ext(f.Name()), ".cbz") || strings.EqualFold(path.Ext(f.Name()), ".cbr") {
fmt.Printf("Found cbz file!\n")

//TODO: parse comic info
file, _ := ioutil.ReadFile(p)
md5 := md5.Sum(file)
fmt.Printf("MD5: %x\n", md5)

}
return nil
}

func watch(f []string) error {
return nil
}

+ 6
- 0
vendor/vendor.json View File

@@ -0,0 +1,6 @@
{
"comment": "",
"ignore": "test",
"package": [],
"rootPath": "git.chiefnoah.tech/chiefnoah/gocomics"
}

+ 7
- 11
web/api.go View File

@@ -2,8 +2,8 @@ package web

import (
"git.chiefnoah.tech/chiefnoah/gocomics/config"
"github.com/gin-gonic/gin"
"net/http"
"io"
)

//This is where the REST API stuff will go
@@ -11,16 +11,12 @@ import (
//Starts the API server and registers handlers
func Start(c *config.ApiConfig) {
//TODO: register handlers
http.HandleFunc("/", RootHandler)
router := gin.Default()

if c.UseTLS || c.ForceTLS {
http.ListenAndServeTLS(c.SSLPort, "cert.pem", "key.pem", nil)
}
if !c.ForceTLS {
http.ListenAndServe(c.HttpPort, nil)
}
router.GET("/", rootHandler)
router.RunTLS(c.HttpPort, "./test.pem", "./test.key")
}

func RootHandler(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "error... nothing here :(")
}
func rootHandler(c *gin.Context) {
c.String(http.StatusOK, "hi")
}

Loading…
Cancel
Save