feat: linkedin

This commit is contained in:
ange 2024-12-21 11:13:23 +00:00
parent 186491250e
commit 08557f45db
Signed by: ange
GPG key ID: 9E0C4157BB7BEB1D
11 changed files with 53 additions and 24 deletions

View file

@ -16,15 +16,19 @@ var routes = []struct {
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("/(.+\\.css)"), css},
{[]string{"GET"}, url("/([^/]+)"), html},
}
func url(s string) *regexp.Regexp {
return regexp.MustCompile("^" + s + "/?$")
}
func getParam(r *http.Request, i int) string {
return r.Context().Value(URLParam{}).([]string)[i]
}
func route(w http.ResponseWriter, r *http.Request) {
for _, rt := range routes {
matches := rt.regex.FindStringSubmatch(r.URL.Path)

View file

@ -26,6 +26,6 @@ func generateTmpl() {
"name": names[i],
"names": names,
})
TMPL["/" + names[i]] = b.Bytes()
TMPL[names[i]] = b.Bytes()
}
}

View file

@ -1,23 +1,26 @@
package main
import (
"path/filepath"
"net/http"
"fmt"
)
func index(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/contact", http.StatusMovedPermanently)
}
func style(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "html/style.css")
}
func static(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path)
}
func css(w http.ResponseWriter, r *http.Request) {
fmt.Println(filepath.Join("css", getParam(r, 0)))
http.ServeFile(w, r, filepath.Join("css", getParam(r, 0)))
}
func html(w http.ResponseWriter, r *http.Request) {
if t, found := TMPL[r.URL.Path]; found {
if t, found := TMPL[getParam(r, 0)]; found {
w.Write(t)
} else {
http.NotFound(w, r)