-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
259 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.idea | ||
tmp | ||
node_modules | ||
.DS_Store | ||
.DS_Store | ||
data/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ import "html/template" | |
type Article struct { | ||
Metadata Metadata | ||
Content template.HTML | ||
Comments []Comment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type Comment struct { | ||
Id int | ||
Username string | ||
PostId int | ||
Comment string | ||
Approved bool | ||
CreatedAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package store | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"os" | ||
"sync" | ||
|
||
_ "modernc.org/sqlite" | ||
) | ||
|
||
var ( | ||
instance *sql.DB | ||
once sync.Once | ||
) | ||
|
||
func Init() *sql.DB { | ||
once.Do(func() { | ||
path := "./data/db.sqlite" | ||
|
||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
log.Println("📂 SQLite file not found. Creating database...") | ||
if err := os.MkdirAll("data", 0755); err != nil { | ||
log.Fatalf("Failed to create data/ directory: %v", err) | ||
} | ||
} | ||
|
||
var err error | ||
instance, err = sql.Open("sqlite", path) | ||
if err != nil { | ||
log.Fatalf("SQLite connection error: %v", err) | ||
} | ||
|
||
if err = instance.Ping(); err != nil { | ||
log.Fatalf("SQLite ping error: %v", err) | ||
} | ||
|
||
createTables(instance) | ||
|
||
log.Println("✅ SQLite initialized") | ||
}) | ||
|
||
return instance | ||
} | ||
|
||
func Get() *sql.DB { | ||
if instance == nil { | ||
log.Fatal("🔴 SQLite not initialized. Call Init() first.") | ||
} | ||
|
||
return instance | ||
} | ||
|
||
func createTables(db *sql.DB) { | ||
query := ` | ||
CREATE TABLE IF NOT EXISTS comments ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
username VARCHAR(16) NOT NULL DEFAULT 'Anonymous', | ||
post_id INTEGER NOT NULL, | ||
comment TEXT NOT NULL, | ||
approved BOOLEAN DEFAULT FALSE, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
); | ||
` | ||
if _, err := db.Exec(query); err != nil { | ||
log.Fatalf("Error creating tables: %v", err) | ||
} | ||
log.Println("✅ Tables created (or already exist)") | ||
} |
Oops, something went wrong.