Skip to content

koriyoshi2041/Assignment2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Blog System MVP

Go Version Node Version React Version TypeScript GORM License

A modern, full-stack blog system built with Go and React, featuring JWT authentication, GORM database persistence, TypeScript safety, auto-generated API clients, and comprehensive APIFox integration.

πŸš€ Features

  • Full Blog System: Complete blogging platform with post creation and listing
  • Database Persistence: GORM integration with SQLite/MySQL/PostgreSQL support
  • Secure Authentication: JWT token-based authentication with bcrypt password hashing
  • Blog Post Management: Create, view, and manage blog posts with user attribution
  • TypeScript Integration: Full TypeScript support with auto-generated API clients
  • Code Generation: OpenAPI Generator for automated frontend and backend models
  • Modern UI: Responsive React interface with Ant Design components
  • Real-time State Management: MobX for reactive state management
  • Protected Routes: Client-side route protection with automatic redirects
  • API Documentation: OpenAPI 3.0+ specification with APIFox integration
  • CORS Support: Cross-origin resource sharing enabled
  • Type Safety: Complete type safety from database to UI components

πŸ› οΈ Tech Stack

Backend

  • Go 1.24+ - High-performance backend service
  • Gin Framework v1.10+ - Fast HTTP web framework
  • GORM v1.30+ - Go ORM with database abstraction
  • SQLite/MySQL/PostgreSQL - Database support with auto-migration
  • JWT - JSON Web Token for authentication
  • bcrypt - Password hashing and verification

Frontend

  • React 19+ - Modern JavaScript UI library
  • TypeScript 5.0+ - Static type checking and enhanced developer experience
  • Ant Design 5+ - Enterprise-class UI components
  • MobX 6+ & MobX-React-Lite - Simple, scalable state management
  • React Router v7+ - Declarative routing
  • Generated API Client - Auto-generated TypeScript client from OpenAPI spec
  • pnpm - Fast, disk space efficient package manager
  • Vite - Fast build tool and development server

API & Code Generation

  • OpenAPI 3.0+ - API specification and documentation
  • OpenAPI Generator - Automated TypeScript client and Go model generation
  • APIFox - API design, testing, and code generation platform

DevOps & Tools

  • Make - Build automation
  • Git - Version control
  • TypeScript Compiler - Type checking and compilation

πŸ“‹ Prerequisites

  • Go 1.24 or higher
  • Node.js 18.0 or higher
  • pnpm package manager
  • OpenAPI Generator CLI (automatically installed)
  • SQLite (default), MySQL 8+, or PostgreSQL 16+ (optional)

πŸš€ Quick Start

1. Clone the repository

git clone https://github.com/koriyoshi2041/Assignment2.git
cd Assignment2

2. Start the backend server

cd backend
go mod tidy
go run .

The backend server will start on http://localhost:8080 with SQLite database

3. Start the frontend development server

cd frontend
pnpm install
pnpm dev

The frontend application will be available at http://localhost:5173

4. Test the application

  • Open your browser to http://localhost:5173
  • Use the test credentials:
    • Admin: admin / password123
    • User: user1 / mypassword
  • Create blog posts, view the blog feed, and test authentication

πŸ“ Project Structure

Assignment2/
β”œβ”€β”€ backend/              # Go backend service
β”‚   β”œβ”€β”€ main.go          # Main application entry point
β”‚   β”œβ”€β”€ models/          # GORM database models
β”‚   β”‚   β”œβ”€β”€ user.go      # User model with associations
β”‚   β”‚   └── post.go      # Post model with relationships
β”‚   β”œβ”€β”€ api_models/      # Auto-generated API models from OpenAPI
β”‚   β”œβ”€β”€ utils.go         # Utility functions
β”‚   β”œβ”€β”€ gen_api.sh       # API model generation script
β”‚   β”œβ”€β”€ go.mod           # Go module dependencies
β”‚   └── go.sum           # Go module checksums
β”œβ”€β”€ frontend/            # React TypeScript frontend application
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/         # Auto-generated TypeScript API client
β”‚   β”‚   β”‚   β”œβ”€β”€ apis/    # API endpoint implementations
β”‚   β”‚   β”‚   β”œβ”€β”€ models/  # TypeScript type definitions
β”‚   β”‚   β”‚   └── runtime.ts
β”‚   β”‚   β”œβ”€β”€ components/  # React TypeScript components
β”‚   β”‚   β”‚   β”œβ”€β”€ LoginPage.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ BlogPage.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ CreatePostForm.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ PostList.tsx
β”‚   β”‚   β”‚   β”œβ”€β”€ Header.tsx
β”‚   β”‚   β”‚   └── ProtectedRoute.tsx
β”‚   β”‚   β”œβ”€β”€ stores/      # MobX stores with TypeScript
β”‚   β”‚   β”‚   β”œβ”€β”€ authStore.ts
β”‚   β”‚   β”‚   β”œβ”€β”€ blogStore.ts
β”‚   β”‚   β”‚   └── index.ts
β”‚   β”‚   β”œβ”€β”€ App.tsx      # Main application component
β”‚   β”‚   └── main.tsx     # Application entry point
β”‚   β”œβ”€β”€ gen_api.sh       # API client generation script
β”‚   β”œβ”€β”€ openapitools.json # OpenAPI Generator configuration
β”‚   β”œβ”€β”€ package.json     # Frontend dependencies
β”‚   β”œβ”€β”€ tsconfig.json    # TypeScript configuration
β”‚   └── vite.config.js   # Vite configuration
β”œβ”€β”€ openapi.yml          # OpenAPI specification
β”œβ”€β”€ Makefile            # Build automation
└── README.md           # Project documentation

