feat: linkedin
This commit is contained in:
parent
186491250e
commit
08557f45db
11 changed files with 53 additions and 24 deletions
|
@ -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)
|
||||
|
|
|
@ -26,6 +26,6 @@ func generateTmpl() {
|
|||
"name": names[i],
|
||||
"names": names,
|
||||
})
|
||||
TMPL["/" + names[i]] = b.Bytes()
|
||||
TMPL[names[i]] = b.Bytes()
|
||||
}
|
||||
}
|
||||
|
|
13
src/views.go
13
src/views.go
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue