major refactoring of the api

This commit is contained in:
didier amyot committed 2015-03-28 20:53:28 -07:00
1 parent d4f7dc57fc
commit f0628e4682
38 files changed
+821 -367

No files matched your search

+16 -12
View File
@@ -2,32 +2,36 @@ package main
import (
"fmt"
"github.com/didiercrunch/doormanserver/api"
"github.com/didiercrunch/doormanserver/shared"
"log"
"net/http"
"path"
"strconv"
"github.com/didiercrunch/doormanserver/shared"
"github.com/gorilla/mux"
)
func createMuxRouter() http.Handler {
r := mux.NewRouter()
serveApi(r.PathPrefix("/api").Subrouter())
serveStatic(r.PathPrefix("/").Subrouter())
return r
}
const STATIC_DIR_PATH = "static/"
func GetAddressToServe() string {
params := shared.GetParams()
return params.Host + ":" + strconv.Itoa(params.Port)
}
func serveStatic2(w http.ResponseWriter, request *http.Request) {
filepath := request.URL.Path
w.Header().Set("Cache-Control", "public, max-age=43200")
fmt.Println(STATIC_DIR_PATH, filepath, path.Join(STATIC_DIR_PATH, filepath))
http.ServeFile(w, request, path.Join(STATIC_DIR_PATH, filepath))
}
func main() {
r := createMuxRouter()
http.Handle("/", r)
mux := http.NewServeMux()
mux.Handle("/api/", api.Create())
mux.HandleFunc("/", serveStatic2)
address := GetAddressToServe()
fmt.Println("server running at", address)
if err := http.ListenAndServe(address, createMuxRouter()); err != nil {
if err := http.ListenAndServe(address, mux); err != nil {
log.Fatal(err)
}
}