feat: initial Gin-Go web app scaffold
This commit is contained in:
19
README.md
Normal file
19
README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Gin + Go Web App
|
||||
|
||||
This is a minimal, functional Go application using the Gin web framework.
|
||||
Features:
|
||||
- Basic REST endpoints
|
||||
- HTML page rendering with templates
|
||||
- In-memory data store for demonstration
|
||||
|
||||
How to run
|
||||
- Install Go (1.20+)
|
||||
- go mod download
|
||||
- go run main.go
|
||||
- Open http://localhost:8080
|
||||
|
||||
Structure
|
||||
- main.go: application entry and routes
|
||||
- templates/index.html: HTML template for root page
|
||||
- static/style.css: simple CSS
|
||||
- go.mod: module declaration
|
||||
65
main.go
Normal file
65
main.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
var (
|
||||
users = []User{
|
||||
{ID: 1, Name: "Alice", Email: "alice@example.com"},
|
||||
{ID: 2, Name: "Bob", Email: "bob@example.com"},
|
||||
}
|
||||
nextID = 3
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
|
||||
// HTML 模板
|
||||
r.LoadHTMLGlob("templates/*.html")
|
||||
// 静态资源
|
||||
r.Static("/static", "./static")
|
||||
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||
"title": "Gin Web App",
|
||||
"message": "Welcome to Gin with Go",
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/users", func(c *gin.Context) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
c.JSON(http.StatusOK, users)
|
||||
})
|
||||
|
||||
r.POST("/users", func(c *gin.Context) {
|
||||
var newUser User
|
||||
if err := c.ShouldBindJSON(&newUser); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
newUser.ID = nextID
|
||||
nextID++
|
||||
users = append(users, newUser)
|
||||
mu.Unlock()
|
||||
c.JSON(http.StatusCreated, newUser)
|
||||
})
|
||||
|
||||
r.Run(":8080")
|
||||
}
|
||||
3
static/style.css
Normal file
3
static/style.css
Normal file
@@ -0,0 +1,3 @@
|
||||
body { font-family: Arial, sans-serif; padding: 2rem; background: #f7f9fc; color: #333; }
|
||||
h1 { color: #2c3e50; }
|
||||
#app { max-width: 800px; margin: 0 auto; }
|
||||
56
templates/index.html
Normal file
56
templates/index.html
Normal file
@@ -0,0 +1,56 @@
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>{{ .title }}</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ .title }}</h1>
|
||||
<p>{{ .message }}</p>
|
||||
|
||||
<div id="app">
|
||||
<h2>Users</h2>
|
||||
<ul id="user-list"></ul>
|
||||
<form id="user-form" autocomplete="off">
|
||||
<input type="text" id="name" placeholder="Name" required>
|
||||
<input type="email" id="email" placeholder="Email" required>
|
||||
<button type="submit">Add User</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
async function fetchUsers(){
|
||||
const res = await fetch('/users');
|
||||
const data = await res.json();
|
||||
const ul = document.getElementById('user-list');
|
||||
ul.innerHTML = '';
|
||||
data.forEach(u => {
|
||||
const li = document.createElement('li');
|
||||
li.textContent = u.id + ' - ' + u.name + ' (' + u.email + ')';
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
fetchUsers();
|
||||
const form = document.getElementById('user-form');
|
||||
form.onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const name = document.getElementById('name').value;
|
||||
const email = document.getElementById('email').value;
|
||||
const res = await fetch('/users', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type':'application/json'},
|
||||
body: JSON.stringify({name, email})
|
||||
});
|
||||
if(res.ok){
|
||||
form.reset();
|
||||
fetchUsers();
|
||||
} else {
|
||||
alert('Error creating user');
|
||||
}
|
||||
};
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user