Please consider this sample Go code snippet,
package main
import (
"fmt"
"log"
"net/http"
"time"
)
func main() {
listen_at := ":3114"
go http.Handle("/", http.FileServer(http.Dir(".")))
//go http.Handle("/max", http.FileServer(http.Dir("."))) <-- Fails
go http.HandleFunc("/ping", ping_handler)
log.Fatal(http.ListenAndServe(listen_at, nil))
}
func ping_handler(w http.ResponseWriter, r *http.Request) {
a := time.Now()
layout := "2006-01-02-03-04-05-000"
fmt.Println("Got root!")
fmt.Println("r is", r)
fmt.Println("RemoteAddr is", r.RemoteAddr)
send_this := "OK GOT ping! " + a.Format(layout)
w.Write([]byte(send_this))
}
I've two questions:
(1) How can I change the FileServer to serve /max instead of / - my attempts failed, I get 404 for http://localhost:3114/max/ and http://localhost:3114/max.
(2) I wish to accept PUT requests to /max - how can I achieve this?
Please point me the right direction, thanks!
Edit 1
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
go http.HandleFunc("/ping", hello)
log.Fatal(http.ListenAndServe(":3114", nil))
}
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Println("STarting hello!")
log.Println("Got connection")
if r.URL.Path != "/ping" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
log.Println("Method:", r.Method)
switch r.Method {
case "GET":
send_this := "OK GOT ping! "
w.Write([]byte(send_this))
case "PUT":
fmt.Println("We got put!")
err := r.ParseForm()
checkErr(err)
fmt.Println("r now", r)
fmt.Println("r.Form", r.Form)
fmt.Println("r.PostForm", r.PostForm)
default:
send_this := "Please dont!"
w.Write([]byte(send_this))
fmt.Fprintf(w, "Unknown request!")
}
}
func checkErr(err error) {
if err != nil {
fmt.Println("Error", err)
}
}
I send a PUT request as curl -k http://localhost:3114/ping -T /tmp/a.go -v, it shows:
STarting hello!
2019/06/07 15:05:10 Got connection
2019/06/07 15:05:10 Method: PUT
We got put!
r now &{PUT /ping HTTP/1.1 1 1 map[Content-Length:[10115] Expect:[100-continue] User-Agent:[curl/7.47.0] Accept:[*/*]] 0xc4200ec2e0 <nil> 10115 [] false localhost:3114 map[] map[] <nil> map[] 127.0.0.1:64612 /ping <nil> <nil> <nil> 0xc42005e340}
r.Form map[]
r.PostForm map[]
How can I find the actual data, and the filename that came in from PUT?
From https://golang.org/pkg/net/http/#ServeMux:
Patterns name fixed, rooted paths, like "/favicon.ico", or rooted
subtrees, like "/images/" (note the trailing slash).
That means that /max, which is a fixed rooted path, will match only /max and the pattern /max/, which is a rooted subtree, will match /max/, any other path that starts with /max/, and by default it will also match /max.
http.Handle("/max/", http.FileServer(http.Dir(".")))
Depending on what the layout of your . directory is, you may need to use https://golang.org/pkg/net/http/#StripPrefix.
Let's say your directory contains two files:
.
├── foo.txt
└── max
└── bar.txt
Given the handler above, a request to /max/bar.txt will return the bar.txt file, but a request to /max/foo.txt or /foo.txt will return 404, no file.
So if you want to serve the files from the /max/ path, but your . directory doesn't have a max sub-directory then you can use StripPrefix to remove the /max prefix from the request's url path before it is passed on to the FileServer.
http.Handle("/max/", http.StripPrefix("/max/", http.FileServer(http.Dir("."))))
To handle PUT requests at the same route you need a custom handler.
type myhandler struct {
fs http.Handler
}
func (h myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "PUT" {
// business as usual
h.fs.ServeHTTP(w, r)
return
}
// handle PUT
// ...
}
And then to register it, do:
fs := http.StripPrefix("/max/", http.FileServer(http.Dir(".")))
http.Handle("/max/", myhandler{fs: fs})
Related
I am looking for a simple way to create dynamic routes with net/http (no routers like mux etc.)
Here is my current code:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
pages := r.URL.Query()["q"]
if len(pages) == 0 {
fmt.Fprintf(w, "§§§§§§§§§§ You need to specify a page §§§§§§§§§§")
return
}
page := pages[0]
var a Page
err := db.QueryRow("SELECT * FROM pages where page = ?", page).Scan(&a.Page, &a.Date, &a.Url)
a.Year = time.Now().UTC().Year()
if err != nil {
if err == sql.ErrNoRows {
fmt.Fprintf(w, "Page %s not found", page)
return
} else {
fmt.Fprintf(w, "Some error happened")
return
}
}
http.Redirect(w, r, a.Url, 301)
})
So now the URL sample.com/?q= works dynamically.
My objective is to work without having to use r.URL.Query()["q"] so directly /pagename
This is not a duplicate of Go url parameters mapping because it is a single level (not nested levels) AND many answers in that question refer to using an external library.
If you don't want to use any third-party libraries, you have to handle the parsing of the path yourself.
For start, you can do this:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
page := r.URL.Path[1:]
// do whatever logic you want
// mind that the page could be "multi/level/path/" as well
})
You can use http.HandleFunc.
In this function, a pattern ending in a slash defines a subtree.
You can register a handler function with the pattern "/page/" like the below example.
package main
import (
"net/http"
"fmt"
)
func handler(w http.ResponseWriter, r *http.Request) {
if is_valid_page(r.URL) {
fmt.Fprint(w, "This is a valid page")
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "Error 404 - Page not found")
}
}
func is_valid_page(page string) {
// check here if page is valid from url
}
func main() {
http.HandleFunc("/page/", handler)
http.ListenAndServe(":8080", nil)
}
more info you can find here: https://golang.org/pkg/net/http/#ServeMux
I am new to Golang, I am testing the net/http to run some path but I got some problem that I don't understand.
Here is my codes.
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404");
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
So, if I go to index http://localhost:8080/, I got empty content index content {{false } map[]} ,
And I goto http://localhost:8080/test I got the content correctly , test content {{false } map[data1:data 1 content data2:data 2 content]},
But when I go back to index http://localhost:8080/ there already content there index content {{false } map[data1:data 1 content data2:data 2 content]},
So question here, why am I not getting the empty struct content when I back to the index? I thought the struct will be in initial state with every single request? The http should be stateless, right?
What you are probably experiencing is the result of this code or something similar (your code does not compile):
package main
import (
"fmt"
"net/http"
)
var Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
Solution
With this you are creating a global variable Content that keeps its state across calls to the webserver. What you probably intended is this:
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
var c Content
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", c)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
var c Content
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
c.Data = data
fmt.Println("test content ", c)
}
Changes made
make Content a type as you already did in your sample (that way it is not a global variable any more but defining a type we can reuse)
declare Content in each call where it is needed (not globally as we do not want it to keep its content across server calls)
Essence
You cannot use a type without declaring a variable from it first. That is why your sample did not build. If you try go will complain that Content is not an expression.
I've a server in golang who handle folder path like that :
fs := http.FileServer(http.Dir("./assets"))
http.Handle("/Images/", fs)
http.ListenAndServe(":8000", nil)
But in this folder there are privates images, and it shouldn't be possible to access files. So how can i secure image access and prevent anybody to access content of folder.
like that for example :
If you want to block a directory using http package, maybe this will be useful to you :
https://groups.google.com/forum/#!topic/golang-nuts/bStLPdIVM6w
package main
import (
"net/http"
"os"
)
type justFilesFilesystem struct {
fs http.FileSystem
}
func (fs justFilesFilesystem) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
return neuteredReaddirFile{f}, nil
}
type neuteredReaddirFile struct {
http.File
}
func (f neuteredReaddirFile) Readdir(count int) ([]os.FileInfo, error) {
return nil, nil
}
func main() {
fs := justFilesFilesystem{http.Dir("/tmp/")}
http.ListenAndServe(":8080", http.FileServer(fs))
}
A little wrapper over FileServer() solves your problem, now you have to add some sort of logic to do Authorization, it looks like you have unique names, that's good, so I just filter the image name for you creating a map of names, now you can add something more dynamic like a key/store(memcached, redis. etc.) Hope you can follow the comments
package main
import (
"log"
"net/http"
"strings"
)
// put the allowed hashs or keys here
// you may consider put them in a key/value store
//
var allowedImages = map[string]bool{
"key-abc.jpg": true,
"key-123.jpg": true,
}
func main() {
http.Handle("/Images/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// here we can do any kind of checking, in this case we'll just split the url and
// check if the image name is in the allowedImages map, we can check in a DB or something
//
parts := strings.Split(r.URL.Path, "/")
imgName := parts[len(parts)-1]
if _, contains := allowedImages[imgName]; !contains { // if the map contains the image name
log.Printf("Not found image: %q path: %s\n", imgName, r.URL.Path)
// if the image is not found we write a 404
//
// Bonus: we don't list the directory, so nobody can know what's inside :)
//
http.NotFound(w, r)
return
}
log.Printf("Serving allowed image: %q\n", imgName)
fileServer := http.StripPrefix("/Images/", http.FileServer(http.Dir("./assets")))
fileServer.ServeHTTP(w, r) // StripPrefix() and FileServer() return a Handler that implements ServerHTTP()
}))
http.ListenAndServe(":8000", nil)
}
https://play.golang.org/p/ehrd_AWXim
How do you serve index.html (or some other static HTML file) using a go web server?
I just want a basic, static HTML file (like an article, for example) which I can serve from a go web server. The HTML should be modifiable outside of the go program, as it would be in the case while using HTML templates.
This is my web server which only hosts hard-coded text ("Hello world!").
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello world!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":3000", nil)
}
That task is very easy with Golang net/http package.
All You need to do is:
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
}
assuming that static files are in folder named static in the root directory of the project.
If it's in folder static, you'll have index.html file calling http://localhost:3000/ which will result in rendering that index file instead of listing all the files availible.
Additionally, calling any other file in that folder (for example http://localhost:3000/clients.html) will show that file, properly rendered by the browser (at least Chrome, Firefox and Safari :))
UPDATE: serving files from url different than "/"
If You want to serve files, say from folder ./public under url: localhost:3000/static You have to use additional function: func StripPrefix(prefix string, h Handler) Handler like this:
package main
import (
"net/http"
)
func main() {
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
http.ListenAndServe(":3000", nil)
}
Thanks to that, all your files from ./public are avalible under localhost:3000/static
Without http.StripPrefix function, if you would try to access file localhost:3000/static/test.html, the server would look for it in ./public/static/test.html
This is because the server treats the whole URI as a relative path to the file.
Fortunately, it's easily solved with the built-in function.
I prefer using http.ServeFile for this over http.FileServer. I wanted directory browsing disabled, a proper 404 if files are missing and an easy way to special case the index file. This way, you can just drop the built binary into a folder and it will serve everything relative to that binary. Of course, you can use strings.Replace on p if you have the files stored in another directory.
func main() {
fmt.Println("Now Listening on 80")
http.HandleFunc("/", serveFiles)
log.Fatal(http.ListenAndServe(":80", nil))
}
func serveFiles(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
p := "." + r.URL.Path
if p == "./" {
p = "./static/index.html"
}
http.ServeFile(w, r, p)
}
If you only want to serve 1 file and not a full directory, you can use http.ServeFile
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
NOT a FTP server: That is something different than what I intended, which would be to serve the index.html homepage, like a normal web server would. Like, when I go to mydomain.com in my browser, I want index.html rendered.
That is mainly what "Writing Web Applications" describes, and what a project like hugo (static html site generator) does.
It is about reading a file, and responsing with a ContentType "text/html":
func (server *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := server.renderFile(w, r.URL.Path)
if err != nil {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
server.fn404(w, r)
}
}
with renderFile() essentially reading and setting the right type:
file, err = ioutil.ReadFile(server.MediaPath + filename)
if ext != "" {
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
}
You can also use the Gorilla Mux Router to server static files.
Assuming you have a static folder and an index.html file in the root.
import "github.com/gorilla/mux"
func main(){
router := mux.NewRouter()
fs := http.FileServer(http.Dir("./static/"))
router.PathPrefix("/static/").Handler(http.StripPrefix("/static/", fs))
log.Fatal(http.ListenAndServe(":8080", router))
}
Example how custom serve mp3 file:
r := http.NewServeMux()
r.HandleFunc("/file/*", func(w http.ResponseWriter, r *http.Request) {
// Prepare file path
pathFile := strings.ReplaceAll(r.RequestURI, "/file/", "./my_path/")
f, err := os.Open(pathFile)
if f == nil || err != nil {
return
}
// Read file into memory
fileBytes, err := ioutil.ReadAll(f)
if err != nil {
log.Println(err)
_, _ = fmt.Fprintf(w, "Error file bytes")
return
}
// Check mime type
mime := http.DetectContentType(fileBytes)
if mime != "audio/mpeg" {
log.Println("Error file type")
_, _ = fmt.Fprintf(w, "Error file type")
return
}
// Custom headers
r.Header.Add("Content-Type", "audio/mpeg")
r.Header.Add("Cache-Control", "must-revalidate, post-check=0, pre-check=0")
r.Header.Add("Content-Description", "File Transfer")
r.Header.Add("Content-Disposition", "attachment; filename=file.mp3")
r.Header.Add("Content-Transfer-Encoding", "binary")
r.Header.Add("Expires", "0")
r.Header.Add("Pragma", "public")
r.Header.Add("Content-Length", strconv.Itoa(len(fileBytes)))
http.ServeFile(w, r, pathFile)
})
log.Fatal(http.ListenAndServe(":80", r))
This is easy in golang as:
package main
import (
"log"
"net/http"
)
func main() {
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("."))))
}
`
You can just do this and make sure to keep your HTML file as index.html
This will serve the index.html file (if you have in the root) to the browser on localhost:8080
func main() {
port := flag.String("p", "8080", "port to serve on")
directory := flag.String("d", ".", "static file folder")
flag.Parse()
http.Handle("/", http.FileServer(http.Dir(*directory)))
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
Let's take the following pattern:
package main
import (
"fmt"
"net/http"
)
func main() {
admin := http.NewServeMux()
admin.HandleFunc("/", root)
admin.HandleFunc("/foo", foo)
http.Handle("/admin", admin)
http.ListenAndServe(":4567", nil)
}
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Admin: ROOT")
}
func foo(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Admin: FOO")
}
How is it that when I run /admin, it will fire the root handler, but when I run /admin/foo it won't? Just to be clear, I'm not looking for an alternative package, I actually have a custom router, I'm just generally curious to what's going on here as this pattern isn't making much sense to me.
Like #DewyBroto said, you have to use the full path in the child mux.
You could make a wrapper like this:
func NewChildMux(prefix string, vars ...interface{}) *http.ServeMux {
sm := http.NewServeMux()
for i := 0; i < len(vars); i += 2 {
path := prefix + vars[i].(string)
sm.HandleFunc(path, vars[i+1].(func(http.ResponseWriter, *http.Request)))
}
return sm
}
func main() {
admin := NewChildMux("/admin",
"/", root,
"/foo/", foo,
)
http.Handle("/admin/", admin)
http.ListenAndServe(":4567", nil)
}
Try the following:
func main() {
admin := http.NewServeMux()
// Prefix all paths with the mount point. A ServeMux matches
// the full path, even when invoked from another ServeMux.
mountPoint := "/admin"
admin.HandleFunc(mountPoint, root)
admin.HandleFunc(mountPoint + "/foo", foo)
// Add a trailing "/" to the mount point to indicate a subtree match.
http.Handle(mountPoint + "/", admin)
http.ListenAndServe(":4567", nil)
}