40 lines
764 B
Go
40 lines
764 B
Go
package httpserver
|
|
|
|
import (
|
|
"github.com/go-chi/chi"
|
|
"github.com/go-chi/chi/middleware"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func URLRouter() (http.Handler, error) {
|
|
templateInit()
|
|
r := chi.NewRouter()
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
r.NotFound(notFound)
|
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
tmp.ExecuteTemplate(w, "home.html", nil)
|
|
})
|
|
|
|
staticFileServer(r)
|
|
|
|
return r, nil
|
|
}
|
|
|
|
func staticFileServer(rr chi.Router) {
|
|
workDir, _ := os.Getwd()
|
|
staticDir := filepath.Join(workDir, "web/static")
|
|
|
|
rr.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
|
if fi, err := os.Stat(staticDir + r.RequestURI); os.IsNotExist(err) || fi.IsDir() {
|
|
notFound(w, r)
|
|
} else {
|
|
http.FileServer(http.Dir(staticDir)).ServeHTTP(w, r)
|
|
}
|
|
})
|
|
}
|