Skip to content

Commit

Permalink
feat(server): release mode
Browse files Browse the repository at this point in the history
  • Loading branch information
a-wing committed Oct 28, 2023
1 parent 9b000d0 commit caa0051
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LD_FLAGS='\
'

GOBUILD=CGO_ENABLED=0 \
go build -trimpath -ldflags $(LD_FLAGS)
go build -tags release -trimpath -ldflags $(LD_FLAGS)

.PHONY: default
default: webapp build
Expand Down
8 changes: 8 additions & 0 deletions server/index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !release

package server

import "embed"

//go:embed index.html
var dist embed.FS
13 changes: 13 additions & 0 deletions server/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Not Found file</title>
</head>
<body>
<center>
This is not release mode, not found static pages
</center>
</body>
<html>
22 changes: 22 additions & 0 deletions server/release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build release

package server

import (
"embed"
"io/fs"
"log"
)

//go:embed build
var build embed.FS

var dist fs.FS

func init() {
var err error
dist, err = fs.Sub(build, "build")
if err != nil {
log.Fatal(err)
}
}
22 changes: 6 additions & 16 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ package server

import (
"context"
"embed"
"io/fs"
"log"
"net/http"
"os"
"time"

"github.com/a-wing/lightcable"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"

"filegogo/server/api"
"filegogo/server/config"
"filegogo/server/httpd"
"filegogo/server/store"
"filegogo/server/turnd"

"github.com/a-wing/lightcable"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)

//go:embed build
var dist embed.FS

func Run(cfg *config.Config) {
var turndServer *turnd.Server
if cfg.Turn != nil {
Expand Down Expand Up @@ -58,12 +53,7 @@ func Run(cfg *config.Config) {
// processing should be stopped.
r.Use(middleware.Timeout(60 * time.Second))

fsys, err := fs.Sub(dist, "build")
if err != nil {
log.Fatal(err)
}

r.Route("/" + cfg.Http.PathPrefix, func(r chi.Router) {
r.Route("/"+cfg.Http.PathPrefix, func(r chi.Router) {
r.Route("/api", func(r chi.Router) {
r.Get("/config", hander.GetConfig)
r.Handle("/signal/*", cable)
Expand All @@ -79,7 +69,7 @@ func Run(cfg *config.Config) {
})
})

r.Handle("/*", http.StripPrefix("/"+cfg.Http.PathPrefix, http.FileServer(httpd.NewSPA("index.html", http.FS(fsys)))))
r.Handle("/*", http.StripPrefix("/"+cfg.Http.PathPrefix, http.FileServer(httpd.NewSPA("index.html", http.FS(dist)))))
})

log.Printf("=== Listen Port: %s ===\n", cfg.Http.Listen)
Expand Down

0 comments on commit caa0051

Please sign in to comment.