Hello World in HTML

master
bel 2020-03-12 15:23:41 -06:00
parent 122806f438
commit 45c58cf0af
3 changed files with 25 additions and 16 deletions

View File

@ -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)

View File

@ -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)
}

View File

@ -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(),
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)
}
}