Skip to content

Instantly share code, notes, and snippets.

@danshearmur
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save danshearmur/508cea11f9a0211619a6 to your computer and use it in GitHub Desktop.

Select an option

Save danshearmur/508cea11f9a0211619a6 to your computer and use it in GitHub Desktop.

Revisions

  1. danshearmur revised this gist Jul 25, 2014. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -39,13 +39,14 @@ func serv(proxy string, port string) {

    http.HandleFunc("/css/", func(res http.ResponseWriter, req *http.Request) {
    path := req.URL.Path[1:]
    get, _ := http.Get(proxy + strings.Replace(path, "css", "", 1))
    get, err := http.Get(proxy + strings.Replace(path, "css", "", 1))
    if err != nil {
    http.NotFound(res, req)
    return
    }
    defer get.Body.Close()
    content, _ := ioutil.ReadAll(get.Body)
    res.Header().Set(
    "Content-Type",
    "text/css",
    )
    res.Header().Set("Content-Type", "text/css")
    io.WriteString(res, string(content))
    })

  2. Daniel Shearmur revised this gist Jul 25, 2014. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    Serve the current directory and map some extra paths to "/index.html"
    Also proxy "/css/*" to a seperate webservice
    * Serve the current directory
    * Map some extra paths to `/index.html`
    * Also proxy `/css/*` to a seperate webservice

    Usage:

  3. Daniel Shearmur revised this gist Jul 25, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,6 @@ Also proxy "/css/*" to a seperate webservice

    Usage:

    PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go
    ```
    PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go
    ```
  4. Daniel Shearmur revised this gist Jul 25, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,5 @@ Serve the current directory and map some extra paths to "/index.html"
    Also proxy "/css/*" to a seperate webservice

    Usage:
    ```PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go```

    PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go
  5. Daniel Shearmur revised this gist Jul 25, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,4 @@ Serve the current directory and map some extra paths to "/index.html"
    Also proxy "/css/*" to a seperate webservice

    Usage:
    PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go
    ```PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go```
  6. Daniel Shearmur revised this gist Jul 25, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    Serve the current directory and map some extra paths to "/index.html"
    Also proxy "/css/*" to a seperate webservice

    Usage:
    PORT=8080 PROXY_URL="http://localhost:5455/27850" go run main.go
  7. Daniel Shearmur created this gist Jul 25, 2014.
    68 changes: 68 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    package main

    import (
    "io"
    "io/ioutil"
    "net/http"
    "os"
    "strings"
    )

    var allowedPaths = []string{
    "photo",
    "characteristics",
    "interests",
    "personality",
    "introduction",
    "verification",
    }

    func contains(needle string, haystack []string) bool {
    for i := 0; i < len(haystack); i++ {
    if haystack[i] == needle {
    return true
    }
    }
    return false
    }

    func serv(proxy string, port string) {

    http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
    path := req.URL.Path[1:]
    if contains(path, allowedPaths) {
    http.ServeFile(res, req, "index.html")
    } else {
    http.StripPrefix("/", http.FileServer(http.Dir(""))).ServeHTTP(res, req)
    }
    })

    http.HandleFunc("/css/", func(res http.ResponseWriter, req *http.Request) {
    path := req.URL.Path[1:]
    get, _ := http.Get(proxy + strings.Replace(path, "css", "", 1))
    defer get.Body.Close()
    content, _ := ioutil.ReadAll(get.Body)
    res.Header().Set(
    "Content-Type",
    "text/css",
    )
    io.WriteString(res, string(content))
    })

    http.ListenAndServe(":"+port, nil)

    }

    func main() {

    proxy := os.Getenv("PROXY_URL")
    if proxy == "" {
    panic("please specify PROXY_URL environment variable")
    }
    port := os.Getenv("PORT")
    if port == "" {
    port = "9000"
    }
    serv(proxy, port)

    }