-
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.
* [ch] enable sync between volumes - In order to update the docker container with a file change, a docker rebuild is required. This will stop the need to run a rebuild. * [ch] set docker timeout to `0` - The default timeout for docker `stop` and `restart` is `10` seconds. Please see [Docker Docs](https://docs.docker.com/compose/reference/restart/). * [ch] remove cmd to make entry file executable - This has been recently pointed out that this is not required as it is a php file. * [ch] add best practice to `ENTRYPOINT` - It is considered more performant to pass `ENTRYPOINT` and `CMD` an array of the required command. * [f] revert `ENTRYPOINT` to `ServerStub.php` * [ch] implement restart of app on changes - Separate demo and test with env specific Docker file and add script to allow for app to be restarted where there are any changes (create,delete,modify) made whilst deloping the application. This is more proof of concept as this will only work on a Linux OS with `inotify-tools` installed, but as a proof of concept it _seems_ to work gracefully. * [ch] Add CLI flags - Allow `h`, `--host`, `p`, `--port` to be passed to start the server. * [a] using GoLang to watch local dev - This will start up the ReactPHP server in a child process within the GoLang script. Killing the script will kill the child process as intended allowing for a better development environment. * [ch] improve initial output code - refs #17
- Loading branch information
1 parent
585486e
commit 2b63667
Showing
11 changed files
with
222 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/exec" | ||
"fmt" | ||
"io/ioutil" | ||
"encoding/json" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
) | ||
|
||
var ( | ||
settings struct { | ||
ServerPath string `json:"server_path"` | ||
Port string `json:"port"` | ||
Host string `json:"host"` | ||
DirectoriesToWatch []string `json:"directories_to_watch"` | ||
} | ||
ProcessID int = 0 | ||
) | ||
|
||
func clearScreen() { | ||
c := exec.Command("clear") | ||
c.Stdout = os.Stdout | ||
c.Run() | ||
} | ||
|
||
func startApp(pathToServer string, port string, host string) { | ||
if port != "" { | ||
port = "--port=" + port | ||
} | ||
|
||
if host != "" { | ||
host = "--host=" + host | ||
} | ||
|
||
systemWithoutOutput("php", pathToServer, port, host) | ||
} | ||
|
||
func stopApp() { | ||
killCommand := fmt.Sprintf("kill %d", ProcessID) | ||
systemWithoutOutput("sh","-c", killCommand) | ||
} | ||
|
||
func restartApp(serverPath string, port string, host string) { | ||
clearScreen() | ||
fmt.Println("Restarting app") | ||
stopApp() | ||
startApp(serverPath, port, host) | ||
} | ||
|
||
func systemWithoutOutput(cmd string, arg ...string) { | ||
command := exec.Command(cmd, arg...) | ||
command.Stdout = os.Stdout | ||
err := command.Start() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
ProcessID = command.Process.Pid | ||
} | ||
|
||
func getDevServerConfig () { | ||
jsonFile, err := ioutil.ReadFile("./reactor.config.json") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
json.Unmarshal(jsonFile, &settings) | ||
} | ||
|
||
func initConsole (directoriesBeingWatch []string) { | ||
fmt.Println("Running React App") | ||
fmt.Println("Current directories/files being watched:") | ||
for _, directory := range directoriesBeingWatch { | ||
fmt.Println("\t",directory) | ||
} | ||
fmt.Println("") | ||
} | ||
|
||
func main() { | ||
getDevServerConfig() | ||
|
||
var ( | ||
serverPath string = settings.ServerPath | ||
directoriesToWatch []string = settings.DirectoriesToWatch | ||
port string = settings.Port | ||
host string = settings.Host | ||
) | ||
|
||
startApp(serverPath, port, host) | ||
initConsole(directoriesToWatch) | ||
|
||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer watcher.Close() | ||
|
||
done := make(chan bool) | ||
|
||
go func() { | ||
for { | ||
select { | ||
case event := <-watcher.Events: | ||
if event.Op&fsnotify.Write == fsnotify.Write { | ||
restartApp(serverPath, port, host) | ||
} | ||
case err := <-watcher.Errors: | ||
log.Println("error:", err) | ||
} | ||
} | ||
}() | ||
|
||
for _, directory := range directoriesToWatch { | ||
err = watcher.Add(directory) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
<-done | ||
} |
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,11 @@ | ||
#!/bin/sh | ||
|
||
composer start | ||
|
||
echo "Watching \`./example\` and \`./src\`" | ||
|
||
while true; do | ||
filename=$(inotifywait -qre modify -e create -e move -e delete --format %f {./example,./src} ) | ||
printf "Restarted due to change in %s" $filename|awk '{split($0,a,"__"); print a[1]}' | ||
docker restart -t 0 reactive_slim >> /dev/null | ||
done |
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,14 @@ | ||
#!/bin/sh | ||
|
||
php ./example/app-prod-mode.php & | ||
|
||
echo "Watching \`./example\` and \`./src\`" | ||
|
||
while true; do | ||
filename=$(inotifywait -qre modify -e create -e move -e delete --format %f {./example,./src} ) | ||
printf "Restarted due to change in %s" $filename|awk '{split($0,a,"__"); print a[1]}' | ||
kill `ps aux|grep app-prod-mode.php | \ | ||
head -n1 | \ | ||
awk -d='\t' '{print $2}'` | ||
php ./example/app-prod-mode.php & | ||
done |
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,13 @@ | ||
#!/bin/sh | ||
|
||
# Build & run container | ||
docker build -t reactive_slim_test -f ./Dockerfile.test . | ||
docker run -d -p 1351:1351 --name reactive_tests reactive_slim_test | ||
|
||
# run tests | ||
./bin/phpspec r | ||
./bin/phpunit -c ./test/phpunit.xml.dist | ||
|
||
# tidy up | ||
docker kill reactive_tests | ||
docker rm -f reactive_tests |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM php:7.0-alpine | ||
|
||
COPY . /var/app | ||
WORKDIR /var/app | ||
|
||
RUN apk update | ||
RUN apk add --update zlib-dev | ||
RUN docker-php-ext-install -j$(getconf _NPROCESSORS_ONLN) iconv | ||
RUN rm -rf /tmp/* | ||
|
||
EXPOSE 1351 | ||
|
||
ENTRYPOINT ["php", "./test/Integration/ServerStub.php"] |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"server_path": "./example/app-dev-mode.php", | ||
"port": "1337", | ||
"host": "0.0.0.0", | ||
"directories_to_watch": [ | ||
"./example/", | ||
"./src/" | ||
] | ||
} |
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