Skip to content

Commit

Permalink
feat: 切换数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua committed Feb 2, 2025
1 parent f993141 commit 352b25a
Show file tree
Hide file tree
Showing 23 changed files with 177 additions and 62 deletions.
2 changes: 1 addition & 1 deletion backend/api/memo/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func GetUserInfo(c *fiber.Ctx) error {
if err != nil {
return err
}
if len(parserHeader.Authorization) < 6 {
if len(parserHeader.Authorization) <= 7 {
return errors.New("未鉴权")
}
// log.Println(parserHeader.Authorization[7:])
Expand Down
9 changes: 9 additions & 0 deletions backend/api/user/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package user
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -53,6 +54,14 @@ func registryUser(c *fiber.Ctx) error {

_store, _ := c.Locals("store").(*store.Store)

if len(userCreate.Password) < 4 {
return errors.New("密码长度正确")
}

if len(userCreate.Name) == 0 {
return errors.New("用户名不能为空")
}

err = store.CreateUser(store.UserCreate{
Name: userCreate.Name,
Email: userCreate.Email,
Expand Down
20 changes: 19 additions & 1 deletion backend/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"log"

"github.com/gofiber/fiber/v2"
Expand All @@ -9,11 +10,28 @@ import (
"github.com/kehuay/aimemos/api/resource"
"github.com/kehuay/aimemos/api/user"
"github.com/kehuay/aimemos/store"
"github.com/kehuay/aimemos/store/db"
)

func main() {

_store, err := store.NewStore()
dbConfig := db.NewDefaultDBConfig()

var host = flag.String("host", dbConfig.Host, "db host")
var port = flag.String("port", dbConfig.Port, "db port")
var dbuser = flag.String("user", dbConfig.User, "db user")
var dbname = flag.String("dbname", dbConfig.DBName, "db database")
var dbpassword = flag.String("password", dbConfig.Password, "db password")

flag.Parse()

dbConfig.Host = *host
dbConfig.Port = *port
dbConfig.User = *dbuser
dbConfig.DBName = *dbname
dbConfig.Password = *dbpassword

_store, err := store.NewStore(dbConfig)

if err != nil {
log.Fatal("数据库配置失败")
Expand Down
34 changes: 31 additions & 3 deletions backend/store/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,42 @@ package db

import (
"database/sql"
"fmt"

_ "github.com/lib/pq"
)

const Dsn = "host=14.103.71.103 port=6432 user=aimemos password=aimemos%2025 dbname=aimemos sslmode=disable TimeZone=Asia/Shanghai"
type DBConfig struct {
Host string
Port string
User string
Password string
DBName string
}

func NewDefaultDBConfig() DBConfig {
return DBConfig{
Host: "127.0.0.1",
Port: "6432",
User: "postgres",
Password: "postgres",
DBName: "postgres",
}
}

func NewPostgresDB(config DBConfig) (*sql.DB, error) {

func NewPostgresDB() (*sql.DB, error) {
db, err := sql.Open("postgres", Dsn)
db, err := sql.Open(
"postgres",
fmt.Sprintf(
"host=%s port=%s user=%s password=%s dbname=%s sslmode=disable TimeZone=Asia/Shanghai",
config.Host,
config.Port,
config.User,
config.Password,
config.DBName,
),
)

return db, err
}
23 changes: 12 additions & 11 deletions backend/store/memo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Memo struct {
type MemoStatus string

const (
MemoStatusDraft MemoStatus = "draft"
MemoStatusActive MemoStatus = "active"
MemoStatusDraft MemoStatus = "private"
MemoStatusActive MemoStatus = "public"
MemoStatusArchived MemoStatus = "archived"
)

Expand Down Expand Up @@ -110,7 +110,7 @@ func CreateMemo(memoCreate MemoCreate, store Store) (Memo, error) {
}

err = tx.QueryRow(
"insert into memos (content, creator_id) values ($1, $2) returning id, create_at, update_at;",
"insert into memos (markdown, creator_id) values ($1, $2) returning id, created_at, updated_at;",
memoCreate.Content,
memoCreate.CreatorId,
).Scan(&memo.Id, &memo.CreateAt, &memo.UpdateAt)
Expand Down Expand Up @@ -183,7 +183,7 @@ func UpdateMemo(memoUpdate MemoUpdate, store Store) (Memo, error) {
}

err = tx.QueryRow(
"update memos set content=$1 where id=$2 returning id, create_at, update_at;",
"update memos set markdown=$1 where id=$2 returning id, created_at, updated_at;",
memoUpdate.Content,
memoUpdate.Id,
).Scan(&memo.Id, &memo.CreateAt, &memo.UpdateAt)
Expand All @@ -206,14 +206,14 @@ func QueryMemos(memoQuery MemoQuery, store Store) ([]Memo, error) {

if memoQuery.PageNo != 0 && memoQuery.PageSize != 0 {
rows, err = store.db.Query(
"select memos.*, users.username from memos left join users on memos.creator_id = users.id where creator_id=$1 order by create_at desc limit $2 offset $3",
"select memos.id, memos.markdown, memos.created_at, memos.updated_at, memos.creator_id, memos.status, users.username from memos left join users on memos.creator_id = users.id where creator_id=$1 order by created_at desc limit $2 offset $3",
memoQuery.CreatorId,
memoQuery.PageSize,
(memoQuery.PageNo-1)*memoQuery.PageSize,
)
} else {
rows, err = store.db.Query(
"select memos.*, users.username from memos left join users on memos.creator_id = users.id where creator_id=$1 order by create_at desc",
"select memos.id, memos.markdown, memos.created_at, memos.updated_at, memos.creator_id, memos.status, users.username from memos left join users on memos.creator_id = users.id where creator_id=$1 order by created_at desc",
memoQuery.CreatorId,
)
}
Expand All @@ -228,14 +228,15 @@ func QueryMemos(memoQuery MemoQuery, store Store) ([]Memo, error) {

for rows.Next() {
memo := Memo{}
err = rows.Scan(&memo.Id,
err = rows.Scan(
&memo.Id,
&memo.Content,
&memo.CreateAt,
&memo.UpdateAt,
&memo.CreatorId,
&memo.IsFixed,
&memo.Status,
&memo.CreatorName)
&memo.CreatorName,
)
if err != nil {
return []Memo{}, err
}
Expand All @@ -250,10 +251,10 @@ func QueryMemoById(id string, creator_id string, store Store) (Memo, error) {
memo := Memo{}

err := store.db.QueryRow(
"select * from memos where id=$1 and creator_id=$2",
"select memos.id, memos.markdown, memos.created_at, memos.updated_at, memos.creator_id, memos.status from memos where id=$1 and creator_id=$2",
id,
creator_id,
).Scan(&memo.Id, &memo.Content, &memo.CreateAt, &memo.UpdateAt, &memo.CreatorId, &memo.IsFixed, &memo.Status)
).Scan(&memo.Id, &memo.Content, &memo.CreateAt, &memo.UpdateAt, &memo.CreatorId, &memo.Status)

return memo, err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/store/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Resource struct {
func CreateResource(resource Resource, store Store) (Resource, error) {

err := store.db.QueryRow(
"insert into resources (id, type, data,creator_id) values ($1, $2, $3, $4) returning id, type, data",
"insert into resources (id, type, raw,creator_id) values ($1, $2, $3, $4) returning id, type, raw",
resource.Id,
resource.Type,
resource.Data,
Expand All @@ -28,7 +28,7 @@ func QueryResource(id string, store Store) (Resource, error) {
resource := Resource{}

err := store.db.QueryRow(
"select id,type,data from resources where id = $1;",
"select id,type,raw from resources where id = $1;",
id,
).Scan(&resource.Id, &resource.Type, &resource.Data)

Expand Down
4 changes: 2 additions & 2 deletions backend/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ type Store struct {
db *sql.DB
}

func NewStore() (Store, error) {
_db, err := db.NewPostgresDB()
func NewStore(config db.DBConfig) (Store, error) {
_db, err := db.NewPostgresDB(config)
if err != nil {
return Store{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.png" />
<link rel="icon" type="image/svg+xml" href="/writing.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Writing Hub</title>
</head>
Expand Down
26 changes: 1 addition & 25 deletions mark-parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,7 @@ import (

func main() {

text := `### 个人'SOP'shadj实际~达成共识~结果适当补偿技术差距时都会保持身材不好就很多首班车时间不长的时间
- [ ] 晚上备好隔天的衣服
- [X] 起床先'喝一杯温热水'
- [ ] 睡够8小时
- [ ] 早起列当天Todo List
✅ 喝水喝水
'''go
package main
import "fmt"
func main(){
fmt.Println("Hello, Huy!")
}
'''
- 1
- 2
- 3
这是一个'Markdown'文本解析器
`
text := "[使用Golang实现的代码高亮库](https://github.com/alecthomas/chroma)\n"

bytes := []byte(text)
tokens := parser.Parser(bytes)
Expand Down
2 changes: 1 addition & 1 deletion mark-parser/parser/blocks/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package blocks
import "regexp"

const (
CodeRegexp = `^'''(.*)\s((?s).*)\s'''`
CodeRegexp = "^```(.*)\n((?s).*)\n```"
)

func FindCodeIndex(text []byte) (Indexes, bool) {
Expand Down
2 changes: 1 addition & 1 deletion mark-parser/parser/inline/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package inline
import "regexp"

const (
CodeRegexp = `^'(.*?)'`
CodeRegexp = "^`(.*?)`"
)

func FindCodeIndex(text []byte) (Indexes, bool) {
Expand Down
3 changes: 3 additions & 0 deletions mark-parser/parser/inline/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ type InlineType int
type Indexes struct {
Indexes []int
Type InlineType
Matches []int
}

const (
Code InlineType = 1
Blob InlineType = 2
Tag InlineType = 3
Link InlineType = 4
)

func InlineParse(text []byte, tokens []token.Token, blockStartIndex int, parsers []Inline) []token.Token {
Expand Down Expand Up @@ -52,6 +54,7 @@ func InlineParse(text []byte, tokens []token.Token, blockStartIndex int, parsers
BlockStartIndex: blockStartIndex,
Text: []int{index + indexes.Indexes[2], index + indexes.Indexes[3]},
Children: []token.Token{},
Matches: indexes.Matches,
})
startIndex = index + indexes.Indexes[1]
index += indexes.Indexes[1]
Expand Down
27 changes: 27 additions & 0 deletions mark-parser/parser/inline/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package inline

import (
"regexp"
)

const (
LinkRegexp = `^\[(.*)\]\((.*)\)`
)

func FindLinkIndex(text []byte) (Indexes, bool) {
re, _ := regexp.Compile(LinkRegexp)

// log.Printf("%s", text)

matches := re.FindSubmatchIndex(text)

if len(matches) == 6 {
return Indexes{
Indexes: matches,
Type: Link,
Matches: matches[2:],
}, true
}

return Indexes{}, false
}
5 changes: 5 additions & 0 deletions mark-parser/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func Parser(text []byte) []token.Token {
Tag: "tag",
Matcher: inline.FindTagIndex,
},
{
Type: inline.Link,
Tag: "link",
Matcher: inline.FindLinkIndex,
},
}

tokens := []token.Token{}
Expand Down
7 changes: 5 additions & 2 deletions mark-parser/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func RenderBlockStart(texts []byte, token token.Token) string {
case int(blocks.Code):
// 获取语言
lang := string(texts[token.BlockStartIndex+token.Matches[0] : token.BlockStartIndex+token.Matches[1]])
return fmt.Sprintf("<pre class=\"%s\"><code>", lang)
return fmt.Sprintf("<div><div class=\"top\"><span class=\"lang\">%s</span><span class=\"icon i-lucide:clipboard\"></span></div><pre class=\"lang-%s\"><code>", lang, lang)
case int(blocks.Image):
// 提取宽度
style := ""
Expand Down Expand Up @@ -96,7 +96,7 @@ func RenderBlockEnd(texts []byte, token token.Token) string {
case int(blocks.TodoList):
return "</li>"
case int(blocks.Code):
return "</code></pre>"
return "</code></pre></div>"
case int(blocks.Image):
return "</div>"
}
Expand All @@ -112,6 +112,9 @@ func RenderInline(texts []byte, token token.Token) string {
return fmt.Sprintf("<span class=\"inline-code\">%s</span>", text)
case int(inline.Tag):
return fmt.Sprintf("<span class=\"tag\">%s</span>", text)
case int(inline.Link):
link := string(texts[token.BlockStartIndex+token.Matches[2] : token.BlockStartIndex+token.Matches[3]])
return fmt.Sprintf("<a class=\"outlink\" target=\"_blank\" href=\"%s\">%s</a>", link, text)
}

return text
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@iconify-json/lucide": "^1.2.25",
"@lucide/lab": "^0.1.2",
"@unocss/reset": "^65.4.0",
"@vueuse/core": "^12.5.0",
Expand Down
Loading

0 comments on commit 352b25a

Please sign in to comment.