-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from lologarithm/client_server
v1.1.0 Add server!
- Loading branch information
Showing
39 changed files
with
137 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
icons | ||
wowsim | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
GOOS=windows GOARCH=amd64 go build -o wowsim.exe web.go | ||
GOOS=darwin GOARCH=amd64 go build -o wowsim-amd64-darwin web.go | ||
GOOS=linux go build -o wowsim-amd64-linux web.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/lologarithm/wowsim | ||
|
||
go 1.15 | ||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
postMessage({ | ||
msg: "ready" | ||
}); | ||
|
||
var workerID = ""; | ||
|
||
addEventListener('message', async (e) => { | ||
const msg = e.data.msg; | ||
const payload = e.data.payload; | ||
|
||
if (msg == "apiCall") { | ||
const id = e.data.id; | ||
let url = '/api' | ||
|
||
let response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json;charset=utf-8' | ||
}, | ||
body: JSON.stringify(payload) | ||
}); | ||
|
||
let result = await response.json(); | ||
postMessage({ | ||
msg: "apiCall", | ||
payload: result, | ||
id: id, | ||
}); | ||
} else if (msg == "setID") { | ||
workerID = payload; | ||
postMessage({ msg: "idconfirm" }) | ||
} | ||
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"encoding/json" | ||
"flag" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/lologarithm/wowsim/tbc" | ||
) | ||
|
||
func init() { | ||
fs := http.FileServer(http.Dir(".")) | ||
//go:embed ui | ||
var uifs embed.FS | ||
|
||
func main() { | ||
var useFS = flag.Bool("usefs", false, "Use local file system and wasm. Set to true for dev") | ||
var host = flag.String("host", ":3333", "URL to host the interface on.") | ||
|
||
flag.Parse() | ||
|
||
var fs http.Handler | ||
if *useFS { | ||
log.Printf("Using local file system for development.") | ||
fs = http.FileServer(http.Dir(".")) | ||
} else { | ||
log.Printf("Embedded file server running.") | ||
fs = http.FileServer(http.FS(uifs)) | ||
} | ||
|
||
http.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) { | ||
resp.Header().Add("Cache-Control", "no-cache") | ||
if strings.HasSuffix(req.URL.Path, ".wasm") { | ||
resp.Header().Set("content-type", "application/wasm") | ||
} else if strings.HasSuffix(req.URL.Path, "/ui.js") { | ||
var uijs []byte | ||
var err error | ||
if *useFS { | ||
// read file straight off disk | ||
uijs, err = ioutil.ReadFile("./ui/ui.js") | ||
} else { | ||
uijs, err = uifs.ReadFile("ui/ui.js") | ||
// modify so that simworker is replaced with networker. | ||
uijs = bytes.Replace(uijs, []byte(`this.worker = new window.Worker('simworker.js');`), []byte(`this.worker = new window.Worker('networker.js');`), 1) | ||
} | ||
if err != nil { | ||
log.Printf("Failed to open file..., %s", err) | ||
} | ||
resp.Write(uijs) | ||
return | ||
} | ||
log.Printf("Serving: %s", req.URL.String()) | ||
fs.ServeHTTP(resp, req) | ||
}) | ||
// http.HandleFunc("/simtbc", simTBCPage) | ||
|
||
http.HandleFunc("/api", handleAPI) | ||
|
||
log.Printf("Launching interface on http://localhost%s/ui", *host) | ||
|
||
log.Printf("Closing: %s", http.ListenAndServe(*host, nil)) | ||
} | ||
|
||
func handleAPI(w http.ResponseWriter, r *http.Request) { | ||
st := time.Now() | ||
// Assumes input is a JSON object as a string | ||
request := tbc.ApiRequest{} | ||
body, _ := ioutil.ReadAll(r.Body) | ||
err := json.Unmarshal(body, &request) | ||
if err != nil { | ||
panic("Error parsing request: " + err.Error() + "\nRequest: " + string(body)) | ||
} | ||
result := tbc.ApiCall(request) | ||
resultData, err := json.Marshal(result) | ||
if err != nil { | ||
panic("Error marshaling result: " + err.Error()) | ||
} | ||
w.Write(resultData) | ||
|
||
log.Printf("API request took %v", time.Since(st)) | ||
} |