push first writes
This commit is contained in:
parent
7095d782e1
commit
46e5b86321
2
.gitignore
vendored
2
.gitignore
vendored
@ -13,3 +13,5 @@
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
.idea/
|
||||
|
||||
12
cmd/blog/main.go
Normal file
12
cmd/blog/main.go
Normal 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
5
go.mod
Normal 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
2
go.sum
Normal 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=
|
||||
39
internal/httpserver/router.go
Normal file
39
internal/httpserver/router.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
17
internal/httpserver/view.go
Normal file
17
internal/httpserver/view.go
Normal 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
3
web/static/css/main.css
Normal file
@ -0,0 +1,3 @@
|
||||
body {
|
||||
background: rgb(50,50,50);
|
||||
}
|
||||
14
web/templates/404.html
Normal file
14
web/templates/404.html
Normal 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>
|
||||
1
web/templates/footer.html
Normal file
1
web/templates/footer.html
Normal file
@ -0,0 +1 @@
|
||||
footer
|
||||
1
web/templates/header.html
Normal file
1
web/templates/header.html
Normal file
@ -0,0 +1 @@
|
||||
header
|
||||
13
web/templates/home.html
Normal file
13
web/templates/home.html
Normal 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>
|
||||
1
web/templates/static.html
Normal file
1
web/templates/static.html
Normal file
@ -0,0 +1 @@
|
||||
<link href="/css/main.css" rel="stylesheet">
|
||||
Loading…
x
Reference in New Issue
Block a user