From 45c58cf0afdd5759622a24266f5d04c55ee164d4 Mon Sep 17 00:00:00 2001 From: bel Date: Thu, 12 Mar 2020 15:23:41 -0600 Subject: [PATCH] Hello World in HTML --- config/config.go | 3 +++ server/routes.go | 18 +++--------------- server/server.go | 20 +++++++++++++++++++- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index d3062ad..7094fda 100755 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ var ( StoreAddr string StoreUser string StorePass string + Root string ) func init() { @@ -34,6 +35,7 @@ func Refresh() { as.Append(args.STRING, "storeaddr", "storage address", "") as.Append(args.STRING, "storeuser", "storage username", "") as.Append(args.STRING, "storepass", "storage password", "") + as.Append(args.STRING, "root", "root for static files", "./public") if err := as.Parse(); err != nil { panic(err) } @@ -44,6 +46,7 @@ func Refresh() { StoreAddr = as.Get("storeaddr").GetString() StoreUser = as.Get("storeuser").GetString() StorePass = as.Get("storepass").GetString() + Root = as.Get("root").GetString() if db, err := storage.New(storage.TypeFromString(StoreType), StoreAddr, StoreUser, StorePass); err != nil { panic(err) diff --git a/server/routes.go b/server/routes.go index 90946b8..c77cf1f 100755 --- a/server/routes.go +++ b/server/routes.go @@ -2,10 +2,8 @@ package server import ( "fmt" - "local/gziphttp" "local/router" "net/http" - "path/filepath" ) func (s *Server) Routes() error { @@ -16,7 +14,7 @@ func (s *Server) Routes() error { }{ { path: fmt.Sprintf("%s%s", wildcard, wildcard), - handler: s.gzip(s.authenticate(http.NotFound)), + handler: s.gzip(s.authenticate(s.static)), }, } @@ -28,16 +26,6 @@ func (s *Server) Routes() error { return nil } -func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if gziphttp.Can(r) { - gz := gziphttp.New(w) - defer gz.Close() - w = gz - } - if filepath.Ext(r.URL.Path) == ".css" { - w.Header().Set("Content-Type", "text/css; charset=utf-8") - } - h(w, r) - } +func (s *Server) static(w http.ResponseWriter, r *http.Request) { + s.fileServer.ServeHTTP(w, r) } diff --git a/server/server.go b/server/server.go index ee05aeb..c643a3e 100755 --- a/server/server.go +++ b/server/server.go @@ -2,19 +2,23 @@ package server import ( "local/firestormy/config" + "local/gziphttp" "local/oauth2/oauth2client" "local/router" "log" "net/http" + "path/filepath" ) type Server struct { *router.Router + fileServer http.Handler } func New() *Server { return &Server{ - Router: router.New(), + Router: router.New(), + fileServer: http.FileServer(http.Dir(config.Root)), } } @@ -30,3 +34,17 @@ func (s *Server) authenticate(foo http.HandlerFunc) http.HandlerFunc { foo(w, r) } } + +func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if gziphttp.Can(r) { + gz := gziphttp.New(w) + defer gz.Close() + w = gz + } + if filepath.Ext(r.URL.Path) == ".css" { + w.Header().Set("Content-Type", "text/css; charset=utf-8") + } + h(w, r) + } +}