Skip to content

Commit

Permalink
playing with serving web requests
Browse files Browse the repository at this point in the history
  • Loading branch information
evillemez committed Oct 2, 2015
1 parent 27e7662 commit c4bde76
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
.otto
.otto
3 changes: 3 additions & 0 deletions Makefile
@@ -0,0 +1,3 @@
default:
(cd /opt/gopath && go install github.com/evillemez/grotto)

7 changes: 0 additions & 7 deletions extras.go

This file was deleted.

18 changes: 18 additions & 0 deletions handlers/static.go
@@ -0,0 +1,18 @@
package handlers

import (
"fmt"
"net/http"
)

func Index(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "Index")
}

func Name(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "Name")
}

func Misc(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, "Misc")
}
7 changes: 0 additions & 7 deletions hello/hello.go

This file was deleted.

12 changes: 8 additions & 4 deletions main.go
@@ -1,9 +1,13 @@
package main

import "github.com/evillemez/grotto/hello"
import (
"fmt"
"log"
"net/http"
)

func main() {
foo()

hello.SayHello()
fmt.Println("Starting...")
log.Fatal(http.ListenAndServe("127.0.0.1:8080", NewRouter()))
fmt.Println("Exited.")
}
36 changes: 36 additions & 0 deletions routes.go
@@ -0,0 +1,36 @@
package main

import (
"net/http"
"github.com/gorilla/mux"
"github.com/evillemez/grotto/handlers"
)

type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes {
Route {"Index", "GET", "/", handlers.Index},
Route {"Name", "GET", "/name/{name}", handlers.Name},
Route {"Misc", "GET", "/misc", handlers.Misc},
}

func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)

for _, route := range routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}

return router
}

0 comments on commit c4bde76

Please sign in to comment.