push first writes

This commit is contained in:
Dober 2020-04-21 19:03:45 +03:00
parent 7095d782e1
commit 46e5b86321
No known key found for this signature in database
GPG Key ID: CDA4F9D586140A3B
12 changed files with 110 additions and 0 deletions

2
.gitignore vendored
View File

@ -13,3 +13,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.idea/

12
cmd/blog/main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"mxblog/internal/httpserver"
"net/http"
)
func main() {
h, _ := httpserver.URLRouter()
http.ListenAndServe(":8080", h)
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module mxblog
go 1.13
require github.com/go-chi/chi v4.1.1+incompatible

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/go-chi/chi v4.1.1+incompatible h1:MmTgB0R8Bt/jccxp+t6S/1VGIKdJw5J74CK/c9tTfA4=
github.com/go-chi/chi v4.1.1+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=

View File

@ -0,0 +1,39 @@
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)
}
})
}

View File

@ -0,0 +1,17 @@
package httpserver
import (
"html/template"
"net/http"
)
var tmp *template.Template
func templateInit() {
tmp, _ = template.ParseGlob("web/templates/*")
}
func notFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
tmp.ExecuteTemplate(w, "404.html", nil)
}

3
web/static/css/main.css Normal file
View File

@ -0,0 +1,3 @@
body {
background: rgb(50,50,50);
}

14
web/templates/404.html Normal file
View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MXBlog - Not Found!</title>
{{template "static.html"}}
</head>
<body>
{{template "header.html"}}
<span>Not Found!!!</span>
<a href="/">To home</a>
{{template "footer.html"}}
</body>
</html>

View File

@ -0,0 +1 @@
footer

View File

@ -0,0 +1 @@
header

13
web/templates/home.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MXBlog - Home</title>
{{template "static.html"}}
</head>
<body>
{{template "header.html"}}
welcome to home
{{template "footer.html"}}
</body>
</html>

View File

@ -0,0 +1 @@
<link href="/css/main.css" rel="stylesheet">