app.go 662 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "net/http"
  6. )
  7. const (
  8. addr = ":8080"
  9. )
  10. /*
  11. @app.route("/")
  12. def index():
  13. */
  14. func RouteIndex(w http.ResponseWriter, r *http.Request) {
  15. http.Redirect(w, r, "/contacts", http.StatusSeeOther)
  16. }
  17. func setupRoutes(mux *http.ServeMux) {
  18. mux.HandleFunc("/", RouteIndex)
  19. mux.Handle("/contacts", http.NotFoundHandler())
  20. }
  21. func main() {
  22. mux := http.NewServeMux()
  23. setupRoutes(mux)
  24. log.Printf("Listening on http://localhost%s", addr)
  25. if err := http.ListenAndServe(addr, mux); err != nil {
  26. if !errors.Is(err, http.ErrServerClosed) {
  27. log.Printf("Server error: %v\n", err)
  28. return
  29. }
  30. }
  31. log.Println("Server shutdown")
  32. }