2020-07-29 16:21:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
type headers map[string]string
|
|
|
|
|
2020-07-29 16:21:55 +02:00
|
|
|
// Server base struct
|
|
|
|
type Server struct {
|
|
|
|
port string
|
|
|
|
dirPath string
|
2020-09-12 23:54:30 +02:00
|
|
|
cors headers
|
|
|
|
cache headers
|
2020-07-29 16:21:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
var dCacheHdrs = headers{
|
2020-09-05 00:11:57 +02:00
|
|
|
"control": "no-cache",
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
var dCorsHdrs = headers{
|
2020-09-05 00:11:57 +02:00
|
|
|
"origin": "*",
|
|
|
|
"methods": "*",
|
|
|
|
"headers": "*",
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
func cacheHandler(hdrs headers, next http.Handler) http.Handler {
|
2020-09-05 00:11:57 +02:00
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for k, v := range hdrs {
|
|
|
|
w.Header().Add("Cache-"+strings.Title(k), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
func corsHandler(hdrs headers, next http.Handler) http.Handler {
|
2020-09-05 00:11:57 +02:00
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2020-09-12 23:54:30 +02:00
|
|
|
for k, v := range hdrs {
|
2020-09-05 00:11:57 +02:00
|
|
|
w.Header().Add("Access-Control-Allow-"+strings.Title(k), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
return http.HandlerFunc(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) serve() {
|
|
|
|
s.parseArgs()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/", cacheHandler(s.cache, corsHandler(s.cors, s.handle())))
|
2020-07-31 20:11:31 +02:00
|
|
|
|
|
|
|
log.Print("Listening on", s.port)
|
2020-09-05 00:11:57 +02:00
|
|
|
log.Fatal(http.ListenAndServe(s.port, mux))
|
2020-07-31 20:11:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 00:11:57 +02:00
|
|
|
func (s *Server) handle() http.Handler {
|
|
|
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
2020-07-29 16:21:55 +02:00
|
|
|
path := s.dirPath + r.URL.Path
|
|
|
|
file, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
2020-09-12 23:54:30 +02:00
|
|
|
log.Println("File " + path + " not exists")
|
2020-07-29 16:21:55 +02:00
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch mode := file.Mode(); {
|
|
|
|
case mode.IsDir():
|
2020-09-05 00:11:57 +02:00
|
|
|
handleDir(path, w, r)
|
|
|
|
case mode.IsRegular():
|
|
|
|
handleFile(path, w, r)
|
2020-07-29 16:21:55 +02:00
|
|
|
}
|
2020-09-05 00:11:57 +02:00
|
|
|
}
|
2020-07-29 16:21:55 +02:00
|
|
|
|
2020-09-05 00:11:57 +02:00
|
|
|
return http.HandlerFunc(fn)
|
2020-07-29 16:21:55 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 00:11:57 +02:00
|
|
|
func (s *Server) parseArgs() {
|
2020-09-12 23:54:30 +02:00
|
|
|
for k, v := range dCorsHdrs { // parse cors into headers
|
2020-09-05 00:11:57 +02:00
|
|
|
if s.cors[k] == "" { // if empty
|
|
|
|
s.cors[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 23:54:30 +02:00
|
|
|
for k, v := range dCacheHdrs {
|
2020-09-05 00:11:57 +02:00
|
|
|
if s.cache[k] == "" { // if empty
|
|
|
|
s.cache[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleDir(path string, w http.ResponseWriter, r *http.Request) {
|
2020-07-29 16:21:55 +02:00
|
|
|
log.Println("Serving " + path + " dir...")
|
|
|
|
dirList, err := ioutil.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var outputDirList []string
|
|
|
|
for _, file := range dirList {
|
|
|
|
fileURL := strings.TrimRight(r.RequestURI, "/") + "/" + file.Name()
|
|
|
|
outputDirList = append(outputDirList, "<a href=\""+fileURL+"\">"+file.Name()+"</a>")
|
|
|
|
}
|
|
|
|
response := strings.Join(outputDirList, "<br />")
|
|
|
|
w.Write([]byte(string(response)))
|
|
|
|
}
|
|
|
|
|
2020-09-05 00:11:57 +02:00
|
|
|
func handleFile(path string, w http.ResponseWriter, r *http.Request) {
|
2020-07-29 16:21:55 +02:00
|
|
|
log.Println("Serving " + path + " file...")
|
|
|
|
|
|
|
|
http.ServeFile(w, r, path)
|
|
|
|
}
|