impressive17
@impressive17

Почему не работает веб-сервер на golang?

Написал веб-сервер, который должен возвращать структуру. Но сервер выдает 404. Как я понимаю, helloHandler даже не запускается.
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"time"
)

func main(){
	handler:=http.NewServeMux()

	// C R U D
	handler.HandleFunc("/hello", helloHandler)

	s:= http.Server{
		Addr: ":8080",
		Handler: nil,
		ReadTimeout: 10 * time.Second,
		WriteTimeout: 10 * time.Second,
		IdleTimeout: 10 * time.Second,
		MaxHeaderBytes: 1 << 20,        //1*2^20 - 128kBytes
	}
	log.Fatal(s.ListenAndServe())
}

type Resp struct{
	Message string
	Error string
}



func helloHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	fmt.Print("WORK")
	resp := Resp {
		Message: "hello",
	}

	respJson, _ := json.Marshal(resp)

	w.WriteHeader(http.StatusOK)

	w.Write(respJson)

}

5dd84b85948ed602696706.png
  • Вопрос задан
  • 400 просмотров
Решения вопроса 1
uvelichitel
@uvelichitel Куратор тега Go
habrahabr.ru/users/uvelichitel
Handler для сервера не указали.
s:= http.Server{
    Addr: ":8080",
    Handler: handler, //здесь
    ReadTimeout: 10 * time.Second,
    WriteTimeout: 10 * time.Second,
    IdleTimeout: 10 * time.Second,
    MaxHeaderBytes: 1 << 20,        //1*2^20 - 128kBytes
  }
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы