Works now, Mutex whatever

This commit is contained in:
Pablu23
2024-02-21 23:32:46 +01:00
parent fd8e5bbdbd
commit cc7009c106
3 changed files with 20 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"database/sql"
_ "embed"
"sync"
_ "github.com/mattn/go-sqlite3"
)
@@ -28,6 +29,8 @@ type Chapter struct {
type DatabaseManager struct {
ConnectionString string
db *sql.DB
rw *sync.Mutex
Mangas map[int]*Manga
Chapters map[int]*Chapter
@@ -37,7 +40,7 @@ type DatabaseManager struct {
func NewDatabase(connectionString string, createIfNotExists bool) DatabaseManager {
return DatabaseManager{
ConnectionString: connectionString,
rw: &sync.Mutex{},
Mangas: make(map[int]*Manga),
Chapters: make(map[int]*Chapter),
CreateIfNotExists: createIfNotExists,
@@ -75,6 +78,8 @@ func (dbMgr *DatabaseManager) Close() error {
func (dbMgr *DatabaseManager) Save() error {
db := dbMgr.db
dbMgr.rw.Lock()
defer dbMgr.rw.Unlock()
for _, m := range dbMgr.Mangas {
count := 0
err := db.QueryRow("SELECT COUNT(*) FROM Manga where ID = ?", m.Id).Scan(&count)
@@ -128,6 +133,10 @@ func (dbMgr *DatabaseManager) createDatabaseIfNotExists() error {
func (dbMgr *DatabaseManager) load() error {
db := dbMgr.db
dbMgr.rw.Lock()
defer dbMgr.rw.Unlock()
rows, err := db.Query("SELECT * FROM Manga")
if err != nil {
return err

17
main.go
View File

@@ -37,19 +37,6 @@ func main() {
return
}
//var latestTimeStamp int64 = 0
//var latestUrl string
//for _, m := range db.Mangas {
// if latestTimeStamp < m.LatestChapter.TimeStampUnix {
// latestTimeStamp = m.LatestChapter.TimeStampUnix
// latestUrl = m.LatestChapter.Url
// }
//}
//
//if latestUrl == "" {
// latestUrl = "/title/80381-i-stan-the-prince/1539086-ch_16"
//}
server := Server{
ImageBuffers: make(map[string]*bytes.Buffer),
NextReady: make(chan bool),
@@ -68,10 +55,6 @@ func main() {
}
}()
//server.LoadCurr()
//go server.LoadPrev()
//go server.LoadNext()
http.HandleFunc("/", server.HandleMenu)
http.HandleFunc("/new/title/{title}/{chapter}", server.HandleNew)
http.HandleFunc("/current/", server.HandleCurrent)

View File

@@ -188,7 +188,10 @@ func (s *Server) HandlePrev(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) HandleCurrent(w http.ResponseWriter, _ *http.Request) {
tmpl := template.Must(template.ParseFiles("test.gohtml"))
tmpl := template.Must(template.ParseFiles("viewer.gohtml"))
s.DbMgr.rw.Lock()
defer s.DbMgr.rw.Unlock()
mangaId, chapterId, err := getMangaIdAndChapterId(s.CurrSubUrl)
if err != nil {
@@ -311,6 +314,9 @@ func (s *Server) AppendImagesToBuf(html string) ([]Image, error) {
func (s *Server) HandleMenu(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("menu.gohtml"))
s.DbMgr.rw.Lock()
defer s.DbMgr.rw.Unlock()
all := s.DbMgr.Mangas
l := len(all)
mangaViewModels := make([]MangaViewModel, l)