Hello World in HTML

master
bel 2020-03-12 15:23:41 -06:00
parent 65655080dd
commit 6d39ef9aa2
3 changed files with 25 additions and 16 deletions

View File

@ -16,6 +16,7 @@ var (
StoreAddr string StoreAddr string
StoreUser string StoreUser string
StorePass string StorePass string
Root string
) )
func init() { func init() {
@ -34,6 +35,7 @@ func Refresh() {
as.Append(args.STRING, "storeaddr", "storage address", "") as.Append(args.STRING, "storeaddr", "storage address", "")
as.Append(args.STRING, "storeuser", "storage username", "") as.Append(args.STRING, "storeuser", "storage username", "")
as.Append(args.STRING, "storepass", "storage password", "") as.Append(args.STRING, "storepass", "storage password", "")
as.Append(args.STRING, "root", "root for static files", "./public")
if err := as.Parse(); err != nil { if err := as.Parse(); err != nil {
panic(err) panic(err)
} }
@ -44,6 +46,7 @@ func Refresh() {
StoreAddr = as.Get("storeaddr").GetString() StoreAddr = as.Get("storeaddr").GetString()
StoreUser = as.Get("storeuser").GetString() StoreUser = as.Get("storeuser").GetString()
StorePass = as.Get("storepass").GetString() StorePass = as.Get("storepass").GetString()
Root = as.Get("root").GetString()
if db, err := storage.New(storage.TypeFromString(StoreType), StoreAddr, StoreUser, StorePass); err != nil { if db, err := storage.New(storage.TypeFromString(StoreType), StoreAddr, StoreUser, StorePass); err != nil {
panic(err) panic(err)

View File

@ -2,10 +2,8 @@ package server
import ( import (
"fmt" "fmt"
"local/gziphttp"
"local/router" "local/router"
"net/http" "net/http"
"path/filepath"
) )
func (s *Server) Routes() error { func (s *Server) Routes() error {
@ -16,7 +14,7 @@ func (s *Server) Routes() error {
}{ }{
{ {
path: fmt.Sprintf("%s%s", wildcard, wildcard), 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 return nil
} }
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc { func (s *Server) static(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { s.fileServer.ServeHTTP(w, r)
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)
}
} }

View File

@ -2,19 +2,23 @@ package server
import ( import (
"local/firestormy/config" "local/firestormy/config"
"local/gziphttp"
"local/oauth2/oauth2client" "local/oauth2/oauth2client"
"local/router" "local/router"
"log" "log"
"net/http" "net/http"
"path/filepath"
) )
type Server struct { type Server struct {
*router.Router *router.Router
fileServer http.Handler
} }
func New() *Server { func New() *Server {
return &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) 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)
}
}