From a255d391b5fd2faef1bf60ca27772d1bba91cb3b Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 12 Apr 2020 17:28:05 +0000 Subject: [PATCH] Create public static files to serve --- config/config.go | 5 ++++- public/index.html | 8 ++++++++ server/routes.go | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 public/index.html diff --git a/config/config.go b/config/config.go index 1844511..148ded6 100644 --- a/config/config.go +++ b/config/config.go @@ -6,7 +6,8 @@ import ( ) var ( - Port string + Port string + Public string ) func init() { @@ -16,8 +17,10 @@ func init() { func New() { as := args.NewArgSet() as.Append(args.INT, "p", "port to listen on", 52222) + as.Append(args.STRING, "d", "dir with public files", "./public") if err := as.Parse(); err != nil { panic(err) } Port = fmt.Sprintf(":%d", as.GetInt("p")) + Public = as.GetString("d") } diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..73d7d6f --- /dev/null +++ b/public/index.html @@ -0,0 +1,8 @@ + + + + + +

Hi

+ + diff --git a/server/routes.go b/server/routes.go index 2d8eec0..eba530f 100644 --- a/server/routes.go +++ b/server/routes.go @@ -1,9 +1,12 @@ package server import ( + "fmt" + "local/cheqbooq/config" "local/cheqbooq/server/account" "local/cheqbooq/server/balance" "local/cheqbooq/server/transaction" + "local/router" "net/http" ) @@ -12,6 +15,7 @@ func (s *Server) Routes() error { "api/v1/balance": s.balance1, "api/v1/transaction": s.transaction1, "api/v1/account": s.account1, + fmt.Sprintf("%s%s", router.Wildcard, router.Wildcard): s.public, } for path, handler := range routes { if err := s.Add(path, handler); err != nil { @@ -51,3 +55,8 @@ func (s *Server) account1(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) } } + +func (s *Server) public(w http.ResponseWriter, r *http.Request) { + d := http.FileServer(http.Dir(config.Public)) + d.ServeHTTP(w, r) +}