Skip to content

Commit

Permalink
Switch to embedding all files
Browse files Browse the repository at this point in the history
  • Loading branch information
LordRalex committed Apr 6, 2023
1 parent 58e0f5c commit 884ad3e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
4 changes: 0 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ FROM alpine
WORKDIR /cfwidget

COPY --from=builder /go/bin/cfwidget /go/bin/cfwidget
COPY --from=builder /cfwidget/favicon.ico /cfwidget/favicon.ico
COPY --from=builder /cfwidget/css /cfwidget/css
COPY --from=builder /cfwidget/js /cfwidget/js
COPY --from=builder /cfwidget/templates /cfwidget/templates

EXPOSE 8080

Expand Down
46 changes: 22 additions & 24 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"embed"
"fmt"
"github.com/cfwidget/cfwidget/env"
"github.com/cfwidget/cfwidget/widget"
Expand All @@ -14,7 +15,6 @@ import (
"html/template"
"log"
"net/http"
"os"
"strings"
"time"
)
Expand All @@ -24,20 +24,27 @@ type ApiWebResponse struct {
Accepted bool `json:"accepted,omitempty"`
}

var AllowedFiles = []string{"favicon.ico", "css/app.css"}

const AuthorPath = "author/"

var templateEngine *template.Template

var messagePrinter = message.NewPrinter(language.English)

//go:embed favicon.ico
var faviconFile []byte

//go:embed css/app.css
var cssFile []byte

//go:embed templates/*
var templates embed.FS

func RegisterApiRoutes(e *gin.Engine) {
templates, err := template.New("").ParseGlob("templates/*.tmpl")
var err error
templateEngine, err = template.New("").ParseFS(templates, "templates/*.tmpl")
if err != nil {
panic(err)
}
templateEngine = templates

e.SetHTMLTemplate(templateEngine)

e.GET("/*projectPath", setTransaction, readFromCache, Resolve, GetAuthor, GetProject)
Expand Down Expand Up @@ -67,24 +74,15 @@ func Resolve(c *gin.Context) {
}
}

for _, v := range AllowedFiles {
if v == path {
var contentType string
if strings.HasSuffix(v, ".css") {
contentType = "text/css"
} else if strings.HasSuffix(v, ".ico") {
contentType = "image/x-icon"
}

file, _ := os.ReadFile(v)
SetInCache(c.Request.Host, c.Request.URL.RequestURI(), http.StatusOK, contentType, file)
c.Data(http.StatusOK, contentType, file)
c.Abort()
return
}
}

if path == "service-worker.js" || path == "service-worker-dev.js" || path == "robots.txt" {
if path == "favicon.ico" {
c.Data(http.StatusOK, "image/x-icon", faviconFile)
c.Abort()
return
} else if path == "css/app.css" {
c.Data(http.StatusOK, "text/css", cssFile)
c.Abort()
return
} else if path == "service-worker.js" || path == "service-worker-dev.js" || path == "robots.txt" {
SetInCache(c.Request.URL.Host, c.Request.URL.RequestURI(), http.StatusNotFound, "", nil)
c.AbortWithStatus(http.StatusNotFound)
return
Expand Down

0 comments on commit 884ad3e

Please sign in to comment.