Skip to content

TencentEdgeOne/go-chi-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

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

Repository files navigation

Go Cloud Functions on EdgeOne Pages - Chi Framework

A full-stack demo application built with Next.js + Tailwind CSS frontend and Go Chi backend, showcasing how to deploy Go Cloud Functions using the Chi router on EdgeOne Pages with RESTful API routing.

πŸš€ Features

  • Chi Router Integration: Lightweight, composable Go HTTP router with middleware stack and route groups
  • Modern UI Design: Dark theme with #1c66e5 accent color, responsive layout with interactive elements
  • Interactive API Testing: Built-in API endpoint panel β€” click "Call" to test each REST endpoint in real-time
  • RESTful API Design: Complete Todo CRUD operations with structured route groups (/api/todos)
  • TypeScript Support: Complete type definitions and type safety on the frontend

πŸ› οΈ Tech Stack

Frontend

  • Next.js 15 - React full-stack framework (with Turbopack)
  • React 19 - User interface library
  • TypeScript - Type-safe JavaScript
  • Tailwind CSS 4 - Utility-first CSS framework

UI Components

  • shadcn/ui - High-quality React components
  • Lucide React - Beautiful icon library
  • class-variance-authority - Component style variant management
  • clsx & tailwind-merge - CSS class name merging utilities

Backend

  • Go 1.21 - Cloud Functions runtime
  • Chi v5 - Lightweight, composable Go HTTP router

πŸ“ Project Structure

go-chi/
β”œβ”€β”€ cloud-functions/                # Go Cloud Functions source
β”‚   β”œβ”€β”€ api.go                     # Chi app with all REST API routes
β”‚   β”œβ”€β”€ go.mod                     # Go module definition
β”‚   └── go.sum                     # Go dependency checksums
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ app/                       # Next.js App Router
β”‚   β”‚   β”œβ”€β”€ globals.css           # Global styles (dark theme)
β”‚   β”‚   β”œβ”€β”€ layout.tsx            # Root layout
β”‚   β”‚   └── page.tsx              # Main page (API testing UI)
β”‚   β”œβ”€β”€ components/               # React components
β”‚   β”‚   └── ui/                   # UI base components
β”‚   β”‚       β”œβ”€β”€ button.tsx        # Button component
β”‚   β”‚       └── card.tsx          # Card component
β”‚   └── lib/                      # Utility functions
β”‚       └── utils.ts              # Common utilities (cn helper)
β”œβ”€β”€ public/                        # Static assets
β”‚   β”œβ”€β”€ eo-logo-blue.svg          # EdgeOne logo (blue)
β”‚   └── eo-logo-white.svg         # EdgeOne logo (white)
β”œβ”€β”€ package.json                   # Project configuration
└── README.md                     # Project documentation

πŸš€ Quick Start

Requirements

  • Node.js 18+
  • pnpm (recommended) or npm
  • Go 1.21+ (for local development)

Install Dependencies

pnpm install
# or
npm install

Development Mode

edgeone pages dev

Visit http://localhost:8088 to view the application.

Build Production Version

edgeone pages build

🎯 Core Features

1. Chi REST API Routes

All API endpoints are defined in a single cloud-functions/api.go file using Chi's route groups:

Method Route Description
GET / Welcome message with route list
GET /health Health check
GET /api/todos List all todos
POST /api/todos Create a new todo
GET /api/todos/{id} Get todo by ID
PATCH /api/todos/{id}/toggle Toggle todo completion
DELETE /api/todos/{id} Delete a todo

2. Interactive API Testing Panel

  • 7 pre-configured API endpoint cards with "Call" buttons
  • Real-time JSON response display with syntax highlighting
  • POST request support with pre-filled JSON body
  • Loading states and error handling

3. Chi Framework Convention

The Go backend uses Chi's standard patterns β€” composable routing, middleware, and subrouters:

package main

import (
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
    "net/http"
)

func main() {
    r := chi.NewRouter()

    r.Use(middleware.Recoverer)
    r.Use(middleware.Logger)

    r.Route("/api/todos", func(r chi.Router) {
        r.Get("/", listTodos)
        r.Post("/", createTodo)
        r.Get("/{id}", getTodo)
        r.Patch("/{id}/toggle", toggleTodo)
        r.Delete("/{id}", deleteTodo)
    })

    http.ListenAndServe(":9000", r)
}

4. Data Model

type Todo struct {
    ID        int       `json:"id"`
    Title     string    `json:"title"`
    Completed bool      `json:"completed"`
    CreatedAt time.Time `json:"createdAt"`
}

πŸ”§ Configuration

Tailwind CSS Configuration

The project uses Tailwind CSS 4 with custom color variables:

:root {
  --primary: #1c66e5;        /* Primary color */
  --background: #000000;     /* Background color */
  --foreground: #ffffff;     /* Foreground color */
}

Component Styling

Uses class-variance-authority to manage component style variants with multiple preset styles.

πŸ“š Documentation

πŸš€ Deployment Guide

EdgeOne Pages Deployment

  1. Push code to GitHub repository
  2. Create new project in EdgeOne Pages console
  3. Select GitHub repository as source
  4. Configure build command: edgeone pages build
  5. Deploy project

Go Chi Cloud Function

Create cloud-functions/api.go in project root with your Chi application:

package main

import (
    "github.com/go-chi/chi/v5"
    "net/http"
    "encoding/json"
)

func main() {
    r := chi.NewRouter()

    r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(map[string]string{
            "message": "Hello from Go Chi on EdgeOne Pages!",
        })
    })

    http.ListenAndServe(":9000", r)
}

Deploy

Deploy with EdgeOne Pages

πŸ“„ License

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors