package main

import (
	"fmt"
	"net/http"
	"os"
	"text/template"
)

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

	// routes
	mux.HandleFunc("/", handleRoot)
	mux.HandleFunc("/robots.txt", serveRobots)
	mux.HandleFunc("/*", handleRoot)

	mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))

	fmt.Println("listening on 8080")
	http.ListenAndServe(":8080", mux)
}

func handleRoot(w http.ResponseWriter, r *http.Request) {
	templ, err := template.ParseFiles("templates/home.html")
	if err != nil {
		http.Error(w, "template not found", http.StatusInternalServerError)
		return
	}

	templ.Execute(w, nil)
}

func serveRobots(w http.ResponseWriter, r *http.Request) {
	robots, err := os.ReadFile("templates/robots.txt")
	if err != nil {
		fmt.Fprintf(w, "sucks to suck")
		return
	}

	fmt.Fprint(w, string(robots))
}
