9

How do I get only the file name one.json from the following request: http://localhost/slow/one.json?

I just need to serve this file and others from the url? This is a test server that I need to respond very slow.

http.HandleFunc("/slow/", func(w http.ResponseWriter, r *http.Request) {
    log.Println("Slow...")
    log.Println(r.URL.Path[1:])
    time.Sleep(100 * time.Millisecond)
    http.ServeFile(w, r, r.URL.Path[1:])
})
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Chris G.
  • 23,930
  • 48
  • 177
  • 302
  • What's wrong with looking up the filename from Request.URL.Path? – Volker Jun 15 '17 at 09:12
  • It gives me "slow/one.json/" and I need to serve the file from the root. – Chris G. Jun 15 '17 at 09:31
  • 3
    What about using strings.Split() function and construct the file path from the root? – rnk Jun 15 '17 at 09:54
  • Where is your current attempt, and what's wrong with it? – Jonathan Hall Jun 15 '17 at 09:55
  • 3
    Easiest / recommended to use [`http.StripPrefix()`](https://golang.org/pkg/net/http/#StripPrefix) for such things: [Why do I need to use http.StripPrefix to access my static files?](https://stackoverflow.com/questions/27945310/why-do-i-need-to-use-http-stripprefix-to-access-my-static-files/27946132#27946132). Also about serving a file: [Include js file in Go template](https://stackoverflow.com/questions/28899675/include-js-file-in-go-template/28899786#28899786) – icza Jun 15 '17 at 10:05
  • Agree http.StripPrefix() but how can I use http.StripPrefix() in a function. I need to set a timer also? – Chris G. Jun 15 '17 at 10:11
  • Hmm, how would you use the http.StripPrefix() in a function? – Chris G. Jun 15 '17 at 10:27
  • @ChrisG. Have you checked the answers I linked? There is extensive explanation how to use `http.StripPrefix()` in them. – icza Jun 15 '17 at 10:34
  • @icza thanks. I ended up using NewServeMux and using different folders. – Chris G. Jun 15 '17 at 10:57

2 Answers2

19

I believe you are looking for path.Base: "Base returns the last element of path."

r,_ := http.NewRequest("GET", "http://localhost/slow/one.json", nil)
fmt.Println(path.Base(r.URL.Path))
// one.json

Playground link

Adrian
  • 42,911
  • 6
  • 107
  • 99
-1

Created two folders slow and fast and then I ended up using the following:

package main

import (
    "log"
    "net/http"
    "time"
    "fmt"
)

func main() {

    h := http.NewServeMux()

    h.HandleFunc("/fast/", func(w http.ResponseWriter, r *http.Request) {

        fmt.Println(r.URL.Path[1:])
        time.Sleep(100 * time.Millisecond)
        http.ServeFile(w, r, r.URL.Path[1:])

    })

    h.HandleFunc("/slow/", func(w http.ResponseWriter, r *http.Request) {

        fmt.Println(r.URL.Path[1:])
        time.Sleep(6000 * time.Millisecond)
        http.ServeFile(w, r, r.URL.Path[1:])

    })

    h.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(404)
    })

    err := http.ListenAndServe(":8080", h)
    log.Fatal(err)
}
Chris G.
  • 23,930
  • 48
  • 177
  • 302