Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb5c8cf
commit 76ef541
Showing
11 changed files
with
82,515 additions
and
22 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func index(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, "This is the index page") | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", index) | ||
http.ListenAndServe(":8000", mux) | ||
} |
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,20 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func greet(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
name := vars["name"] | ||
fmt.Fprintf(w, "今日は %s", name) | ||
} | ||
|
||
func main() { | ||
r := mux.NewRouter() | ||
r.HandleFunc("/greet/{name}", greet) | ||
http.ListenAndServe(":8000", r) | ||
} |
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,89 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
var booksdir string | ||
|
||
type JSONResult struct { | ||
Title string `json:"title"` | ||
Lines int `json:"lines,string"` | ||
} | ||
|
||
func bookController(w http.ResponseWriter, r *http.Request) { | ||
// get the name of the book | ||
vars := mux.Vars(r) | ||
book, ok := vars["book"] | ||
if !ok { | ||
http.Error(w, "no book specified", 404) | ||
return | ||
} | ||
|
||
// turn the name of the book into a file path | ||
path := filepath.Join(booksdir, book) | ||
|
||
// count the lines in the book | ||
lines, err := countLines(path) | ||
|
||
// if there was an error, report it back to the client and return | ||
if err != nil { | ||
http.Error(w, err.Error(), 500) | ||
return | ||
} | ||
|
||
// create our JSON result value | ||
res := &JSONResult{ | ||
Title: book, | ||
Lines: lines, | ||
} | ||
|
||
// create a JSON encoded using w to write JSON back to the client | ||
enc := json.NewEncoder(w) | ||
enc.Encode(res) | ||
} | ||
|
||
func countLines(path string) (int, error) { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer f.Close() | ||
|
||
sc := bufio.NewScanner(f) | ||
var lines int | ||
for sc.Scan() { | ||
lines++ | ||
} | ||
err = sc.Err() | ||
return lines, err | ||
} | ||
|
||
func main() { | ||
// check that os.Args[1] has the books directory | ||
if len(os.Args) < 2 { | ||
log.Fatal("usage: %s /path/to/books", os.Args[0]) | ||
} | ||
|
||
// record the books directory | ||
booksdir = os.Args[1] | ||
|
||
// set up our router | ||
r := mux.NewRouter() | ||
|
||
// register /books/{book} handler | ||
r.HandleFunc("/books/{book}", bookController) | ||
|
||
// listen and serve for ever | ||
err := http.ListenAndServe(":8080", r) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |