Skip to content

Commit a3a8731

Browse files
shyimaragon999
authored andcommitted
Add go webserver
1 parent f862067 commit a3a8731

File tree

10 files changed

+810
-654
lines changed

10 files changed

+810
-654
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/node_modules/
22
/public/build/
3-
3+
/.idea
4+
/bindata.go
5+
/php-dump-server
46
.DS_Store

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SHELL := bash
2+
3+
setup:
4+
go install github.com/go-bindata/go-bindata/...
5+
6+
build:
7+
go-bindata public/ public/bundle/
8+
go build -ldflags="-s -w"

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module php-dump-server
2+
3+
go 1.15
4+
5+
require (
6+
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
7+
github.com/h2non/filetype v1.1.0
8+
gopkg.in/antage/eventsource.v1 v1.0.0-20150318155416-803f4c5af225
9+
)

go.sum

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/go-bindata/go-bindata v1.0.0 h1:DZ34txDXWn1DyWa+vQf7V9ANc2ILTtrEjtlsdJRF26M=
5+
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
6+
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
7+
github.com/gobuffalo/here v0.6.0 h1:hYrd0a6gDmWxBM4TnrGw8mQg24iSVoIkHEk7FodQcBI=
8+
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
9+
github.com/h2non/filetype v1.1.0 h1:Or/gjocJrJRNK/Cri/TDEKFjAR+cfG6eK65NGYB6gBA=
10+
github.com/h2non/filetype v1.1.0/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
11+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
12+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
13+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
14+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
15+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
16+
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
17+
github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQDXbLhiuI=
18+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
19+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
21+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
22+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
23+
gopkg.in/antage/eventsource.v1 v1.0.0-20150318155416-803f4c5af225 h1:xy+AV3uSExoRQc2qWXeZdbhFGwBFK/AmGlrBZEjbvuQ=
24+
gopkg.in/antage/eventsource.v1 v1.0.0-20150318155416-803f4c5af225/go.mod h1:SiXNRpUllqhl+GIw2V/BtKI7BUlz+uxov9vBFtXHqh8=
25+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
26+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
27+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
28+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
29+
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
30+
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

main.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"github.com/h2non/filetype"
7+
"gopkg.in/antage/eventsource.v1"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"sync"
12+
)
13+
14+
var es eventsource.EventSource
15+
var lockState = make(map[string]bool)
16+
var lockMutex = &sync.Mutex{}
17+
18+
func main() {
19+
es = eventsource.New(nil, func(request *http.Request) [][]byte {
20+
return [][]byte{[]byte("Access-Control-Allow-Origin: http://localhost:5000")}
21+
})
22+
defer es.Close()
23+
port := flag.Int("port", 9009, "Listen port for Server")
24+
25+
flag.Parse()
26+
27+
http.HandleFunc("/client", clientEvent)
28+
http.HandleFunc("/unlock", unlock)
29+
http.HandleFunc("/is-locked", isLocked)
30+
http.Handle("/events", es)
31+
http.HandleFunc("/", serveStaticFile)
32+
33+
err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
34+
if err != nil {
35+
log.Fatalln(err)
36+
}
37+
}
38+
39+
func clientEvent(w http.ResponseWriter, r *http.Request) {
40+
body, err := ioutil.ReadAll(r.Body)
41+
42+
if err != nil {
43+
w.WriteHeader(500)
44+
return
45+
}
46+
47+
log.Println("Incomming request from PD")
48+
es.SendEventMessage(string(body), "message", "1")
49+
50+
pdAction := r.Header.Get("pd-action")
51+
pdId := r.Header.Get("pd-id")
52+
53+
if pdAction == "pause" && len(pdId) > 0 {
54+
lockMutex.Lock()
55+
lockState[pdId] = true
56+
lockMutex.Unlock()
57+
log.Printf("Received lock request for %s\n", pdId)
58+
}
59+
}
60+
61+
func isLocked(w http.ResponseWriter, r *http.Request) {
62+
lockMutex.Lock()
63+
entry, ok := lockState[r.Header.Get("pd-id")]
64+
defer lockMutex.Unlock()
65+
66+
if !ok {
67+
w.Write([]byte("0"))
68+
return
69+
}
70+
71+
if entry {
72+
w.Write([]byte("1"))
73+
return
74+
}
75+
76+
w.Write([]byte("0"))
77+
}
78+
79+
func unlock(w http.ResponseWriter, r *http.Request) {
80+
lockMutex.Lock()
81+
defer lockMutex.Unlock()
82+
83+
id := r.Header.Get("pd-id")
84+
85+
_, ok := lockState[id]
86+
87+
if !ok {
88+
return
89+
}
90+
91+
delete(lockState, id)
92+
93+
log.Printf("Removed lock for %s\n", id)
94+
}
95+
96+
func serveStaticFile(w http.ResponseWriter, r *http.Request) {
97+
filePath := r.URL.Path
98+
99+
if filePath == "/" {
100+
filePath = "/index.html"
101+
}
102+
103+
file, err := Asset(fmt.Sprintf("public%s", filePath))
104+
if err != nil {
105+
w.WriteHeader(404)
106+
return
107+
}
108+
109+
kind := filetype.GetType(filePath)
110+
w.Header().Set("Content-Type", kind.MIME.Value)
111+
w.Write(file)
112+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"svelte": "^3.0.0"
1818
},
1919
"dependencies": {
20+
"@rollup/plugin-replace": "^2.3.4",
2021
"highlight.js": "^10.5.0",
2122
"sirv-cli": "^1.0.0",
2223
"svelte-highlight": "^0.6.2"

rollup.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import resolve from '@rollup/plugin-node-resolve';
44
import livereload from 'rollup-plugin-livereload';
55
import { terser } from 'rollup-plugin-terser';
66
import css from 'rollup-plugin-css-only';
7+
import replace from "@rollup/plugin-replace";
78

89
const production = !process.env.ROLLUP_WATCH;
910

@@ -31,12 +32,15 @@ function serve() {
3132
export default {
3233
input: 'src/main.js',
3334
output: {
34-
sourcemap: true,
35+
sourcemap: !production,
3536
format: 'iife',
3637
name: 'app',
3738
file: 'public/build/bundle.js'
3839
},
3940
plugins: [
41+
replace({
42+
isProduction: production,
43+
}),
4044
svelte({
4145
compilerOptions: {
4246
// enable run-time checks when not in production

src/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</svelte:head>
1111

1212
<main>
13-
{#each $messageStore as message}
13+
{#each $messageStore.reverse() as message}
1414
<Message message={message} />
1515
{/each}
1616
</main>

src/messageStore.js

Lines changed: 13 additions & 35 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)