feat: copy button

This commit is contained in:
ange 2024-12-12 14:28:45 +00:00
parent b9d7553420
commit 4b530d332c
Signed by: ange
GPG key ID: 9E0C4157BB7BEB1D
5 changed files with 65 additions and 19 deletions

View file

@ -6,7 +6,7 @@ import (
)
func main() {
http.HandleFunc("/", Route)
http.HandleFunc("/", route)
err := http.ListenAndServe(":3000", nil)
if err != nil {

View file

@ -15,17 +15,17 @@ var routes = []struct {
regex *regexp.Regexp
handler http.HandlerFunc
}{
{[]string{"GET"}, URL(""), Index},
{[]string{"GET"}, URL("/style\\.css"), Style},
{[]string{"GET"}, URL("/static/.+"), Static},
{[]string{"GET"}, URL("/[^/]+"), HTML},
{[]string{"GET"}, url(""), index},
{[]string{"GET"}, url("/style\\.css"), style},
{[]string{"GET"}, url("/static/.+"), static},
{[]string{"GET"}, url("/[^/]+"), html},
}
func URL(s string) *regexp.Regexp {
func url(s string) *regexp.Regexp {
return regexp.MustCompile("^" + s + "/?$")
}
func Route(w http.ResponseWriter, r *http.Request) {
func route(w http.ResponseWriter, r *http.Request) {
for _, rt := range routes {
matches := rt.regex.FindStringSubmatch(r.URL.Path)
if len(matches) > 0 {

View file

@ -5,18 +5,18 @@ import (
"path/filepath"
)
func Index(w http.ResponseWriter, r *http.Request) {
func index(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join("html", "index.html"))
}
func Style(w http.ResponseWriter, r *http.Request) {
func style(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "/html/style.css")
}
func Static(w http.ResponseWriter, r *http.Request) {
func static(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path)
}
func HTML(w http.ResponseWriter, r *http.Request) {
func html(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join("html", r.URL.Path + ".html"))
}