implement tempaltes
This commit is contained in:
@@ -1,48 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type FileData struct {
|
||||
ImageNames []string
|
||||
SpicyImageNames []string
|
||||
AsciiFiles []AsciiEntry
|
||||
}
|
||||
|
||||
type AsciiEntry struct {
|
||||
Name string
|
||||
FontSize int
|
||||
}
|
||||
|
||||
var templ *template.Template
|
||||
|
||||
func main() {
|
||||
fmt.Print("Now running server!")
|
||||
templ, _ = template.ParseGlob("templates/*.html")
|
||||
|
||||
fmt.Print("Now running server!\n")
|
||||
// Serves index
|
||||
http.HandleFunc("/", index_handler)
|
||||
|
||||
// Serves json for html to find file names
|
||||
http.HandleFunc("/files", files_handler)
|
||||
http.HandleFunc("/files", file_handler)
|
||||
|
||||
// Serves ascii page
|
||||
http.HandleFunc("/ascii", ascii_handler)
|
||||
|
||||
// Serves images
|
||||
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("./static"))))
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
|
||||
|
||||
// Serves what ever the user is requesting based on above urls
|
||||
// Serves what ever the user is requesting base on above urls
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
||||
func index_handler(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./static/index.html")
|
||||
templ.ExecuteTemplate(w, "images.html", nil)
|
||||
}
|
||||
|
||||
func files_handler(w http.ResponseWriter, r *http.Request) {
|
||||
files, err := os.ReadDir("./static")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var names []string
|
||||
for _, f := range files {
|
||||
name := f.Name()
|
||||
if strings.HasSuffix(name, ".jpg") ||
|
||||
strings.HasSuffix(name, ".png") ||
|
||||
strings.HasSuffix(name, ".gif") {
|
||||
names = append(names, "/images/"+name)
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(names)
|
||||
func ascii_handler(w http.ResponseWriter, r *http.Request) {
|
||||
templ.ExecuteTemplate(w, "ascii.html", nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user