Created
May 14, 2013 13:03
-
-
Save pmn/5575685 to your computer and use it in GitHub Desktop.
A basic webserver in Go, including a route table loaded from a subpackage
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
package main | |
import ( | |
"fmt" | |
"github.com/gorilla/mux" | |
"html/template" | |
"mywebsite/api" | |
"log" | |
"net/http" | |
"os" | |
) | |
func defaultHandler(w http.ResponseWriter, h *http.Request) { | |
t, err := template.ParseFiles("templates/index.html") | |
if err != nil { | |
panic("Couldn't parse the template file") | |
} | |
t.Execute(w, nil) | |
} | |
func aboutHandler(w http.ResponseWriter, h *http.Request) { | |
fmt.Fprint(w, "About this website") | |
} | |
func Log(handler http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
remoteAddr := r.RemoteAddr | |
if len(remoteAddr) == 0 { | |
remoteAddr = r.Header.Get("x-forwarded-for") | |
} | |
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) | |
handler.ServeHTTP(w, r) | |
}) | |
} | |
func main() { | |
// Set up the site's routes | |
r := mux.NewRouter() | |
r.HandleFunc("/", defaultHandler) | |
r.HandleFunc("/about", aboutHandler) | |
// Register the API routes and attach them to "/api" | |
apiRouter := r.PathPrefix("/api").Subrouter() | |
api.RegisterRoutes(apiRouter) | |
port := os.Getenv("PORT") | |
if len(port) == 0 { | |
port = "8080" | |
} | |
http.Handle("/", r) | |
http.Handle("/static/", | |
http.StripPrefix("/static", http.FileServer(http.Dir("./static")))) | |
log.Printf("[+] Starting Application...") | |
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
package api | |
import ( | |
"fmt" | |
"github.com/gorilla/mux" | |
"net/http" | |
) | |
func defaultHandler(w http.ResponseWriter, h *http.Request) { | |
fmt.Fprint(w, "API Documentation will be coming at a later date") | |
} | |
func pingHandler(w http.ResponseWriter, h *http.Request) { | |
// pingHandler returns the userName from the user's auth cookie | |
fmt.Fprint(w, "ping") | |
} | |
func RegisterRoutes(r *mux.Router) { | |
r.HandleFunc("/", defaultHandler) | |
// The ping handler tests for whether or not the user is authenticated | |
r.HandleFunc("/ping", RequireAuth(WhoAmI)) | |
// Authorization routes | |
r.HandleFunc("/register", RegisterAccount).Methods("POST") | |
r.HandleFunc("/auth", LogIn).Methods("POST") | |
r.HandleFunc("/auth/logout", RequireAuth(LogOut)) | |
r.HandleFunc("/changepassword", RequireAuth(ChangePassword)) | |
r.HandleFunc("/forgotpassword", ForgotPassword) | |
// Client handlers | |
r.HandleFunc("/client", RequireAuth(Compress(GetClients))).Methods("GET") | |
r.HandleFunc("/client", RequireAuth(AddClient)).Methods("POST") | |
r.HandleFunc("/client/{id}", RequireAuth(Compress(GetClient))).Methods("GET") | |
r.HandleFunc("/client/{id}", RequireAuth(UpdateClient)).Methods("POST") | |
r.HandleFunc("/client/{id}", RequireAuth(DeleteClient)).Methods("DELETE") | |
r.HandleFunc("/client/tagged/{tag}", RequireAuth(Compress(GetTaggedClients))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment