Skip to content

ayz6uem/metanode-base1-blog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Base1 Blog

一个基于 Go 语言开发的博客系统后端,使用 Gin 框架和 SQLite 数据库。

项目特性

  • Go 语言开发:使用 Go Modules 进行依赖管理,构建高性能的 Web 服务
  • RESTful API:提供完整的博客 CRUD 操作接口
  • JWT 认证:基于 Token 的用户认证和授权机制
  • 数据库支持:使用 GORM + SQLite,支持数据迁移和模型关联
  • 中间件系统:日志记录、错误处理、认证拦截等中间件
  • 上下文管理:通过 Context 传递用户 ID 等请求信息

技术栈

  • Web 框架:Gin HTTP Framework
  • ORM:GORM
  • 数据库:SQLite
  • 认证:JWT (golang-jwt/jwt/v5)
  • 配置管理:godotenv
  • 日志:logrus

项目结构

base1-blog/
├── cmd/                    # 应用程序入口
│   └── main.go            # 主程序文件
├── config/                # 配置相关
│   └── database.go        # 数据库配置
├── controllers/           # 控制器层
│   ├── auth.go           # 认证控制器
│   ├── post.go           # 文章控制器
│   └── user.go           # 用户控制器
├── middleware/            # 中间件
│   ├── auth.go           # 认证中间件
│   └── logger.go         # 日志中间件
├── models/               # 数据模型
│   ├── comment.go        # 评论模型
│   ├── post.go           # 文章模型
│   ├── user.go           # 用户模型
│   └── models.go         # 模型初始化
├── routes/               # 路由配置
│   └── routes.go         # 路由定义
├── utils/                # 工具函数
│   ├── jwt.go            # JWT 工具
│   ├── response.go       # 响应格式化
│   └── web.go            # Web 工具
├── .env                  # 环境变量配置
├── go.mod                # Go 模块定义
├── go.sum                # 依赖版本锁定
└── rest.http             # HTTP 请求测试文件

快速开始

1. 克隆项目

git clone <repository-url>
cd base1-blog

2. 安装依赖

go mod download

3. 启动服务

go run ./cmd/server

服务将在 http://localhost:8080 启动。

构建部署

编译可执行文件

go build -o bin/metanode ./cmd/server

运行可执行文件

.\bin\metanode.exe  # Windows
./bin/metanode     # Linux/macOS

API 接口

认证接口

用户注册

POST /api/auth/register
Content-Type: application/json

{
  "username": "jackson1",
  "password": "123456",
  "email": "[email protected]"
}

用户登录

POST /api/auth/login
Content-Type: application/json

{
  "username": "jackson1",
  "password": "123456"
}

用户接口

获取用户信息

GET /api/users/info
Authorization: Bearer <token>

文章接口

创建文章

POST /api/posts
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "My first post",
  "content": "This is the content of my first post."
}

获取文章列表(分页)

GET /api/posts?page=1&size=10

更新文章

PUT /api/posts/:id
Authorization: Bearer <token>
Content-Type: application/json

{
  "title": "Updated title",
  "content": "Updated content"
}

删除文章

DELETE /api/posts/:id
Authorization: Bearer <token>

评论文章

POST /api/posts/:id/comments
Authorization: Bearer <token>
Content-Type: application/json

{
  "content": "Great post!"
}

获取文章评论列表

GET /api/posts/:id/comments

环境变量

项目使用 .env 文件进行环境配置:

# JWT 配置
JWT_SECRET=metanode-blog

# 服务器配置
PORT=8080
GIN_MODE=debug

环境变量说明

  • JWT_SECRET: JWT 签名密钥,生产环境建议使用强密钥
  • PORT: 服务器监听端口,默认为 8080
  • GIN_MODE: Gin 运行模式(debug/release/test)

数据库

项目使用 SQLite 作为数据库,数据库文件为 blog.db。首次运行时会自动创建数据库表结构。

测试

可以使用 rest.http 文件进行 API 测试,该文件包含了所有接口的示例请求。推荐使用支持 .http 文件的 IDE(如 JetBrains IDEs)直接发送请求。

开发说明

  • 所有需要认证的接口都需要在请求头中包含 Authorization: Bearer <token>
  • 用户认证后,用户 ID 会通过 Context 在整个请求链中传递
  • 响应格式统一使用 JSON 格式,包含 codemessagedata 字段
  • 使用 GORM 进行数据库操作,支持模型关联和迁移
  • 中间件系统提供了日志记录、认证拦截、错误处理等功能

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages