Skip to content

Commit fc945c8

Browse files
committed
Add simple SQLite db functionality
Signed-off-by: Faiz Jazadi <[email protected]>
1 parent ff6dd93 commit fc945c8

File tree

9 files changed

+111
-41
lines changed

9 files changed

+111
-41
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module codeberg.org/fj/lcat.dev
22

33
go 1.19
4+
5+
require github.com/mattn/go-sqlite3 v1.14.18 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
2+
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

internal/config/config.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import (
66
)
77

88
type Config struct {
9-
ListenAddr string
9+
ListenAddr string
10+
DatabasePath string
11+
TemplatesDir string
12+
Debug bool
1013
}
1114

1215
func getEnvOrDefault(key, fallback string) string {
@@ -19,6 +22,13 @@ func getEnvOrDefault(key, fallback string) string {
1922
func NewFromEnv() (config *Config, err error) {
2023
config = new(Config)
2124
config.ListenAddr = getEnvOrDefault("LISTEN_ADDR", constants.DefaultListenAddr)
25+
config.DatabasePath = getEnvOrDefault("DATABASE_PATH", constants.DefaultDatabasePath)
26+
config.TemplatesDir = getEnvOrDefault("TEMPLATES_DIR", constants.DefaultTemplatesDir)
2227

28+
if getEnvOrDefault("DEBUG", "") == "1" {
29+
config.Debug = true
30+
} else {
31+
config.Debug = constants.DefaultDebug
32+
}
2333
return
2434
}

internal/constants/constants.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package constants
22

33
const (
4-
DefaultListenAddr = ":8080"
4+
DefaultListenAddr = ":8080"
5+
DefaultTemplatesDir = "web"
6+
DefaultDatabasePath = "db.sqlite3"
7+
DefaultDebug = false
58
)

internal/core/app.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package core
22

33
import (
44
"database/sql"
5+
_ "github.com/mattn/go-sqlite3"
56
"net/http"
67

78
"codeberg.org/fj/lcat.dev/internal/config"
89
"codeberg.org/fj/lcat.dev/internal/front"
9-
"codeberg.org/fj/lcat.dev/internal/templates"
10+
"codeberg.org/fj/lcat.dev/internal/logpost"
11+
"codeberg.org/fj/lcat.dev/internal/views"
1012
)
1113

1214
type App struct {
@@ -19,10 +21,16 @@ func New(config *config.Config) (app *App, err error) {
1921
mux: http.NewServeMux(),
2022
config: config,
2123
}
22-
templateManager := templates.New("web")
23-
db := sql.Open(k)
2424

25-
frontHandler := front.New(templateManager)
25+
db, err := sql.Open("sqlite3", config.DatabasePath)
26+
if err != nil {
27+
return
28+
}
29+
30+
logpostManager := logpost.NewManager(db)
31+
templateManager := views.NewManager(config.TemplatesDir, config.Debug)
32+
33+
frontHandler := front.New(templateManager, logpostManager)
2634
frontHandler.Register(app.mux)
2735

2836
return

internal/front/front.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import (
44
"net/http"
55

66
"codeberg.org/fj/lcat.dev/internal/logpost"
7-
"codeberg.org/fj/lcat.dev/internal/templates"
7+
"codeberg.org/fj/lcat.dev/internal/views"
88
)
99

1010
type Handler struct {
11-
tm *templates.Manager
11+
viewsManager *views.Manager
1212
logpostManager *logpost.Manager
1313
}
1414

15-
func New(tm *templates.Manager, logpostManager *logpost.Manager) *Handler {
15+
func New(viewsManager *views.Manager, logpostManager *logpost.Manager) *Handler {
1616
return &Handler{
17-
tm: tm,
17+
viewsManager: viewsManager,
1818
logpostManager: logpostManager,
1919
}
2020
}
@@ -24,5 +24,11 @@ func (h *Handler) Register(mux *http.ServeMux) {
2424
}
2525

2626
func (h *Handler) index(w http.ResponseWriter, r *http.Request) {
27-
h.tm.Get(templates.Index).Execute(w, nil)
27+
logposts, err := h.logpostManager.GetAllPinnedFirst(20, 0)
28+
if err != nil {
29+
http.Error(w, err.Error(), http.StatusInternalServerError)
30+
return
31+
}
32+
33+
h.viewsManager.Get(views.Index).Execute(w, logposts)
2834
}

internal/logpost/logpost.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package logpost
33
import "database/sql"
44

55
type Model struct {
6-
id int
7-
title string
8-
pinned bool
9-
content string
10-
created_at string
11-
updated_at string
6+
Id string
7+
Title string
8+
Content string
9+
IsPinned bool
10+
IsMarkdown bool
11+
CreatedAt string
12+
UpdatedAt string
1213
}
1314

1415
type Manager struct {
@@ -19,16 +20,19 @@ func NewManager(db *sql.DB) *Manager {
1920
return &Manager{db: db}
2021
}
2122

22-
func (m *Manager) GetAll() (posts []Model, err error) {
23-
rows, err := m.db.Query("SELECT * FROM log_posts")
23+
func (m *Manager) GetAllPinnedFirst(limit, offset int) (posts []Model, err error) {
24+
rows, err := m.db.Query(`SELECT id, title, content, COALESCE(is_pinned, FALSE), is_markdown, created, last_updated
25+
FROM log_post
26+
ORDER BY is_pinned DESC, created DESC
27+
LIMIT ? OFFSET ?`, limit, offset)
2428
if err != nil {
2529
panic(err)
2630
}
2731
defer rows.Close()
2832

2933
for rows.Next() {
3034
var post Model
31-
err := rows.Scan(&post.id, &post.title, &post.pinned, &post.content, &post.created_at, &post.updated_at)
35+
err := rows.Scan(&post.Id, &post.Title, &post.Content, &post.IsPinned, &post.IsMarkdown, &post.CreatedAt, &post.UpdatedAt)
3236
if err != nil {
3337
panic(err)
3438
}

internal/views/views.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package views
2+
3+
import (
4+
"html/template"
5+
"path"
6+
)
7+
8+
const Index = "index"
9+
10+
type Manager struct {
11+
templates map[string]*template.Template
12+
templatesDir string
13+
alwaysCompile bool
14+
}
15+
16+
func NewManager(templatesDir string, alwaysCompile bool) *Manager {
17+
t := new(Manager)
18+
t.templates = make(map[string]*template.Template)
19+
t.templatesDir = templatesDir
20+
t.alwaysCompile = alwaysCompile
21+
t.compile()
22+
return t
23+
}
24+
25+
func (t *Manager) templateFilename(filename string) string {
26+
return path.Join(t.templatesDir, filename)
27+
}
28+
29+
func (t *Manager) compile() {
30+
t.templates[Index] = template.Must(template.ParseFiles(t.templateFilename("index.html")))
31+
}
32+
33+
func (t *Manager) Get(name string) *template.Template {
34+
if t.alwaysCompile {
35+
t.compile()
36+
}
37+
return t.templates[name]
38+
}

web/index.html

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
body {
1414
font-family: monospace;
15-
margin:1.5em;
15+
margin:1em;
1616
font-size:1rem;
1717
}
1818

@@ -24,8 +24,7 @@
2424

2525
@media (min-width: 40rem){
2626
body {
27-
font-size: 1.125rem;
28-
padding: 0rem 20%;
27+
padding: 0rem 10%;
2928
}
3029
}
3130

@@ -76,25 +75,23 @@ <h1>&lt;cat</h1>
7675
</nav>
7776

7877
<main>
79-
<h4>2023-11</h4>
80-
<ol>
81-
<li><a href="#">Hello world!</a></li>
82-
<li><a href="#">Hello world!</a></li>
83-
</ol>
78+
<table style="width:100%">
79+
<tr>
80+
<th>Timestamp</th>
81+
<th>Title</th>
82+
</tr>
83+
{{ range . }}
84+
<tr>
85+
<td>{{ .CreatedAt }}</td>
86+
<td>
87+
<a href="/{{ .Id }}">
88+
{{ if .IsPinned }}[📌]{{ end }}
8489

85-
<h4>2023-08</h4>
86-
<ol>
87-
<li><a href="#">Hello world!</a></li>
88-
<li><a href="#">Hello world!</a></li>
89-
</ol>
90-
91-
<form method="get">
92-
<label>limit:</label>
93-
<input type="number" style="width: 3em" name="limit">
94-
<label>offset:</label>
95-
<input type="number" style="width: 3em" name="limit">
96-
<button type="submit">submit</button>
97-
</form>
90+
{{ .Title }}
91+
</a>
92+
</td>
93+
</tr>
94+
{{ end }}
9895
</main>
9996
</body>
10097
</html>

0 commit comments

Comments
 (0)