πŸ” API Endpoints

Authentication

  • POST /user/login - User authentication
    • Request: {"username": "string", "password": "string"}
    • Response: {"token": "jwt_token"}
  • POST /user/logout - User logout
    • Headers: Authorization: Bearer <token>
    • Response: {"message": "Logged out successfully"}

User Information

  • GET /user/info - Get current user information
    • Headers: Authorization: Bearer <token>
    • Response: {"nick_name": "string"}

Blog Posts

  • POST /posts - Create a new blog post
    • Headers: Authorization: Bearer <token>
    • Request: {"content": "string"}
    • Response: {"id": 1, "content": "string", "author_nickname": "string", "created_at": "2025-01-01T00:00:00Z"}
  • GET /posts - Get all blog posts
    • Response: [{"id": 1, "content": "string", "author_nickname": "string", "created_at": "2025-01-01T00:00:00Z"}]

πŸ”§ Development

Using Makefile

make run-backend      # Start backend server
make run-frontend     # Start frontend development server
make build-backend    # Build backend binary
make build-frontend   # Build frontend for production
make clean           # Clean build artifacts

API Client Generation

The project uses OpenAPI Generator to automatically generate both frontend and backend code:

Frontend TypeScript API Client:

cd frontend
./gen_api.sh          # Generate TypeScript API client from OpenAPI spec

Backend Go API Models:

cd backend
./gen_api.sh          # Generate Go API models from OpenAPI spec

This generates:

  • Frontend: AuthenticationApi, PostsApi, UserInformationApi with TypeScript types
  • Backend: Go structs for requests/responses with proper JSON tags
  • Type Definitions: Complete type safety across the entire stack
  • Runtime: HTTP client with proper error handling

TypeScript Development

cd frontend
pnpm run build        # Build with TypeScript compilation
npx tsc --noEmit      # Type checking only

Database Configuration

The application supports multiple database backends via environment variables:

SQLite (Default):

# No configuration needed - uses ./blog_system.db
go run .

MySQL:

export DB_TYPE=mysql
export DB_HOST=localhost
export DB_PORT=3306
export DB_USER=root
export DB_PASSWORD=password
export DB_NAME=blog_system
go run .

PostgreSQL:

export DB_TYPE=postgres
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=postgres
export DB_PASSWORD=password
export DB_NAME=blog_system
go run .

πŸ§ͺ Testing

Manual Testing

  1. Start both backend and frontend servers
  2. Navigate to http://localhost:5173
  3. Test login functionality with provided credentials
  4. Create and view blog posts
  5. Test logout functionality
  6. Verify protected route access and JWT token handling
  7. Verify database persistence by restarting servers

API Testing with APIFox

  1. Import the openapi.yml file into APIFox
  2. Configure environment variables
  3. Run the provided test cases
  4. Verify all endpoints return expected responses

πŸ“Š APIFox Integration

This project demonstrates proper APIFox workflow:

1. API Design & Documentation

  • Import openapi.yml into APIFox
  • Complete API specification with examples
  • Auto-generated interactive documentation

2. Code Generation

  • Export OpenAPI JSON from APIFox
  • Use OpenAPI Generator CLI to generate TypeScript client
  • Automated type-safe API integration

3. Mock Services & Testing

  • APIFox mock services for parallel development
  • Automated API testing and validation
  • Team collaboration through shared specifications

Workflow

APIFox Design β†’ OpenAPI Export β†’ OpenAPI Generator β†’ TypeScript Client

This replaces manual API coding with automated, type-safe client generation.

🚒 Deployment

Backend Deployment

cd backend
go build -o server .
./server

Frontend Deployment

cd frontend
pnpm run build
# Deploy the dist/ folder to your static hosting service

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Gin Framework for the excellent Go web framework
  • Ant Design for the beautiful React UI components
  • APIFox for comprehensive API development tools

πŸ“ž Contact

For questions or support, please open an issue in the GitHub repository.


Note: This is an educational project demonstrating modern full-stack web development practices with Go, React, TypeScript, GORM, and proper APIFox workflow with automated code generation for both frontend and backend.

About

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •