Optimize assets

This commit is contained in:
Thomas Miceli 2023-03-23 16:00:48 +01:00
parent 0e0e92b88f
commit 6c981570a1
No known key found for this signature in database
GPG key ID: D86C6F6390AF050F
14 changed files with 6547 additions and 791 deletions

View file

@ -77,8 +77,6 @@ func parseLog(out io.Reader) []*Commit {
}
scanNext = true
fmt.Println("> " + string(scanner.Bytes()))
// new commit found
currentFile = nil
currentCommit = &Commit{Hash: string(scanner.Bytes()[2:]), Files: []File{}}

View file

@ -45,9 +45,18 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
setData(ctx, "httpProtocol", strings.ToUpper(httpProtocol))
if config.C.HTTP.Git {
setData(ctx, "httpCloneUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName+".git")
if config.C.HTTP.Port == "80" || config.C.HTTP.Port == "443" {
setData(ctx, "httpCloneUrl", httpProtocol+"://"+config.C.HTTP.Domain+"/"+userName+"/"+gistName+".git")
} else {
setData(ctx, "httpCloneUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName+".git")
}
}
if config.C.HTTP.Port == "80" || config.C.HTTP.Port == "443" {
setData(ctx, "httpCopyUrl", httpProtocol+"://"+config.C.HTTP.Domain+"/"+userName+"/"+gistName)
} else {
setData(ctx, "httpCopyUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName)
}
setData(ctx, "httpCopyUrl", httpProtocol+"://"+config.C.HTTP.Domain+":"+config.C.HTTP.Port+"/"+userName+"/"+gistName)
setData(ctx, "currentUrl", template.URL(ctx.Request().URL.Path))

View file

@ -3,6 +3,7 @@ package web
import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
"github.com/labstack/echo/v4"
@ -14,6 +15,7 @@ import (
"opengist/internal/config"
"opengist/internal/git"
"opengist/internal/models"
"os"
"path/filepath"
"regexp"
"strconv"
@ -23,6 +25,59 @@ import (
var store *sessions.CookieStore
var re = regexp.MustCompile("[^a-z0-9]+")
var fm = template.FuncMap{
"split": strings.Split,
"indexByte": strings.IndexByte,
"toInt": func(i string) int64 {
val, _ := strconv.ParseInt(i, 10, 64)
return val
},
"inc": func(i int64) int64 {
return i + 1
},
"splitGit": func(i string) []string {
return strings.FieldsFunc(i, func(r rune) bool {
return r == ',' || r == ' '
})
},
"lines": func(i string) []string {
return strings.Split(i, "\n")
},
"isMarkdown": func(i string) bool {
return ".md" == strings.ToLower(filepath.Ext(i))
},
"isCsv": func(i string) bool {
return ".csv" == strings.ToLower(filepath.Ext(i))
},
"csvFile": func(file *git.File) *git.CsvFile {
if ".csv" != strings.ToLower(filepath.Ext(file.Filename)) {
return nil
}
csvFile, err := git.ParseCsv(file)
if err != nil {
return nil
}
return csvFile
},
"httpStatusText": http.StatusText,
"loadedTime": func(startTime time.Time) string {
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
},
"slug": func(s string) string {
return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-")
},
"avatarUrl": func(userHash string) string {
return "https://www.gravatar.com/avatar/" + userHash + "?d=identicon&s=200"
},
"emailToMD5": func(email string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email)))))
},
"asset": func(jsfile string) string {
return "/" + manifestEntries[jsfile].File
},
}
type Template struct {
templates *template.Template
@ -34,6 +89,7 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, _ echo.Con
func Start() {
store = sessions.NewCookieStore([]byte("opengist"))
parseManifestEntries()
e := echo.New()
e.HideBanner = true
@ -57,57 +113,7 @@ func Start() {
e.Use(middleware.Secure())
e.Renderer = &Template{
templates: template.Must(template.New("t").Funcs(
template.FuncMap{
"split": strings.Split,
"indexByte": strings.IndexByte,
"toInt": func(i string) int64 {
val, _ := strconv.ParseInt(i, 10, 64)
return val
},
"inc": func(i int64) int64 {
return i + 1
},
"splitGit": func(i string) []string {
return strings.FieldsFunc(i, func(r rune) bool {
return r == ',' || r == ' '
})
},
"lines": func(i string) []string {
return strings.Split(i, "\n")
},
"isMarkdown": func(i string) bool {
return ".md" == strings.ToLower(filepath.Ext(i))
},
"isCsv": func(i string) bool {
return ".csv" == strings.ToLower(filepath.Ext(i))
},
"csvFile": func(file *git.File) *git.CsvFile {
if ".csv" != strings.ToLower(filepath.Ext(file.Filename)) {
return nil
}
csvFile, err := git.ParseCsv(file)
if err != nil {
return nil
}
return csvFile
},
"httpStatusText": http.StatusText,
"loadedTime": func(startTime time.Time) string {
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
},
"slug": func(s string) string {
return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-")
},
"avatarUrl": func(userHash string) string {
return "https://www.gravatar.com/avatar/" + userHash + "?d=identicon&s=200"
},
"emailToMD5": func(email string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email)))))
},
}).ParseGlob("templates/*/*.html")),
templates: template.Must(template.New("t").Funcs(fm).ParseGlob("templates/*/*.html")),
}
e.HTTPErrorHandler = func(er error, ctx echo.Context) {
@ -291,3 +297,25 @@ func logged(next echo.HandlerFunc) echo.HandlerFunc {
func noRouteFound(echo.Context) error {
return notFound("Page not found")
}
// ---
type Asset struct {
File string `json:"file"`
}
var manifestEntries map[string]Asset
func parseManifestEntries() {
file, err := os.Open("public/manifest.json")
if err != nil {
log.Fatal().Err(err).Msg("Failed to open manifest.json")
}
byteValue, err := io.ReadAll(file)
if err != nil {
log.Fatal().Err(err).Msg("Failed to read manifest.json")
}
if err = json.Unmarshal(byteValue, &manifestEntries); err != nil {
log.Fatal().Err(err).Msg("Failed to unmarshal manifest.json")
}
}

View file

@ -148,7 +148,7 @@ func validateReservedKeywords(fl validator.FieldLevel) bool {
name := fl.Field().String()
restrictedNames := map[string]struct{}{}
for _, restrictedName := range []string{"register", "login", "logout", "config", "admin", "all"} {
for _, restrictedName := range []string{"assets", "register", "login", "logout", "config", "admin", "all"} {
restrictedNames[restrictedName] = struct{}{}
}

7133
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8,18 +8,23 @@
"preview": "node_modules/.bin/vite preview"
},
"devDependencies": {
"@codemirror/commands": "^6.0.0",
"@codemirror/lang-javascript": "^6.0.0",
"@codemirror/view": "^6.0.1",
"@tailwindcss/forms": "^0.5.1",
"@tailwindcss/typography": "^0.5.2",
"autoprefixer": "^10.4.7",
"codemirror": "^6.0.0",
"highlight.js": "^11.5.1",
"@codemirror/commands": "^6.2.2",
"@codemirror/lang-javascript": "^6.1.4",
"@codemirror/view": "^6.9.3",
"@tailwindcss/forms": "^0.5.3",
"@tailwindcss/typography": "^0.5.9",
"autoprefixer": "^10.4.14",
"codemirror": "^6.0.1",
"cssnano": "^5.1.15",
"highlight.js": "^11.7.0",
"markdown-it": "^13.0.1",
"moment": "^2.29.3",
"postcss": "^8.4.13",
"tailwindcss": "^3.0.24",
"vite": "^2.9.2"
"postcss-cssnext": "^3.1.1",
"postcss-import": "^15.1.0",
"postcss-loader": "^7.1.0",
"sugarss": "^4.0.1",
"tailwindcss": "^3.2.7",
"vite": "^4.2.1"
}
}

View file

@ -1,6 +1,7 @@
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
plugins: {
tailwindcss: {},
autoprefixer: {},
cssnano: {},
},
}

1
public/main.js vendored
View file

@ -1,5 +1,6 @@
import './style.css'
import './markdown.css'
import './favicon.svg'
import 'highlight.js/styles/tokyo-night-dark.css'
import moment from 'moment'
import md from 'markdown-it'

4
public/style.css vendored
View file

@ -8,6 +8,10 @@
}
}
html {
@apply bg-gray-800;
}
a {
@apply text-primary-500;
}

