diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | derelict.go | 47 | ||||
-rw-r--r-- | recent.tmpl | 10 | ||||
-rw-r--r-- | view.tmpl | 15 |
4 files changed, 54 insertions, 19 deletions
@@ -1,3 +1,4 @@ __pycache__ *.json derelict +.derelict diff --git a/derelict.go b/derelict.go index 2f5e8b3..aac7c10 100644 --- a/derelict.go +++ b/derelict.go @@ -4,9 +4,11 @@ import ( "embed" "github.com/gorilla/mux" "html/template" + "io/ioutil" "log" "net/http" "os" + "sort" ) //go:embed *.json *.css *.js *.svg @@ -17,24 +19,63 @@ var internal embed.FS var templates *template.Template +var root string + func main() { + initRoot() + port := os.Getenv("DERELICTPORT") if port == "" { port = "8080" } + templates = template.Must(template.ParseFS(internal, "*.tmpl")) router := mux.NewRouter() router.HandleFunc("/", handleRecent).Methods("GET") - router.HandleFunc("/view", handleView).Queries("id", "{id:[_0-9]+}").Methods("GET") + router.HandleFunc("/view/{id:[_0-9]+}", handleView).Methods("GET") + router.HandleFunc("/battles/", handleBattlesPost).Methods("POST") + router.HandleFunc("/battles/{id:[_0-9]+}", handleBattlesId).Methods("GET") router.PathPrefix("/").Handler(http.FileServer(http.FS(content))) + log.Fatal(http.ListenAndServe(":"+port, router)) } +func initRoot() { + root = os.Getenv("DERELICTROOT") + if root == "" { + root = ".derelict" + } + os.Mkdir(root, 0755) + os.Mkdir(root+"/battles", 0755) +} + +func handleBattlesPost(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotImplemented) +} + +func handleBattlesId(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + w.Header().Add("Content-Type", "application/json") + http.ServeFile(w, r, root+"/battles/"+id) +} + func handleRecent(w http.ResponseWriter, r *http.Request) { - templates.ExecuteTemplate(w, "recent", "") + files, err := ioutil.ReadDir(root + "/battles") + if err != nil { + panic(err) + } + sort.Slice(files, func(lhs, rhs int) bool { + return files[lhs].ModTime().After(files[rhs].ModTime()) + }) + count := 10 + if count > len(files) { + count = len(files) + } + templates.ExecuteTemplate(w, "recent", files[:count]) } func handleView(w http.ResponseWriter, r *http.Request) { - templates.ExecuteTemplate(w, "view", "") + id := mux.Vars(r)["id"] + templates.ExecuteTemplate(w, "view", id) } diff --git a/recent.tmpl b/recent.tmpl index 8f0a48d..4a64d58 100644 --- a/recent.tmpl +++ b/recent.tmpl @@ -13,9 +13,13 @@ <section> <h2>Recent</h2> <table> - <tr><td><a href="#">Battle in Amamake</a><td>127 wrecks<td>3 grids<td>2021-03-29 18:00 - <tr><td><a href="#">Battle in Eytjangard</a><td>20 wrecks<td>2 grids<td>2021-03-24 18:00 - <tr><td><a href="#">Battle in N-RAEL</a><td>8 wrecks<td>1 grid<td>2021-03-22 18:00 +{{range .}} + <tr> + <td><a href="/view/{{.Name}}">{{.Name}}</a> + <td>some wrecks + <td>some grids + <td>{{.ModTime.Format "January 02, 2006 15:04 UTC"}} +{{end}} </table> <h2>About</h2> <p><strong>Derelict</strong> is a tool for 3D visualizations of after the battle wreckage fields for EVE Online. Main @@ -4,7 +4,7 @@ <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <link rel="stylesheet" type="text/css" href="/style.css"> -<title>Derelict</title> +<title>{{.}} - Derelict</title> <body> <div id="wrapper"> <div id="container"></div> @@ -12,18 +12,7 @@ <h1>Derelict</h1> <p>LMB and drag in space to rotate camera.<br>RMB and drag in space to move camera. <p>LMB wreck to open killmail.<br>RMB wreck to focus camera. - <p>Address must contain <code>id</code> parameter to show some wrecks. - <br>Currently there is no service that would offer an API to acquire battle reports, so only battles that I - uploaded manually are available as of now: - <ul> - <li><a href="?id=30002016_202012141900">Random nano gang</a> - <li><a href="?id=30002537_202104060300">Battle in Amamake</a> - <li><a href="?id=31000376_202103301800">Another nano gang</a> - <li><a href="?id=31001761_202012040000">Foxholers eviction defense</a> - <li><a href="?id=31000488_202104142200">Fight in J123450</a> - <li><a href="?id=30002134_202104152000">French fries nano</a> - </ul> - <p>I plan to address this issue. + <p>Scroll to zoom in/out. <h2>Grid</h2> <select id="grid"></select> <h2>Timeline</h2> |