Skip to content

Commit ae0b3b6

Browse files
committed
fix: embed static directory to serve webpage from cli
1 parent 9eaf605 commit ae0b3b6

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

.goreleaser.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ dockers:
9797
goos: linux
9898
goarch: amd64
9999
dockerfile: Dockerfile.server
100-
extra_files:
101-
- static
102100
image_templates:
103101
- "{{ .Env.REGISTRY }}/{{ .Env.PROJECT_SERVER_NAME }}:{{ .Version }}-amd64"
104102
- "{{ .Env.REGISTRY }}/{{ .Env.PROJECT_SERVER_NAME }}:latest-amd64"
@@ -113,8 +111,6 @@ dockers:
113111
goos: linux
114112
goarch: arm64
115113
dockerfile: Dockerfile.server
116-
extra_files:
117-
- static
118114
image_templates:
119115
- "{{ .Env.REGISTRY }}/{{ .Env.PROJECT_SERVER_NAME }}:{{ .Version }}-arm64v8"
120116
- "{{ .Env.REGISTRY }}/{{ .Env.PROJECT_SERVER_NAME }}:latest-arm64v8"

Dockerfile.server

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FROM alpine
33
WORKDIR /app
44

55
COPY dnsq dnsq
6-
COPY ./static/ static/
76

87
EXPOSE 8080
98

cmd/server.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"embed"
8+
79
"github.com/spf13/cobra"
810
"github.com/sunggun-yu/dnsq/internal/server"
911
)
1012

13+
// StaticFS is an embed.FS that contains static files from static directory
14+
var StaticFS embed.FS
15+
1116
// flags struct for server command
1217
type serverFlags struct {
1318
port int
@@ -16,24 +21,29 @@ type serverFlags struct {
1621
// serverCmd represents the server command
1722
var serverCmd = serverCommand()
1823

24+
// serverCommand returns a cobra.Command for server
1925
func serverCommand() *cobra.Command {
2026

2127
// add flags
2228
var flags serverFlags
2329

30+
// create a new cobra.Command
2431
cmd := &cobra.Command{
2532
Use: "server",
2633
Short: "A sub command for server",
2734
Run: func(cmd *cobra.Command, args []string) {
28-
server.Run(flags.port)
35+
srv := server.NewServer(flags.port, StaticFS)
36+
srv.Run()
2937
},
3038
}
3139

40+
// add flags
3241
cmd.Flags().IntVarP(&flags.port, "port", "p", 8080, "port number for the server")
3342

3443
return cmd
3544
}
3645

46+
// init registers the server command
3747
func init() {
3848
rootCmd.AddCommand(serverCmd)
3949
}

internal/server/server.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,65 @@
11
package server
22

33
import (
4+
"embed"
45
"fmt"
6+
"io/fs"
7+
"net/http"
8+
"path"
59

610
"github.com/gin-gonic/gin"
711
"github.com/sunggun-yu/dnsq/internal/server/handlers"
812
)
913

10-
func Run(port int) {
14+
type Server struct {
15+
Port int
16+
StaticFiles fs.FS
17+
}
18+
19+
// StripPrefixFS is a custom fs.FS that strips a given prefix from the file paths.
20+
type StripPrefixFS struct {
21+
fs fs.FS
22+
prefix string
23+
}
24+
25+
// override the Open method to strip the prefix
26+
func (s *StripPrefixFS) Open(name string) (fs.File, error) {
27+
return s.fs.Open(path.Join(s.prefix, name))
28+
}
29+
30+
// NewServer creates a new server with the given port and static files.
31+
func NewServer(port int, staticFiles embed.FS) *Server {
32+
33+
// Create a new fs.FS that strips the "static" prefix
34+
strippedFS := &StripPrefixFS{
35+
fs: staticFiles,
36+
prefix: "static",
37+
}
38+
39+
return &Server{
40+
Port: port,
41+
StaticFiles: strippedFS,
42+
}
43+
}
44+
45+
// Run starts the server and listens on the given port with the given static files.
46+
func (s *Server) Run() {
47+
1148
// Create a new Gin router
1249
r := gin.Default()
1350

1451
// Serve static files, such as index.html
15-
r.Static("/static", "./static")
52+
r.StaticFS("/static", http.FS(s.StaticFiles))
1653

1754
// API endpoint for DNS lookup
1855
r.GET("/api/lookup", handlers.DNSLookupHandler)
1956

20-
// Serve the index.html at the root
57+
// Serve the index.html at the root. do not specify any path to serve the index.html. it will gets too many redirects if you specify the path.
58+
// not sure why lol
2159
r.NoRoute(func(c *gin.Context) {
22-
c.File("./static/index.html")
60+
c.FileFromFS("", http.FS(s.StaticFiles))
2361
})
2462

2563
// Run the server on the given port
26-
r.Run(fmt.Sprintf(":%d", port))
64+
r.Run(fmt.Sprintf(":%d", s.Port))
2765
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
44
package main
55

66
import (
7+
"embed"
78
"fmt"
89

910
"github.com/sunggun-yu/dnsq/cmd"
@@ -13,6 +14,9 @@ var (
1314
version = "dev"
1415
commit = "none"
1516
date = "unknown"
17+
18+
//go:embed static
19+
staticFS embed.FS
1620
)
1721

1822
// Version returns version and build information. it will be injected from ldflags(goreleaser)
@@ -22,5 +26,6 @@ func Version() string {
2226

2327
func main() {
2428
cmd.SetVersion(Version())
29+
cmd.StaticFS = staticFS
2530
cmd.Execute()
2631
}

0 commit comments

Comments
 (0)