Browse Source

Start rework mounted assets

Vova Tkach 6 years ago
parent
commit
a92b1f6060

+ 12 - 1
Gopkg.lock

@@ -9,9 +9,20 @@
   revision = "1afc55739623b0d65c665abaa71624b4b48fa0df"
   version = "v1.0.0"
 
+[[projects]]
+  digest = "1:3d823a99a747dbcdfb547754418ca792a57dfef66c2d1f540bffbf12c07bd7bf"
+  name = "github.com/vladimirok5959/golang-server-resources"
+  packages = ["resource"]
+  pruneopts = "UT"
+  revision = "4fce983c6e02d13313582a3c9f089c06c7979e8f"
+  version = "v1.0.0"
+
 [solve-meta]
   analyzer-name = "dep"
   analyzer-version = 1
-  input-imports = ["github.com/vladimirok5959/golang-server-bootstrap/bootstrap"]
+  input-imports = [
+    "github.com/vladimirok5959/golang-server-bootstrap/bootstrap",
+    "github.com/vladimirok5959/golang-server-resources/resource",
+  ]
   solver-name = "gps-cdcl"
   solver-version = 1

+ 1 - 0
assets/assets.go

@@ -0,0 +1 @@
+package assets

+ 112 - 0
assets/sys.styles.css

@@ -0,0 +1,112 @@
+html {
+	min-height: 100%;
+	height: 100%;
+	position: relative
+}
+
+body {
+	background: #205081 url(/assets/sys/bg.png) repeat 0 0;
+	margin: 0;
+	padding: 0;
+	color: #FFF;
+	font-family: Arial, sans-serif;
+	font-weight: 300;
+	font-size: 18px;
+	line-height: 21px;
+	position: relative;
+	width: 100%;
+	min-height: 100%;
+	height: 100%;
+	height: auto;
+	display: table
+}
+
+.wrapper {
+	padding: 15px;
+	text-align: center;
+	display: table-cell;
+	vertical-align: middle
+}
+
+.wrapper .logo {
+	width: 332px;
+	height: 203px;
+	margin: 0 auto;
+	background: url(/assets/sys/logo.png) no-repeat 0 0
+}
+
+.wrapper .logo .svg {
+	width: 150px;
+	height: 150px;
+	margin: 0 auto;
+	position: relative;
+	padding-top: 40px
+}
+
+.wrapper .logo .svg img {
+	top: 50%;
+	left: 50%;
+	position: absolute;
+	width: 150px;
+	height: 150px;
+	margin-top: -75px;
+	margin-left: -75px;
+	animation-name: fave;
+	animation-duration: 1000ms;
+	animation-iteration-count: infinite;
+	animation-timing-function: linear
+}
+
+@keyframes fave {
+	0% {
+		width: 150px;
+		height: 150px;
+		margin-top: -75px;
+		margin-left: -75px
+	}
+
+	40% {
+		width: 150px;
+		height: 150px;
+		margin-top: -75px;
+		margin-left: -75px
+	}
+
+	60% {
+		width: 120px;
+		height: 120px;
+		margin-top: -60px;
+		margin-left: -60px
+	}
+
+	100% {
+		width: 150px;
+		height: 150px;
+		margin-top: -75px;
+		margin-left: -75px
+	}
+}
+
+h1, h2 {
+	font-weight: 400;
+	font-size: 34px;
+	line-height: 36px;
+	margin: 10px 0
+}
+
+h2 {
+	font-weight: 350;
+	font-size: 14px;
+	line-height: 18px;
+	margin-bottom: 0
+}
+
+@media only screen and (max-width:800px) {
+	.wrapper {
+		padding: 15px 0
+	}
+
+	h1, h2 {
+		padding: 0 15px
+	}
+}

+ 3 - 0
assets/sys.styles.css.go

