1616// Free Software Foundation, Inc.,
1717// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1818
19+ //go:generate statik -src=../static
20+
1921package server
2022
2123import (
2224 "fmt"
2325 "html/template"
26+ "io/ioutil"
2427 "net/http"
2528 "os"
2629 "time"
2730
28- rice "github.com/GeertJohan/go.rice"
31+ "github.com/rakyll/statik/fs"
32+ _ "github.com/ts2/ts2-sim-server/server/statik"
2933 "github.com/ts2/ts2-sim-server/simulation"
3034 log "gopkg.in/inconshreveable/log15.v2"
3135)
@@ -37,16 +41,11 @@ const (
3741)
3842
3943var (
40- sim * simulation.Simulation
41- hub * Hub
42- logger log.Logger
43- staticBox * rice.Box
44+ sim * simulation.Simulation
45+ hub * Hub
46+ logger log.Logger
4447)
4548
46- func init () {
47- staticBox = rice .MustFindBox ("../static" )
48- }
49-
5049// InitializeLogger creates the logger for the server module
5150func InitializeLogger (parentLogger log.Logger ) {
5251 logger = parentLogger .New ("module" , "server" )
@@ -75,8 +74,19 @@ func Run(s *simulation.Simulation, addr, port string) {
7574//
7675// /ws - WebSocket endpoint for all TS2 clients and managers.
7776func HttpdStart (addr , port string ) {
77+ statikFS , err := fs .New ()
78+ if err != nil {
79+ logger .Crit ("Unable to read statik FS" , "error" , err )
80+ return
81+ }
82+ http .Handle ("/static/" , http .StripPrefix ("/static/" , http .FileServer (statikFS )))
7883
79- homeTemplData , err := staticBox .String ("/index.html" )
84+ homeTemplFile , err := statikFS .Open ("/index.html" )
85+ if err != nil {
86+ logger .Crit ("Unable to read index.html from statikFS" , "error" , err )
87+ return
88+ }
89+ homeTemplData , err := ioutil .ReadAll (homeTemplFile )
8090 if err != nil {
8191 logger .Crit ("Unable to open `index.html` " , "error" , err )
8292 return
@@ -86,9 +96,6 @@ func HttpdStart(addr, port string) {
8696 http .HandleFunc ("/" , serveHome )
8797 http .HandleFunc ("/ws" , serveWs )
8898
89- staticFileServer := http .StripPrefix ("/static/" , http .FileServer (staticBox .HTTPBox ()))
90- http .Handle ("/static/" , staticFileServer )
91-
9299 serverAddress := fmt .Sprintf ("%s:%s" , addr , port )
93100 logger .Info ("Starting HTTP" , "submodule" , "http" , "address" , serverAddress )
94101 err = http .ListenAndServe (serverAddress , nil )
0 commit comments