latihanws/main.go
2025-05-09 17:22:38 +07:00

35 lines
615 B
Go

package main
import (
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
)
func main() {
router := chi.NewRouter()
router.Get("/", HandleIndex)
router.Get("/{name}", HandleName)
// fmt.Println("server running at http://localhost:8080")
slog.Info("server running at http://localhost:8080")
if err := http.ListenAndServe(":8080", router); err != nil {
panic(err)
}
}
func HandleIndex(w http.ResponseWriter, r *http.Request) {
Index().Render(r.Context(), w)
}
func HandleName(w http.ResponseWriter, r *http.Request) {
path := chi.URLParam(r, "name")
TemplateName(path).Render(r.Context(), w)
}