@@ -0,0 +1,3 @@
+package assets
+
+var SysStylesCss = []byte(`html{min-height:100%;height:100%;position:relative}body{background:#205081 url(/assets/sys/bg.png) repeat 0 0;margin:0;padding:0;color:#FFF;font-family:Arial,sans-serif;font-weight:300;font-size:18px;line-height:21px;position:relative;width:100%;min-height:100%;height:100%;height:auto;display:table}.wrapper{padding:15px;text-align:center;display:table-cell;vertical-align:middle}.wrapper .logo{width:332px;height:203px;margin:0 auto;background:url(/assets/sys/logo.png) no-repeat 0 0}.wrapper .logo .svg{width:150px;height:150px;margin:0 auto;position:relative;padding-top:40px}.wrapper .logo .svg img{top:50%;left:50%;position:absolute;width:150px;height:150px;margin-top:-75px;margin-left:-75px;animation-name:fave;animation-duration:1000ms;animation-iteration-count:infinite;animation-timing-function:linear}@keyframes fave{0%{width:150px;height:150px;margin-top:-75px;margin-left:-75px}40%{width:150px;height:150px;margin-top:-75px;margin-left:-75px}60%{width:120px;height:120px;margin-top:-60px;margin-left:-60px}100%{width:150px;height:150px;margin-top:-75px;margin-left:-75px}}h1,h2{font-weight:400;font-size:34px;line-height:36px;margin:10px 0}h2{font-weight:350;font-size:14px;line-height:18px;margin-bottom:0}@media only screen and (max-width:800px){.wrapper{padding:15px 0}h1,h2{padding:0 15px}}`)

+ 12 - 0
main.go

@@ -5,10 +5,12 @@ import (
 	"fmt"
 	"net/http"
 
+	"golang-fave/assets"
 	"golang-fave/consts"
 	"golang-fave/utils"
 
 	"github.com/vladimirok5959/golang-server-bootstrap/bootstrap"
+	"github.com/vladimirok5959/golang-server-resources/resource"
 	/*
 		"context"
 		"errors"
@@ -48,10 +50,20 @@ func main() {
 		return
 	}
 
+	res := resource.New()
+	res.Add("assets/sys/styles.css", "text/css", assets.SysStylesCss)
+
 	bootstrap.Start("127.0.0.1:8080", 30, "assets", func(w http.ResponseWriter, r *http.Request) {
 		w.Header().Set("Server", "fave.pro/"+consts.ServerVersion)
 	}, func(w http.ResponseWriter, r *http.Request) {
 
+		// Mounted assets
+		if res.Response(w, r, func(w http.ResponseWriter, r *http.Request, i *resource.Resource) {
+			w.Header().Set("Cache-Control", "public, max-age=31536000")
+		}, nil) {
+			return
+		}
+
 		/*
 			// After callback
 			w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")

+ 21 - 0
vendor/github.com/vladimirok5959/golang-server-resources/LICENSE

@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2019 Vova Tkach
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.

+ 66 - 0
vendor/github.com/vladimirok5959/golang-server-resources/resource/resource.go

@@ -0,0 +1,66 @@
+package resource
+
+import (
+	"net/http"
+)
+
+type Resource struct {
+	Path  string
+	Ctype string
+	Bytes []byte
+}
+
+type resource struct {
+	maxurl int
+	list   map[string]Resource
+}
+
+func New() *resource {
+	r := resource{maxurl: 0}
+	r.list = map[string]Resource{}
+	return &r
+}
+
+func (this *resource) Add(path string, ctype string, bytes []byte) {
+	// Do not add if already in resources list
+	if _, ok := this.list[path]; ok == true {
+		return
+	}
+
+	// Add to resources list
+	this.maxurl = len(path)
+	this.list[path] = Resource{
+		Path:  path,
+		Ctype: ctype,
+		Bytes: bytes,
+	}
+}
+
+func (this *resource) Response(w http.ResponseWriter, r *http.Request, before func(w http.ResponseWriter, r *http.Request, i *Resource), after func(w http.ResponseWriter, r *http.Request, i *Resource)) bool {
+	// Do not process if this is not necessary
+	if len(r.URL.Path) <= 1 || len(r.URL.Path)-1 > this.maxurl {
+		return false
+	}
+
+	// Check for resource
+	res, ok := this.list[r.URL.Path[1:]]
+	if ok == false {
+		return false
+	}
+
+	// Call `before` callback
+	if before != nil {
+		before(w, r, &res)
+	}
+
+	// Send resource
+	w.Header().Set("Content-Type", res.Ctype)
+	w.Write(res.Bytes)
+
+	// Call `after` callback
+	if after != nil {
+		after(w, r, &res)
+	}
+
+	return true
+}