forked from albertba/Advertisement_Panel
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Print("Now running server!")
|
|
// Serves index
|
|
http.HandleFunc("/", index_handler)
|
|
|
|
// Serves json for html to find file names
|
|
http.HandleFunc("/files", files_handler)
|
|
|
|
// Serves images
|
|
http.Handle("/images/", http.StripPrefix("/images/", http.FileServer(http.Dir("./static"))))
|
|
|
|
// Serves what ever the user is requesting based on above urls
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|
|
|
|
func index_handler(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "./static/index.html")
|
|
}
|
|
|
|
func files_handler(w http.ResponseWriter, r *http.Request) {
|
|
files, err := os.ReadDir("./static")
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
var names []string
|
|
for _, f := range files {
|
|
name := f.Name()
|
|
if strings.HasSuffix(name, ".jpg") ||
|
|
strings.HasSuffix(name, ".png") ||
|
|
strings.HasSuffix(name, ".gif") {
|
|
names = append(names, "/images/"+name)
|
|
}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(names)
|
|
}
|