website.git

commit 192b08a16ef4af047da8bd2dd57582526257280e

Author: Adam Evyčędo <git@apiote.xyz>

embed static files

 router.go | 51 ++++++++++-----------------------------------------


diff --git a/router.go b/router.go
index 4b65f542f99782964e001c6ff634486f62e590e7..1cda4aef1baedd4926d33efc57b7a2d6dec03623 100644
--- a/router.go
+++ b/router.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"bytes"
+	"embed"
 	"fmt"
 	"html/template"
 	"io"
@@ -9,7 +10,6 @@ 	"io/fs"
 	"io/ioutil"
 	"log"
 	"math/rand"
-	"mime"
 	"net/http"
 	"os"
 	"path/filepath"
@@ -22,6 +22,11 @@ 	"github.com/bytesparadise/libasciidoc"
 	"github.com/bytesparadise/libasciidoc/pkg/configuration"
 	"golang.org/x/text/language"
 )
+
+// TODO go:embed for programs and templates
+
+//go:embed static
+var staticFS embed.FS
 
 type Redirection struct {
 	ID  string
@@ -83,39 +88,6 @@ 	}
 	return uid + "@apiote.xyz"
 }
 
-func static(w http.ResponseWriter, r *http.Request) {
-	acceptLanguage := r.Header.Get("Accept-Language")
-	path := strings.Split(r.URL.Path[1:], "/")
-	var file *os.File
-	var err error
-	if len(path) < 2 || len(path) > 3 {
-		renderStatus(w, 404, acceptLanguage)
-	} else if len(path) == 2 {
-		file, err = os.Open("static/" + path[1])
-		defer file.Close()
-	} else if len(path) == 3 {
-		file, err = os.Open("static/favicon/" + path[2])
-		defer file.Close()
-	}
-	if err != nil && os.IsNotExist(err) {
-		renderStatus(w, 404, acceptLanguage)
-		return
-	} else if err != nil {
-		log.Println(err)
-		fmt.Fprint(w, "Error 500")
-		return
-	}
-	s := strings.Split(file.Name(), ".")
-	ext := "." + s[len(s)-1]
-	mimetype := mime.TypeByExtension(ext)
-	w.Header().Set("Content-Type", mimetype+"; charset=utf-8")
-	_, err = io.Copy(w, file)
-	if err != nil {
-		log.Println(err)
-		fmt.Fprint(w, "Error 500")
-	}
-}
-
 func chooseLanguage(name, acceptLanguage string) string {
 	languagesSet := map[language.Tag]struct{}{}
 	_ = filepath.WalkDir("templates", func(path string, d fs.DirEntry, err error) error {
@@ -292,7 +264,7 @@ 	path := strings.Split(r.URL.Path[1:], "/")
 	if path[1] == "" {
 		renderStatus(w, 404, acceptLanguage)
 	} else {
-		file, err := os.Open("static/" + path[1] + ".asc")
+		file, err := staticFS.Open("static/" + path[1] + ".asc")
 		defer file.Close()
 		if err != nil && os.IsNotExist(err) {
 			renderStatus(w, 404, acceptLanguage)
@@ -580,7 +552,7 @@ 	path := strings.Split(r.URL.Path[1:], "/")
 	if path[3] == "" {
 		renderStatus(w, 404, acceptLanguage)
 	} else {
-		file, err := os.Open("static/" + path[3] + ".pgp")
+		file, err := staticFS.Open("static/" + path[3] + ".pgp")
 		defer file.Close()
 		if err != nil && os.IsNotExist(err) {
 			renderStatus(w, 404, acceptLanguage)
@@ -590,10 +562,7 @@ 			log.Println(err)
 			fmt.Fprint(w, "Error 500")
 			return
 		}
-		s := strings.Split(file.Name(), ".")
-		ext := "." + s[len(s)-1]
-		mimetype := mime.TypeByExtension(ext)
-		w.Header().Set("Content-Type", mimetype)
+		w.Header().Set("Content-Type", "application/pgp-key")
 		w.Header().Set("Access-Control-Allow-Origin", "*")
 		_, err = io.Copy(w, file)
 		if err != nil {
@@ -610,7 +579,7 @@ }
 
 func route() {
 	http.HandleFunc("/", index)
-	http.HandleFunc("/static/", static)
+	http.Handle("/static/", http.FileServer(http.FS(staticFS)))
 
 	http.HandleFunc("/b/", blogEntries)
 	http.HandleFunc("/blog/", blogEntries)