Skip to content

Commit a796ef1

Browse files
committed
embeds static content
1 parent d9f2034 commit a796ef1

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

cmd/go-postgres-sockets/main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
package main
1212

1313
import (
14+
"embed"
1415
"github.com/bebo-dot-dev/go-postgres-sockets/server/api"
1516
"github.com/bebo-dot-dev/go-postgres-sockets/server/database"
1617
"github.com/gorilla/handlers"
18+
"io/fs"
1719
"log"
1820
"net/http"
1921
)
2022

23+
//go:embed static
24+
var embeddedFiles embed.FS
25+
2126
func main() {
2227
log.Printf("Server started")
2328

@@ -28,10 +33,18 @@ func main() {
2833
controller := api.NewNotificationsApiController(service)
2934

3035
router := api.NewRouter(controller)
31-
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./cmd/go-postgres-sockets/static/")))
36+
router.PathPrefix("/").Handler(http.FileServer(getEmbeddedFileSystem()))
3237

3338
corsOrigins := handlers.AllowedOrigins([]string{"https://editor.swagger.io"})
3439
corsHeaders := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
3540
corsMethods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
3641
log.Fatal(http.ListenAndServe(":8080", handlers.CORS(corsOrigins, corsHeaders, corsMethods)(router)))
42+
}
43+
44+
func getEmbeddedFileSystem() http.FileSystem {
45+
f, err := fs.Sub(embeddedFiles, "static")
46+
if err != nil {
47+
panic(err)
48+
}
49+
return http.FS(f)
3750
}

0 commit comments

Comments
 (0)