diff --git a/app.json b/app.json deleted file mode 100644 index aa3e6afd..00000000 --- a/app.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "React Tutorial Server", - "description": "Code from the React tutorial", - "keywords": [ "react", "reactjs", "tutorial" ], - "repository": "https://github.com/reactjs/react-tutorial", - "logo": "https://facebook.github.io/react/img/logo.svg", - "website": "http://facebook.github.io/react/docs/tutorial.html", - "success_url": "/", - "env" : { - "BUILDPACK_URL": "https://github.com/heroku/heroku-buildpack-nodejs.git" - } -} - diff --git a/comments.json b/comments.json index 7bef77ad..a0a66875 100644 --- a/comments.json +++ b/comments.json @@ -8,5 +8,55 @@ "id": 1420070400000, "author": "Paul O’Shannessy", "text": "React is *great*!" + }, + { + "id": 1464862111929, + "author": "12312", + "text": "222" + }, + { + "id": 1464862114322, + "author": "222", + "text": "22" + }, + { + "id": 1464862118569, + "author": "11111", + "text": "111111" + }, + { + "id": 1464862165689, + "author": "22222", + "text": "2222" + }, + { + "id": 1464862194961, + "author": "11111", + "text": "22222222222" + }, + { + "id": 1464862368016, + "author": "hy", + "text": "121233" + }, + { + "id": 1464862371433, + "author": "222", + "text": "2222222" + }, + { + "id": 1464862416312, + "author": "jhgjgjgjg", + "text": "khkjhkhkhk" + }, + { + "id": 1464862508034, + "author": "2222", + "text": "1111" + }, + { + "id": 1464862518122, + "author": "121221", + "text": "2222" } -] +] \ No newline at end of file diff --git a/public/scripts/example.js b/public/scripts/example.js index c249427a..ccf861dc 100644 --- a/public/scripts/example.js +++ b/public/scripts/example.js @@ -10,6 +10,16 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/* + - CommentBox + - CommentList + - Comment + - CommentForm + */ + +/** + * Comment + */ var Comment = React.createClass({ rawMarkup: function() { var rawMarkup = marked(this.props.children.toString(), {sanitize: true}); @@ -28,6 +38,9 @@ var Comment = React.createClass({ } }); +/** + * CommentBox + */ var CommentBox = React.createClass({ loadCommentsFromServer: function() { $.ajax({ @@ -82,6 +95,9 @@ var CommentBox = React.createClass({ } }); +/** + * CommentList + */ var CommentList = React.createClass({ render: function() { var commentNodes = this.props.data.map(function(comment) { @@ -99,6 +115,9 @@ var CommentList = React.createClass({ } }); +/** + * CommentForm + */ var CommentForm = React.createClass({ getInitialState: function() { return {author: '', text: ''}; @@ -140,6 +159,9 @@ var CommentForm = React.createClass({ } }); +/** + * render view + */ ReactDOM.render( , document.getElementById('content') diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 632a1efa..00000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -Flask==0.10.1 diff --git a/server.go b/server.go deleted file mode 100644 index 934a4cfc..00000000 --- a/server.go +++ /dev/null @@ -1,112 +0,0 @@ -/** - * This file provided by Facebook is for non-commercial testing and evaluation - * purposes only. Facebook reserves all rights not expressly granted. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN - * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "log" - "net/http" - "os" - "sync" - "time" -) - -type comment struct { - ID int64 `json:"id"` - Author string `json:"author"` - Text string `json:"text"` -} - -const dataFile = "./comments.json" - -var commentMutex = new(sync.Mutex) - -// Handle comments -func handleComments(w http.ResponseWriter, r *http.Request) { - // Since multiple requests could come in at once, ensure we have a lock - // around all file operations - commentMutex.Lock() - defer commentMutex.Unlock() - - // Stat the file, so we can find its current permissions - fi, err := os.Stat(dataFile) - if err != nil { - http.Error(w, fmt.Sprintf("Unable to stat the data file (%s): %s", dataFile, err), http.StatusInternalServerError) - return - } - - // Read the comments from the file. - commentData, err := ioutil.ReadFile(dataFile) - if err != nil { - http.Error(w, fmt.Sprintf("Unable to read the data file (%s): %s", dataFile, err), http.StatusInternalServerError) - return - } - - switch r.Method { - case "POST": - // Decode the JSON data - var comments []comment - if err := json.Unmarshal(commentData, &comments); err != nil { - http.Error(w, fmt.Sprintf("Unable to Unmarshal comments from data file (%s): %s", dataFile, err), http.StatusInternalServerError) - return - } - - // Add a new comment to the in memory slice of comments - comments = append(comments, comment{ID: time.Now().UnixNano() / 1000000, Author: r.FormValue("author"), Text: r.FormValue("text")}) - - // Marshal the comments to indented json. - commentData, err = json.MarshalIndent(comments, "", " ") - if err != nil { - http.Error(w, fmt.Sprintf("Unable to marshal comments to json: %s", err), http.StatusInternalServerError) - return - } - - // Write out the comments to the file, preserving permissions - err := ioutil.WriteFile(dataFile, commentData, fi.Mode()) - if err != nil { - http.Error(w, fmt.Sprintf("Unable to write comments to data file (%s): %s", dataFile, err), http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Cache-Control", "no-cache") - w.Header().Set("Access-Control-Allow-Origin", "*") - io.Copy(w, bytes.NewReader(commentData)) - - case "GET": - w.Header().Set("Content-Type", "application/json") - w.Header().Set("Cache-Control", "no-cache") - w.Header().Set("Access-Control-Allow-Origin", "*") - // stream the contents of the file to the response - io.Copy(w, bytes.NewReader(commentData)) - - default: - // Don't know the method, so error - http.Error(w, fmt.Sprintf("Unsupported method: %s", r.Method), http.StatusMethodNotAllowed) - } -} - -func main() { - port := os.Getenv("PORT") - if port == "" { - port = "3000" - } - http.HandleFunc("/api/comments", handleComments) - http.Handle("/", http.FileServer(http.Dir("./public"))) - log.Println("Server started: http://localhost:" + port) - log.Fatal(http.ListenAndServe(":"+port, nil)) -} diff --git a/server.php b/server.php deleted file mode 100644 index 75fae215..00000000 --- a/server.php +++ /dev/null @@ -1,53 +0,0 @@ - round(microtime(true) * 1000), - 'author' => $_POST['author'], - 'text' => $_POST['text'] - ]; - - $comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT); - file_put_contents('comments.json', $comments); - } - header('Content-Type: application/json'); - header('Cache-Control: no-cache'); - header('Access-Control-Allow-Origin: *'); - echo $comments; - } else { - return false; - } -} diff --git a/server.pl b/server.pl deleted file mode 100644 index c3212b9c..00000000 --- a/server.pl +++ /dev/null @@ -1,38 +0,0 @@ -# This file provided by Facebook is for non-commercial testing and evaluation -# purposes only. Facebook reserves all rights not expressly granted. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -use Time::HiRes qw(gettimeofday); -use Mojolicious::Lite; -use Mojo::JSON qw(encode_json decode_json); - -app->static->paths->[0] = './public'; - -any '/' => sub { $_[0]->reply->static('index.html') }; - -any [qw(GET POST)] => '/api/comments' => sub { - my $self = shift; - my $comments = decode_json (do { local(@ARGV,$/) = 'comments.json';<> }); - $self->res->headers->cache_control('no-cache'); - $self->res->headers->access_control_allow_origin('*'); - - if ($self->req->method eq 'POST') - { - push @$comments, { - id => int(gettimeofday * 1000), - author => $self->param('author'), - text => $self->param('text'), - }; - open my $FILE, '>', 'comments.json'; - print $FILE encode_json($comments); - } - $self->render(json => $comments); -}; -my $port = $ENV{PORT} || 3000; -app->start('daemon', '-l', "http://*:$port"); diff --git a/server.py b/server.py deleted file mode 100644 index fad15a66..00000000 --- a/server.py +++ /dev/null @@ -1,44 +0,0 @@ -# This file provided by Facebook is for non-commercial testing and evaluation -# purposes only. Facebook reserves all rights not expressly granted. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -import json -import os -import time -from flask import Flask, Response, request - -app = Flask(__name__, static_url_path='', static_folder='public') -app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html')) - - -@app.route('/api/comments', methods=['GET', 'POST']) -def comments_handler(): - with open('comments.json', 'r') as f: - comments = json.loads(f.read()) - - if request.method == 'POST': - new_comment = request.form.to_dict() - new_comment['id'] = int(time.time() * 1000) - comments.append(new_comment) - - with open('comments.json', 'w') as f: - f.write(json.dumps(comments, indent=4, separators=(',', ': '))) - - return Response( - json.dumps(comments), - mimetype='application/json', - headers={ - 'Cache-Control': 'no-cache', - 'Access-Control-Allow-Origin': '*' - } - ) - - -if __name__ == '__main__': - app.run(port=int(os.environ.get("PORT", 3000))) diff --git a/server.rb b/server.rb deleted file mode 100644 index 698f4339..00000000 --- a/server.rb +++ /dev/null @@ -1,49 +0,0 @@ -# This file provided by Facebook is for non-commercial testing and evaluation -# purposes only. Facebook reserves all rights not expressly granted. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN -# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -require 'webrick' -require 'json' - -# default port to 3000 or overwrite with PORT variable by running -# $ PORT=3001 ruby server.rb -port = ENV['PORT'] ? ENV['PORT'].to_i : 3000 - -puts "Server started: http://localhost:#{port}/" - -root = File.expand_path './public' -server = WEBrick::HTTPServer.new Port: port, DocumentRoot: root - -server.mount_proc '/api/comments' do |req, res| - comments = JSON.parse(File.read('./comments.json', encoding: 'UTF-8')) - - if req.request_method == 'POST' - # Assume it's well formed - comment = { id: (Time.now.to_f * 1000).to_i } - req.query.each do |key, value| - comment[key] = value.force_encoding('UTF-8') unless key == 'id' - end - comments << comment - File.write( - './comments.json', - JSON.pretty_generate(comments, indent: ' '), - encoding: 'UTF-8' - ) - end - - # always return json - res['Content-Type'] = 'application/json' - res['Cache-Control'] = 'no-cache' - res['Access-Control-Allow-Origin'] = '*' - res.body = JSON.generate(comments) -end - -trap('INT') { server.shutdown } - -server.start