Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Pineapple217 committed Jan 17, 2024
1 parent 10bcd9d commit 10a10e9
Show file tree
Hide file tree
Showing 37 changed files with 3,332 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "tmp\\main.exe"
cmd = "go build -o ./tmp/main.exe ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = ["database\\models.go", "database\\query.sql.go"]
exclude_regex = ["_test.go", ".*_templ.go", ".*.db"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = [""]
include_ext = ["go", "tpl", "tmpl", "templ", "html", "sql"]
include_file = ["main.go"]
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = ["templ generate", "sqlc generate"]
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

# Go workspace file
go.work

.env
*.db
tmp
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# mb

micro-blog

insiperd by https://github.com/l1mey112/me.l-m.dev
25 changes: 25 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package auth

import (
"crypto/rand"
"math/big"
)

var SecretPassword string = generateRandomPassword(32)

var SecretCookie [32]byte

func generateRandomPassword(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*"
password := make([]byte, length)

for i := range password {
randomIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "password"
}
password[i] = charset[randomIndex.Int64()]
}

return string(password)
}
22 changes: 22 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package context

import "context"

func IsAuth(ctx context.Context) bool {
if isAuth, ok := ctx.Value(AuthContextKey).(bool); ok {
return isAuth
}
return false
}

func GetPostCount(ctx context.Context) string {
if postCount, ok := ctx.Value(PostCountContextKey).(string); ok {
return postCount
}
return "???"
}

type contextKey string

var AuthContextKey contextKey = "isAuth"
var PostCountContextKey contextKey = "postCount"
31 changes: 31 additions & 0 deletions database/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions database/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package database

import (
"context"
"database/sql"
"fmt"

_ "embed"

_ "github.com/mattn/go-sqlite3"
)

var (
//go:embed schema.sql
ddl string
queries *Queries
)

func Init(databaseSource string) {
ctx := context.Background()
db, err := sql.Open("sqlite3", databaseSource)
if err != nil {
panic(err)
}

// create tables
if _, err := db.ExecContext(ctx, ddl); err != nil {
panic(err)
}
queries = New(db)
}

func GetQueries() *Queries {
return queries
}

const queryPosts = `SELECT id, created_at, tags, content FROM posts`
const queryPostsOrder = ` ORDER BY created_at DESC`

func (q *Queries) QueryPost(ctx context.Context, tags []string, search string) ([]Post, error) {
query := queryPosts

if search != "" {
query += fmt.Sprintf(" where (content glob '*%s*' collate nocase)", search)
}

if len(tags) != 0 {
if search != "" {
query += " and ("
} else {
query += " where ("
}

for i, tag := range tags {
// tag := strings.NewReplacer("_", "\\_", "%", "\\%").Replace(t)
query += fmt.Sprintf("tags like '%%%s%%' escape '\\'", tag)

if i+1 < len(tags) {
query += " and "
} else {
query += ")"
}
}
}
query += queryPostsOrder

rows, err := q.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Post
for rows.Next() {
var i Post
if err := rows.Scan(
&i.ID,
&i.CreatedAt,
&i.Tags,
&i.Content,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
32 changes: 32 additions & 0 deletions database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions database/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- name: GetPost :one
SELECT * FROM posts
WHERE created_at = ? LIMIT 1;

-- name: ListPosts :many
SELECT * FROM posts
ORDER BY created_at DESC;

-- name: CreatePost :one
INSERT INTO posts (
created_at, tags, content
) VALUES (
strftime('%s', 'now'), ?, ?
)
RETURNING *;

-- name: UpdatePost :exec
UPDATE posts
set tags = ?,
content = ?
WHERE created_at = ?;

-- name: DeletePost :exec
DELETE FROM posts
WHERE created_at = ?;

-- name: GetPostCount :one
SELECT COUNT(*)
FROM posts;

-- name: CreateSpotifyCache :one
INSERT INTO spotify_cache (
track_id, track_name, artist_name, artist_id, cover_art_url, audio_preview_url
) VALUES (
?, ?, ?, ?, ?, ?
)
RETURNING *;

-- name: GetSpotifyCache :one
SELECT * FROM spotify_cache
WHERE track_id = ? LIMIT 1;

-- name: CreateTYThumbCache :one
INSERT INTO yt_thumb_cache (
yt_id, yt_thumb
) VALUES (
?, ?
)
RETURNING *;

-- name: GetYoutubeCache :one
SELECT * FROM yt_thumb_cache
WHERE yt_id = ? LIMIT 1;

-- name: GetTagsCount :one
WITH split(tag, tags_remaining) AS (
-- Initial query
SELECT
'',
tags || ' '
FROM posts
UNION ALL
SELECT
trim(substr(tags_remaining, 0, instr(tags_remaining, ' '))),
substr(tags_remaining, instr(tags_remaining, ' ') + 1)
FROM split
WHERE tags_remaining != ''
)
SELECT COUNT(DISTINCT tag) AS unique_tag_count
FROM split
WHERE tag != '';

-- name: GetAllTags :many
WITH split (
tag,
tags_remaining
)
AS (-- Initial query
SELECT '',
tags || ' '
FROM posts
UNION ALL
SELECT trim(substr(tags_remaining, 0, instr(tags_remaining, ' ') ) ),
substr(tags_remaining, instr(tags_remaining, ' ') + 1)
FROM split
WHERE tags_remaining != ''
)
SELECT MIN(tag) as tag,
COUNT( * ) AS tag_count
FROM split
WHERE tag != ''
GROUP BY tag
ORDER BY tag_count DESC;
Loading

0 comments on commit 10a10e9

Please sign in to comment.