mirror of
https://github.com/httpjamesm/AnonymousOverflow.git
synced 2025-04-19 21:39:24 +00:00
feat: embed static files
This commit is contained in:
parent
0a6af8d412
commit
0f576945b2
3 changed files with 15 additions and 19 deletions
|
@ -20,12 +20,10 @@ FROM scratch
|
||||||
|
|
||||||
COPY --from=build /app/anonymousoverflow /anonymousoverflow
|
COPY --from=build /app/anonymousoverflow /anonymousoverflow
|
||||||
COPY --from=build /app/healthcheck /healthcheck
|
COPY --from=build /app/healthcheck /healthcheck
|
||||||
COPY templates /templates
|
|
||||||
COPY public /public
|
|
||||||
COPY --from=build /etc/ssl/certs /etc/ssl/certs
|
COPY --from=build /etc/ssl/certs /etc/ssl/certs
|
||||||
|
|
||||||
HEALTHCHECK --interval=60s --timeout=5s --start-period=2s --retries=3 CMD [ "/healthcheck","http://localhost:8080/healthz" ]
|
HEALTHCHECK --interval=60s --timeout=5s --start-period=2s --retries=3 CMD [ "/healthcheck","http://localhost:8080/healthz" ]
|
||||||
|
|
||||||
EXPOSE 8080
|
EXPOSE 8080
|
||||||
|
|
||||||
CMD ["/anonymousoverflow"]
|
CMD ["/anonymousoverflow"]
|
||||||
|
|
16
main.go
16
main.go
|
@ -4,7 +4,11 @@ import (
|
||||||
"anonymousoverflow/env"
|
"anonymousoverflow/env"
|
||||||
"anonymousoverflow/src/middleware"
|
"anonymousoverflow/src/middleware"
|
||||||
"anonymousoverflow/src/routes"
|
"anonymousoverflow/src/routes"
|
||||||
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -13,6 +17,12 @@ import (
|
||||||
"github.com/tavsec/gin-healthcheck/config"
|
"github.com/tavsec/gin-healthcheck/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/*
|
||||||
|
var templates embed.FS
|
||||||
|
|
||||||
|
//go:embed public/*
|
||||||
|
var public embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
env.RunChecks()
|
env.RunChecks()
|
||||||
|
@ -34,14 +44,16 @@ func main() {
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
r.LoadHTMLGlob("templates/*")
|
templ := template.Must(template.New("").ParseFS(templates, "templates/*"))
|
||||||
|
r.SetHTMLTemplate(templ)
|
||||||
|
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
r.Use(middleware.XssPreventionHeaders())
|
r.Use(middleware.XssPreventionHeaders())
|
||||||
r.Use(middleware.OptionsMiddleware())
|
r.Use(middleware.OptionsMiddleware())
|
||||||
r.Use(middleware.Ratelimit())
|
r.Use(middleware.Ratelimit())
|
||||||
|
|
||||||
r.GET("/static/*filepath", routes.StaticContent)
|
static, _ := fs.Sub(public, "public")
|
||||||
|
r.StaticFS("/static", http.FS(static))
|
||||||
|
|
||||||
r.GET("/robots.txt", func(c *gin.Context) {
|
r.GET("/robots.txt", func(c *gin.Context) {
|
||||||
c.String(200, "User-agent: *\nDisallow: /")
|
c.String(200, "User-agent: *\nDisallow: /")
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
package routes
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func StaticContent(c *gin.Context) {
|
|
||||||
cleanFilePath := strings.ReplaceAll(c.Param("filepath"), "..", "")
|
|
||||||
|
|
||||||
c.File(fmt.Sprintf("./public/%s", cleanFilePath))
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue