Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wolf committed Mar 26, 2012
0 parents commit ab6bb56
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
app.yaml
11 changes: 11 additions & 0 deletions LICENSE
@@ -0,0 +1,11 @@
Tumbolia Public License

Copyright 2012, Ryan Wolf <rwolf.webdev@gmail.com>

Copying and distribution of this file, with or without modification, are
permitted in any medium without royalty provided the copyright notice and this
notice are preserved.

TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. opan saurce LOL
8 changes: 8 additions & 0 deletions TODO
@@ -0,0 +1,8 @@
* readme
* use post instead of get for editing
* basic auth for editing
* backend replication to survive instance restarts
* styling for index.template
* make sure we're sending right content types
* html error pages
* env variables for question, basic auth header
8 changes: 8 additions & 0 deletions app.yaml.example
@@ -0,0 +1,8 @@
application: novelty
version: 1
runtime: go
api_version: 3

handlers:
- url: /.*
script: _go_app
8 changes: 8 additions & 0 deletions index.template
@@ -0,0 +1,8 @@
<html>
<head>
<title>{{html .Question}}</title>
</head>
<body>
<h1>{{html .Answer}}</h1>
</body>
</html>
72 changes: 72 additions & 0 deletions novelty/novelty.go
@@ -0,0 +1,72 @@
package hello

import (
"http"
"strings"
"template"
)

var question = "Is Ryan at the Office?"
var answer = "yes"
var rootTemplate = template.Must(template.New("").ParseFile("index.template"))

type Context struct{
Question string
Answer string
}

type RequestHandler func(w http.ResponseWriter, r *http.Request)

var routes = make(map[string]map[string]RequestHandler)

func addRoute(path string, method string, handler RequestHandler) {
if routes[path] == nil {
routes[path] = make(map[string]RequestHandler)
}
routes[path][method] = handler
}

func router(w http.ResponseWriter, r *http.Request) {
route := routes[r.URL.Path]
if route == nil {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
handler := route[r.Method]
if handler == nil {
//TODO: Is there a cleaner way to get a comma-seperated list of keys?
methods := make([]string, len(route))
i := 0
for m, _ := range route {
methods[i] = m
i++
}
w.Header().Set("Allow", strings.Join(methods, ","))
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
handler(w, r)
}

func init() {
addRoute("/", "GET", func(w http.ResponseWriter, r *http.Request) {
// TODO: Is there a way to skip the type and use an inline struct?
context := Context{ Question: question, Answer: answer }
err := rootTemplate.Execute(w, context)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
})

addRoute("/yes", "GET", func(w http.ResponseWriter, r *http.Request) {
answer = "yes"
http.Redirect(w, r, "", http.StatusFound)
})

addRoute("/no", "GET", func(w http.ResponseWriter, r *http.Request) {
answer = "no"
http.Redirect(w, r, "", http.StatusFound)
})

http.HandleFunc("/", router)
}

0 comments on commit ab6bb56

Please sign in to comment.