Skip to content

Instantly share code, notes, and snippets.

@jeromenerf
Created July 31, 2014 17:15
Show Gist options
  • Save jeromenerf/0502536d6efae332af62 to your computer and use it in GitHub Desktop.
Save jeromenerf/0502536d6efae332af62 to your computer and use it in GitHub Desktop.
NIAAAAA
package main
import (
"fmt"
"log"
"net/http"
"path/filepath"
"strings"
"./locations"
"github.com/bmizerany/pat"
)
func Configs(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "text/html")
fmt.Fprintf(w, `<!DOCTYPE html>
<html>
<body>
<p>WIP</p>
</body>
</html>
`)
}
// Config serves configDirectory static files
func Config(w http.ResponseWriter, r *http.Request) {
subdomain := strings.Split(r.Host, ".")[0]
location := locations.Fetch(subdomain)
pathelements := strings.Split(r.URL.Path, "/")[2:] // remove leading /config/
path := strings.Join(pathelements, "/")
log.Println(path)
// global or location based resources
resourceprefix := pathelements[0]
resourcepath := ""
log.Println(resourceprefix)
switch resourceprefix {
case path:
resourcepath = path
case "location":
locationdatapath := strings.Join(pathelements[1:], "/")
resourcepath = filepath.Join("locations_data", location.Id, locationdatapath)
default:
http.NotFound(w, r)
}
resourcepath = filepath.FromSlash(filepath.Join("pouet", resourcepath))
//FIXME not secure enough
log.Println(resourcepath)
}
func main() {
m := pat.New()
m.Get("/config", http.HandlerFunc(Configs))
m.Get("/config/:path/", http.HandlerFunc(Config))
http.Handle("/", m)
// Start the HTTP server
addr := "127.0.0.1:4567"
log.Printf("Listening on http://%s/\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment