Refactoring
This commit is contained in:
parent
78bc74c928
commit
64f43f4a00
26
server.go
26
server.go
@ -8,28 +8,27 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type headers map[string]string
|
||||||
|
|
||||||
// Server base struct
|
// Server base struct
|
||||||
type Server struct {
|
type Server struct {
|
||||||
port string
|
port string
|
||||||
dirPath string
|
dirPath string
|
||||||
cors corsHeaders
|
cors headers
|
||||||
cache cacheHeaders
|
cache headers
|
||||||
}
|
}
|
||||||
|
|
||||||
type cacheHeaders map[string]string
|
var dCacheHdrs = headers{
|
||||||
type corsHeaders map[string]string
|
|
||||||
|
|
||||||
var defCacheHdrs = cacheHeaders{
|
|
||||||
"control": "no-cache",
|
"control": "no-cache",
|
||||||
}
|
}
|
||||||
|
|
||||||
var defCorsHdrs = corsHeaders{
|
var dCorsHdrs = headers{
|
||||||
"origin": "*",
|
"origin": "*",
|
||||||
"methods": "*",
|
"methods": "*",
|
||||||
"headers": "*",
|
"headers": "*",
|
||||||
}
|
}
|
||||||
|
|
||||||
func cacheHandler(hdrs cacheHeaders, next http.Handler) http.Handler {
|
func cacheHandler(hdrs headers, next http.Handler) http.Handler {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
for k, v := range hdrs {
|
for k, v := range hdrs {
|
||||||
w.Header().Add("Cache-"+strings.Title(k), v)
|
w.Header().Add("Cache-"+strings.Title(k), v)
|
||||||
@ -40,9 +39,9 @@ func cacheHandler(hdrs cacheHeaders, next http.Handler) http.Handler {
|
|||||||
return http.HandlerFunc(fn)
|
return http.HandlerFunc(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func corsHandler(cors corsHeaders, next http.Handler) http.Handler {
|
func corsHandler(hdrs headers, next http.Handler) http.Handler {
|
||||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||||
for k, v := range cors {
|
for k, v := range hdrs {
|
||||||
w.Header().Add("Access-Control-Allow-"+strings.Title(k), v)
|
w.Header().Add("Access-Control-Allow-"+strings.Title(k), v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +65,7 @@ func (s *Server) handle() http.Handler {
|
|||||||
path := s.dirPath + r.URL.Path
|
path := s.dirPath + r.URL.Path
|
||||||
file, err := os.Lstat(path)
|
file, err := os.Lstat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("File asd " + path + " not exists")
|
log.Println("File " + path + " not exists")
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -83,13 +82,13 @@ func (s *Server) handle() http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) parseArgs() {
|
func (s *Server) parseArgs() {
|
||||||
for k, v := range defCorsHdrs { // parse cors into headers
|
for k, v := range dCorsHdrs { // parse cors into headers
|
||||||
if s.cors[k] == "" { // if empty
|
if s.cors[k] == "" { // if empty
|
||||||
s.cors[k] = v
|
s.cors[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range defCacheHdrs {
|
for k, v := range dCacheHdrs {
|
||||||
if s.cache[k] == "" { // if empty
|
if s.cache[k] == "" { // if empty
|
||||||
s.cache[k] = v
|
s.cache[k] = v
|
||||||
}
|
}
|
||||||
@ -110,7 +109,6 @@ func handleDir(path string, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
response := strings.Join(outputDirList, "<br />")
|
response := strings.Join(outputDirList, "<br />")
|
||||||
w.Write([]byte(string(response)))
|
w.Write([]byte(string(response)))
|
||||||
log.Println("Served dir")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFile(path string, w http.ResponseWriter, r *http.Request) {
|
func handleFile(path string, w http.ResponseWriter, r *http.Request) {
|
||||||
|
Loading…
Reference in New Issue
Block a user