serving static files in different locations - http

I'm writing a Go Application that serves files in two different directories.
My project structure:
PROJECT_DIR
PROJECT_DIR/config
PROJECT_DIR/src
PROJECT_DIR/client
PROJECT_DIR/client/node_modules
PROJECT_DIR/client/www
in my main go file i start the file server using the following code:
func main() {
log.Print("started web server...");
httpsPortStr := ":" + strconv.FormatUint(config.CfgIni.HttpsPort, 10)
log.Printf("starting https web server at port %v", config.CfgIni.HttpsPort)
http.Handle("/", http.FileServer(http.Dir("client/www")))
http.Handle("/node_modules",http.FileServer(http.Dir(("client/node_modules"))))
err := http.ListenAndServeTLS(httpsPortStr, config.CfgIni.CertificateFile, config.CfgIni.PrivateKeyFile, nil)
if err != nil {
log.Fatalf("https server stopped with the following error: %v", err)
} else {
log.Print("https server stopped with no error")
}
}
as you can see I mapped / to client/www and /node_modules to client/node_modules.
when I try to access files on client/www for example https://host:port/test.html, it works great!
when I try to access files on client/node_modules for example: https://host:port/node_modules/test.html, I get 404 page not found.
test.html file exists in both location and is readable (no permission problems).
I'm probably configuring the routing wrong somehow.
any ideas?
thanks!

The FileServer is trying to route paths such as /node_modules/... to the file "client/node_modules/node_modules/..."
So use StripPrefix, eg:
http.Handle("/node_modules", http.StripPrefix("/node_modules", http.FileServer(http.Dir(("client/node_modules")))))
See another answer here.

Related

How do I dynamically change static files directory of the http server?

I have the following piece of code, that serves static files from a static files directory (staticFilesDir):
for _, prefix := range []string{"css", "img", "js", "static"} {
prefix = "/" + prefix + "/"
fs := http.FileServer(http.Dir(staticFilesDir + prefix))
r.PathPrefix(prefix).Handler(http.StripPrefix(prefix, fs))
}
This directory changes from time to time, and currently I always need to restart the server process to use the new value.
How can I reconfigure/reload the FileServer without restarting the whole process?
Additional complication to that: other handlers of the http server are executing long-running jobs (incl. child processes etc.), that I would like to keep untouched during this reload.
What is the standard solution of this quite typical task?
You can add a level of indirection in between:
type MyFileServer struct {
sync.RWMutex
http.FileServer
}
func (f *MyFileServer) SetDir(dir string) {
f.Lock()
defer f.Unlock()
f.FileServer=http.FileServer(dir)
}
func (f *MyFileServer) ServeHTTP(w http.ResponseWriter,req *http.Request) {
f.RLock()
defer f.RUnlock()
f.FileServer.ServeHTTP(w,req)
}

Is it possible to run http.ListenAndServe() AND ReadFromUDP() concurrently?