View file

@ -3,14 +3,10 @@
<html lang="en" class="h-full">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="http://localhost:3000/favicon.svg" />
<link rel="icon" type="image/svg+xml" href="{{ asset "favicon.svg" }}" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
html {
background-color: #0d1117;
}
</style>
<script type="module" src="http://localhost:3000/main.js"></script>
<link rel="stylesheet" href="{{ asset "main.css" }}" />
<script type="module" src="{{ asset "main.js" }}"></script>
{{ if .htmlTitle }}
<title>{{ .htmlTitle }} - Opengist</title>

View file

@ -146,9 +146,6 @@
<p class="text-xs font-normal text-gray-400">Clone with Git using an SSH key.</p>
</div>
{{ end }}
<div class="text-slate-300 block px-4 py-2 text-sm hover:bg-gray-700 gist-menu-item" role="menuitem" id="gist-menu-embed" data-link="(soon)"><p>Embed</p>
<p class="text-xs font-normal text-gray-400">Embed this gist in your website. (soon)</p>
</div>
<div class="text-slate-300 block px-4 py-2 text-sm hover:bg-gray-700 gist-menu-item" role="menuitem" id="gist-menu-share" data-link="{{ .httpCopyUrl }}"><p>Share</p>
<p class="text-xs font-normal text-gray-400">Copy shareable link for this gist.</p>
</div>

View file

@ -44,6 +44,6 @@
</main>
</div>
<script type="module" src="http://localhost:3000/editor.js"></script>
<script type="module" src="{{ asset "editor.js" }}"></script>
{{ template "footer" .}}

View file

@ -81,7 +81,6 @@
</main>
</div>
<script type="module" src="http://localhost:3000/editor.js"></script>
<script type="module" src="{{ asset "editor.js" }}"></script>
{{ template "footer" .}}

View file

@ -9,7 +9,6 @@ export default defineConfig({
assetsDir: 'assets',
manifest: true,
rollupOptions: {
// overwrite default .html entry
input: ['./public/main.js', './public/editor.js']
}
}