Name string `json:"name"`
Email string `json:"email"`
Active bool `json:"active"`
CreatedAt time.Time `json:"created_at"`
func NewUserStore() *UserStore {
users: make(map[int]User),
func (s *UserStore) AddUser(name, email string) (User, error) {
func (s *UserStore) GetUser(id int) (User, error) {
return User{}, errors.New("user not found")
func (s *UserStore) GetList() ([]User, error) {
users := make([]User, 0, len(s.users))
for _, user := range s.users {
users = append(users, user)
type UserHandler struct {
func NewUserHandler(store *UserStore) *UserHandler {
func (h *UserHandler) CreateUser(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
body, err := io.ReadAll(r.Body)
http.Error(w, "Invalid request body", http.StatusBadRequest)
Name string `json:"name"`
Email string `json:"email"`
if err := json.Unmarshal(body, &req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
req.Name = strings.TrimSpace(req.Name)
req.Email = strings.TrimSpace(req.Email)
if req.Name == "" || req.Email == "" {
http.Error(w, "Invalid request body", http.StatusBadRequest)
user, err := h.store.AddUser(req.Name, req.Email)
http.Error(w, "Failed to create user", http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(user)
func (h *UserHandler) GetUser(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
path := strings.TrimPrefix(r.URL.Path, "/users/")
id, err := strconv.Atoi(path)
http.Error(w, "Invalid user id", http.StatusBadRequest)
user, err := h.store.GetUser(id)
http.Error(w, "User not found", http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(user)
logLevel string `json:"log_level"`
func LoadConfig(filename string) (*Config, error) {
file, err := os.Open(filename)
return nil, fmt.Errorf("打开Config文件出错: %v", err)
bytes, err := io.ReadAll(file)
return nil, fmt.Errorf("读取Config文件出错: %v", err)
err = json.Unmarshal(bytes, &config)
return nil, fmt.Errorf("解析Config文件出错: %v", err)
func WriteConfig(filename string, config *Config) error {
file, err := os.Create(filename)
return fmt.Errorf("创建Config文件出错: %v", err)
bytes, err := json.MarshalIndent(config, "", " ")
return fmt.Errorf("序列化Config文件出错: %v", err)
_, err = file.Write(bytes)
return fmt.Errorf("写入Config文件出错: %v", err)
flag.StringVar(&configFile, "config", "config.json", "Config file path")
flag.IntVar(&port, "port", 8080, "Server port")
config, err := LoadConfig(configFile)
log.Println("Config:", config)
userHandler := NewUserHandler(NewUserStore())
http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
userHandler.CreateUser(w, r)
userHandler.GetUser(w, r)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
addr := fmt.Sprintf(":%d", config.Port)
log.Fatal(http.ListenAndServe(addr, nil))