Skip to content

Commit

Permalink
Add simple SQLite db functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Faiz Jazadi <[email protected]>
  • Loading branch information
lc-at committed Nov 30, 2023
1 parent ff6dd93 commit fc945c8
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 41 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module codeberg.org/fj/lcat.dev

go 1.19

require github.com/mattn/go-sqlite3 v1.14.18 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI=
github.com/mattn/go-sqlite3 v1.14.18/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
12 changes: 11 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
)

type Config struct {
ListenAddr string
ListenAddr string
DatabasePath string
TemplatesDir string
Debug bool
}

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

if getEnvOrDefault("DEBUG", "") == "1" {
config.Debug = true
} else {
config.Debug = constants.DefaultDebug
}
return
}
5 changes: 4 additions & 1 deletion internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package constants

const (
DefaultListenAddr = ":8080"
DefaultListenAddr = ":8080"
DefaultTemplatesDir = "web"
DefaultDatabasePath = "db.sqlite3"
DefaultDebug = false
)
16 changes: 12 additions & 4 deletions internal/core/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package core

import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"net/http"

"codeberg.org/fj/lcat.dev/internal/config"
"codeberg.org/fj/lcat.dev/internal/front"
"codeberg.org/fj/lcat.dev/internal/templates"
"codeberg.org/fj/lcat.dev/internal/logpost"
"codeberg.org/fj/lcat.dev/internal/views"
)

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

frontHandler := front.New(templateManager)
db, err := sql.Open("sqlite3", config.DatabasePath)
if err != nil {
return
}

logpostManager := logpost.NewManager(db)
templateManager := views.NewManager(config.TemplatesDir, config.Debug)

frontHandler := front.New(templateManager, logpostManager)
frontHandler.Register(app.mux)

return
Expand Down
16 changes: 11 additions & 5 deletions internal/front/front.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"net/http"

"codeberg.org/fj/lcat.dev/internal/logpost"
"codeberg.org/fj/lcat.dev/internal/templates"
"codeberg.org/fj/lcat.dev/internal/views"
)

type Handler struct {
tm *templates.Manager
viewsManager *views.Manager
logpostManager *logpost.Manager
}

func New(tm *templates.Manager, logpostManager *logpost.Manager) *Handler {
func New(viewsManager *views.Manager, logpostManager *logpost.Manager) *Handler {
return &Handler{
tm: tm,
viewsManager: viewsManager,
logpostManager: logpostManager,
}
}
Expand All @@ -24,5 +24,11 @@ func (h *Handler) Register(mux *http.ServeMux) {
}

func (h *Handler) index(w http.ResponseWriter, r *http.Request) {
h.tm.Get(templates.Index).Execute(w, nil)
logposts, err := h.logpostManager.GetAllPinnedFirst(20, 0)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

h.viewsManager.Get(views.Index).Execute(w, logposts)
}
22 changes: 13 additions & 9 deletions internal/logpost/logpost.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package logpost
import "database/sql"

type Model struct {
id int
title string
pinned bool
content string
created_at string
updated_at string
Id string
Title string
Content string
IsPinned bool
IsMarkdown bool
CreatedAt string
UpdatedAt string
}

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

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

for rows.Next() {
var post Model
err := rows.Scan(&post.id, &post.title, &post.pinned, &post.content, &post.created_at, &post.updated_at)
err := rows.Scan(&post.Id, &post.Title, &post.Content, &post.IsPinned, &post.IsMarkdown, &post.CreatedAt, &post.UpdatedAt)
if err != nil {
panic(err)
}
Expand Down
38 changes: 38 additions & 0 deletions internal/views/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package views

import (
"html/template"
"path"
)

const Index = "index"

type Manager struct {
templates map[string]*template.Template
templatesDir string
alwaysCompile bool
}

func NewManager(templatesDir string, alwaysCompile bool) *Manager {
t := new(Manager)
t.templates = make(map[string]*template.Template)
t.templatesDir = templatesDir
t.alwaysCompile = alwaysCompile
t.compile()
return t
}

func (t *Manager) templateFilename(filename string) string {
return path.Join(t.templatesDir, filename)
}

func (t *Manager) compile() {
t.templates[Index] = template.Must(template.ParseFiles(t.templateFilename("index.html")))
}

func (t *Manager) Get(name string) *template.Template {
if t.alwaysCompile {
t.compile()
}
return t.templates[name]
}
39 changes: 18 additions & 21 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

body {
font-family: monospace;
margin:1.5em;
margin:1em;
font-size:1rem;
}

Expand All @@ -24,8 +24,7 @@

@media (min-width: 40rem){
body {
font-size: 1.125rem;
padding: 0rem 20%;
padding: 0rem 10%;
}
}

Expand Down Expand Up @@ -76,25 +75,23 @@ <h1>&lt;cat</h1>
</nav>

<main>
<h4>2023-11</h4>
<ol>
<li><a href="#">Hello world!</a></li>
<li><a href="#">Hello world!</a></li>
</ol>
<table style="width:100%">
<tr>
<th>Timestamp</th>
<th>Title</th>
</tr>
{{ range . }}
<tr>
<td>{{ .CreatedAt }}</td>
<td>
<a href="/{{ .Id }}">
{{ if .IsPinned }}[📌]{{ end }}

<h4>2023-08</h4>
<ol>
<li><a href="#">Hello world!</a></li>
<li><a href="#">Hello world!</a></li>
</ol>

<form method="get">
<label>limit:</label>
<input type="number" style="width: 3em" name="limit">
<label>offset:</label>
<input type="number" style="width: 3em" name="limit">
<button type="submit">submit</button>
</form>
{{ .Title }}
</a>
</td>
</tr>
{{ end }}
</main>
</body>
</html>

0 comments on commit fc945c8

Please sign in to comment.