-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
10bcd9d
commit 10a10e9
Showing
37 changed files
with
3,332 additions
and
0 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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
|
@@ -19,3 +19,7 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
.env | ||
*.db | ||
tmp |
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,2 +1,5 @@ | ||
# mb | ||
|
||
micro-blog | ||
|
||
insiperd by https://github.com/l1mey112/me.l-m.dev |
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,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) | ||
} |
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,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" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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; |
Oops, something went wrong.