71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"log"
|
|
"html/template"
|
|
)
|
|
|
|
type Index struct {
|
|
Title string
|
|
}
|
|
|
|
var data = &Index {
|
|
Title: "MBot",
|
|
}
|
|
|
|
func fileHandler(file string, path string) {
|
|
fs := http.FileServer(http.Dir("./" + file))
|
|
http.Handle("/" + path + "/", http.StripPrefix("/" + path + "/", fs))
|
|
}
|
|
|
|
func aboutHandler(w http.ResponseWriter, req *http.Request) {
|
|
templates := template.Must(template.ParseFiles(
|
|
"src/index.tmpl",
|
|
"src/pages/about.tmpl",
|
|
"src/components/navbar.tmpl",
|
|
"src/components/footer.tmpl",
|
|
))
|
|
|
|
err := templates.ExecuteTemplate(w, "base", data)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func indexHandler(w http.ResponseWriter, req *http.Request) {
|
|
templates := template.Must(template.ParseFiles(
|
|
"src/index.tmpl",
|
|
"src/pages/home.tmpl",
|
|
"src/components/navbar.tmpl",
|
|
"src/components/footer.tmpl",
|
|
))
|
|
|
|
err := templates.ExecuteTemplate(w, "base", data)
|
|
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
if _, err := os.Stat("src/index.tmpl"); os.IsNotExist(err) {
|
|
log.Fatal("Template file does not exist:\n", err)
|
|
}
|
|
|
|
fileHandler("src/assets", "assets");
|
|
fileHandler("src/styles", "styles");
|
|
fileHandler("src/scripts", "scripts");
|
|
fileHandler("src/fonts", "fonts");
|
|
|
|
port := ":3939"
|
|
|
|
http.HandleFunc("/", indexHandler)
|
|
http.HandleFunc("/about", aboutHandler)
|
|
|
|
log.Println("Server running at http://localhost" + port)
|
|
http.ListenAndServe(port, nil)
|
|
}
|