I am trying to write a simple web app that will listen for UDP packets.
But I can either only listen for UDP packets, or run the web app...
I am not familiar with GoLang, but here's the code I'm using to...
listen for UDP:
ServerConn, _ := net.ListenUDP("udp", &net.UDPAddr{IP:[]byte{#,#,#,#},Port:####,Zone:""})
defer ServerConn.Close()
buf := make([]byte, 1024)
for {
n, addr, _ := ServerConn.ReadFromUDP(buf)
fmt.Println("Received ", string(buf[0:n]), " from ", addr)
}
Server logic:
package main
We import 4 important libraries
1. “net/http” to access the core go http functionality
2. “fmt” for formatting our text
3. “html/template” a library that allows us to interact with our html file.
4. "time" - a library for working with date and time.
import (
"net/http"
"fmt"
"time"
"html/template"
)
//Create a struct that holds information to be displayed in our HTML file
type Welcome struct {
Name string
Time string
}
//Go application entrypoint
func main() {
//Instantiate a Welcome struct object and pass in some random information.
//We shall get the name of the user as a query parameter from the URL
welcome := Welcome{"Anonymous", time.Now().Format(time.Stamp)}
//We tell Go exactly where we can find our html file. We ask Go to parse the html file (Notice
// the relative path). We wrap it in a call to template.Must() which handles any errors and halts if there are fatal errors
templates := template.Must(template.ParseFiles("templates/welcome-template.html"))
//Our HTML comes with CSS that go needs to provide when we run the app. Here we tell go to create
// a handle that looks in the static directory, go then uses the "/static/" as a url that our
//html can refer to when looking for our css and other files.
http.Handle("/static/", //final url can be anything
http.StripPrefix("/static/",
http.FileServer(http.Dir("static")))) //Go looks in the relative "static" directory first using http.FileServer(), then matches it to a
//url of our choice as shown in http.Handle("/static/"). This url is what we need when referencing our css files
//once the server begins. Our html code would therefore be <link rel="stylesheet" href="/static/stylesheet/...">
//It is important to note the url in http.Handle can be whatever we like, so long as we are consistent.
//This method takes in the URL path "/" and a function that takes in a response writer, and a http request.
http.HandleFunc("/" , func(w http.ResponseWriter, r *http.Request) {
//Takes the name from the URL query e.g ?name=Martin, will set welcome.Name = Martin.
if name := r.FormValue("name"); name != "" {
welcome.Name = name;
}
//If errors show an internal server error message
//I also pass the welcome struct to the welcome-template.html file.
if err := templates.ExecuteTemplate(w, "welcome-template.html", welcome); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
//Start the web server, set the port to listen to 8080. Without a path it assumes localhost
//Print any errors from starting the webserver using fmt
fmt.Println("Listening");
fmt.Println(http.ListenAndServe(":8080", nil));
}
taken from(https://medium.com/google-cloud/building-a-go-web-app-from-scratch-to-deploying-on-google-cloud-part-1-building-a-simple-go-aee452a2e654)
I tried putting both of these extracts in 1 file, as well as running 2 files at the same time using
go run *.go
Any help would be appreciated!
You're going to need to start looking into goroutines - since you're asking to do two things concurrently. I suggest doing some reading into channels, goroutines, and concurrency in general :)

How to get path from config file

I would like to get path to my sqlite DB from a config file. How could I do that in Go?
This is a code, which I wrote before:
database, _ := sql.Open("sqlite3", "C:\\Users\\username\\project\\source.db")
In this case my path is "hard coded" directly in code. I would like to set a variable, which takes a path from a config data.
My first suggestion is that you use a JSON file rather than a YAML file for configuration, since Go supports it natively; you don't need to use any external packages.
type DBConfig struct {
Path string `json:"path"`
}
func loadConfig(path string) (*DBConfig, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var conf DBConfig
err = json.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}
My second suggestion is that you pass in the path to this config file in as a flag. You can supply a flag when you run your application like this:
$ go build . -o MyApp
$ ./MyApp --config=path/to/config/file
Flags are very powerful and allow you to easily configure your applications without changing much code. Using flags in Golang is simple.
var configPath = flag.String("config", "", "Path to file containing app config")
Just make sure that you add flag.Parse to the top of your main function in order to access them.
Here's a full example.
Good luck!

Switch from HTTP to HTTPS in Beego

I try to switch from HTTP to HTTPS:
func handler(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("This is an example server.\n"))
}
func main() {
http.HandleFunc("/", handler)
log.Printf("About to listen on 8080. Go to https://127.0.0.1:8080/")
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
if err != nil {
log.Fatal(err)
}
}
And I am getting the following error:
crypto/tls: failed to parse key PEM data
My application is running in HTTP mode now and I want it to run in HTTPS mode.
Can anyone suggest how to make it work in HTTPS?
The error indicates that the key.pem file cannot be parsed (could be invalid or lacking permission to read its content). Make sure the file is valid and sufficient permissions are set.
For testing purposes, use the generate_cert.go in the crypto/tls package to generate valid cert.pem and key.pem files.
To generate, run the following command (windows):
go run %GOROOT%/src/crypto/tls/generate_cert.go -host="127.0.0.1"
Linux:
go run $GOROOT/src/crypto/tls/generate_cert.go -host="127.0.0.1"

Serving HTTP Requests and Files using same server in Go Lang

Hi I am trying to create a server in Go Lang which serves files and HTTP Requests at the same time.
I want /upload path to accept post requests and
/files path to serve static files in the fpath
I tried with the following code but i get a 404 error
func main() {
fpath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
fpath+="/public"
fmt.Println(fpath)
http.HandleFunc("/upload",uploadFunc)
http.HandleFunc("/files",http.FileServer(http.Dir(fpath)))
panic(http.ListenAndServe(":8080", nil))
}
You need trailing slashes on your handle path if it's a directory. See http://golang.org/pkg/net/http/#ServeMux for more info.
Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash).
Try
func main() {
fpath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
fpath+="/public"
fmt.Println(fpath)
http.HandleFunc("/upload",uploadFunc)
http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(http.Dir(fpath))))
panic(http.ListenAndServe(":8080", nil))
}

Resources