refactoring everything teehee (i'm so glad this isn't a team project)
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
c684f0c7ae
commit
10f5f51e76
17 changed files with 970 additions and 547 deletions
|
@ -3,6 +3,9 @@ package global
|
|||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"html/template"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
@ -19,20 +22,11 @@ var MimeTypes = map[string]string{
|
|||
"js": "application/javascript",
|
||||
}
|
||||
|
||||
var LAST_MODIFIED = time.Now()
|
||||
|
||||
func IsModified(req *http.Request, last_modified time.Time) bool {
|
||||
if len(req.Header["If-Modified-Since"]) == 0 || len(req.Header["If-Modified-Since"][0]) == 0 {
|
||||
return true
|
||||
}
|
||||
request_time, err := time.Parse(http.TimeFormat, req.Header["If-Modified-Since"][0])
|
||||
if err != nil {
|
||||
return true
|
||||
}
|
||||
if request_time.Before(last_modified) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
func DefaultHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Add("Server", "arimelody.me")
|
||||
w.Header().Add("Cache-Control", "max-age=2592000")
|
||||
})
|
||||
}
|
||||
|
||||
type LoggingResponseWriter struct {
|
||||
|
@ -69,3 +63,40 @@ func HTTPLog(next http.Handler) http.Handler {
|
|||
r.Header["User-Agent"][0])
|
||||
})
|
||||
}
|
||||
|
||||
func ServeTemplate(page string, data any) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
lp_layout := filepath.Join("views", "layout.html")
|
||||
lp_header := filepath.Join("views", "header.html")
|
||||
lp_footer := filepath.Join("views", "footer.html")
|
||||
lp_prideflag := filepath.Join("views", "prideflag.html")
|
||||
fp := filepath.Join("views", filepath.Clean(page))
|
||||
|
||||
info, err := os.Stat(fp)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
template, err := template.ParseFiles(lp_layout, lp_header, lp_footer, lp_prideflag, fp)
|
||||
if err != nil {
|
||||
fmt.Printf("Error parsing template files: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = template.ExecuteTemplate(w, "layout.html", data)
|
||||
if err != nil {
|
||||
fmt.Printf("Error executing template: %s\n", err)
|
||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue