Serve static file from within a group with Gin - go

I want to server static file by mapping /fs to filesys in the disk. I can server static file like this:
r := gin.New()
r.Use(static.Serve("/fs", static.LocalFile("./filesys", false)))
// followed by other routes definition such as R.GET()
I also want to guard access by using a authentication middleware, without affecting other routes. I imagine it's something I need to do with Gin's group like this:
r := gin.New()
g := r.Group("/fs")
{ // what is the purpose of this parenthesis BTW?
g.Use(authMiddleWare)
g.Use(static.Serve("/fs", static.LocalFile(fileUploadDir, false)))
}
However, I can't get it to work. It is not routed in. If I do additional g.GET afterward, the path came out to be wrong.
How to go about this?

Hi I checked this issue has been open for 3 years on git with no solution for 3 years and the static package seems not being maintained anymore
This is an alternate solution that might help you
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
grp := r.Group("/static")
{
grp.StaticFS("", http.Dir("/your_directory"))
}
r.Run()
}

Related

GO Lang Web Application- Auto Compile and Reload [duplicate]

This question already has answers here:
Go Auto-Recompile and Reload Server on file change
(12 answers)
Closed 2 years ago.
Created basic web app and it is running on localhost:8080, I have to restart the server on each file change.
File changes take affect Ctrl +c (terminate program)and run again go program go run hello.go.
We do not want to terminate program of each file changes. If we do any changes and refresh browser new change take affect like PHP language
EX
First Program
package main
import (
"fmt"
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World")
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8080", nil)
}
Second Program
package main
import (
"fmt"
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Happy Coding")
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8080", nil)
}
Anyone have a solution to this?
You have to understand that Go & PHP are not the same at all. Go is a statically-typed, COMPILED language -- where PHP is a dynamically-typed, INTERPRETED language.
So using some special third party solution like the one mentioned in the comment above by #AyushGupta is probably your best bet. What you described in your question is the workflow when building services with Go. You compile a binary, run it & repeat when you make changes.

What is difference between router.Static() and router.Use(static.Serve()) on the gin?

I was just reading the documentation of gin and found there are 2 different way to set assets folder to the server, one is using Static() method of the router as follow:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.Static("/assets", "./assets")
Then the other is using static middleware as follows:
package main
import (
"github.com/gin-gonic/gin"
"github.com/gin-contrib/static"
)
func main() {
r := gin.Default()
r.Use(static.Serve("/", static.LocalFile("./assets", false)))
Is there any difference between these 2 ways about speed, side-effect, and so on?
First option from:
Static serves files from the given file system root.
Internally a http.FileServer is used, therefore http.NotFound is used instead
of the Router's NotFound handler.
Second option link:
Static returns a middleware handler that serves static files in the given directory.
both uses http.FileServer under the hood.

What is the idiomatic way to read urls with a file scheme as filenames for ReadFile?

Is there an idiomatic way to read a file from the system starting from a (file scheme) url and not a path?
I tried this first:
fileUrlStr := "file:///path/to/file.json"
jsonBuffer, _ := ioutil.ReadFile(fileUrlStr)
This is my current (mostly working version) but I'm concerned there are some gotchas that I'm missing, so I'm hoping there's a more tried and true way to do it:
fileUrlStr := "file:///path/to/file.json"
fileUrl, _ := url.Parse(fileUrlStr)
jsonBuffer, _ := ioutil.ReadFile(fileUrl.Path)
(Bonus if I can support both file:///Users/jdoe/temp.json and file:///c:/WINDOWS/clock.json without having to add code-paths accounting for them)
Using net/url, the solution that you were using, is the correct one.
It's properly deals with hostnames and paths across platforms and also gives you a chance to check the url scheme is the file scheme.
package main
import (
"fmt"
"net/url"
)
func main() {
for _, path := range []string{
"file:///path/to/file.json",
"file:///c:/WINDOWS/clock.json",
"file://localhost/path/to/file.json",
"file://localhost/c:/WINDOWS/clock.avi",
// A case that you probably don't need to handle given the rarity,
// but is a known legacy win32 issue when translating \\remotehost\share\dir\file.txt
"file:////remotehost/share/dir/file.txt",
} {
u, _ := url.ParseRequestURI(path)
fmt.Printf("url:%v\nscheme:%v host:%v Path:%v\n\n", u, u.Scheme, u.Host, u.Path)
}
}

Beego - Endpoint Testing

I am testing http custom endpoint for beego
package test
import (
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
_ "golife-api-cons/routers"
"net/http"
"net/http/httptest"
"path/filepath"
"runtime"
"testing"
)
func init() {
_, file, _, _ := runtime.Caller(1)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestGet is a sample to run an endpoint test
func TestGet(t *testing.T) {
r, _ := http.NewRequest("GET", "/my/endpoint/fetches/data", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestGet", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
When i run using go test -v ,
I get in response dial tcp :0: getsockopt: connection refused
I am using MariaDB running on my local,
I have verified using netstat -tulpn that my database is running perfectly fine (I get a valid response if i use postman and my server is running)
One weird observation , after inclusion of line _ "golife-api-cons/routers" i get this error even before test's are ran
My test passes with response 200 OK , but without any data as i get in response the above mentioned error
EDIT
The default path by used by TestBeegoInit function used is /path/to/my/project/test
which is not the desired path , so i tried giving the absolute path too , still i am not able to connect DB.
After much trying I came to know that beego initializes its variable called as AppPath in beego/conf.go like -
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
when you run your tests you run them with go test -v
but as a result the os.Args[0] is the text executable which will be /tmp/path/to/test and not path/to/app/exe
hence as a result it does not find config/app.conf which is in your app path which has db connection details.
Responsible line in beego/conf.go -
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
This all happens in beego's init function when you say
import (
"github.com/astaxie/beego"
_ "path/to/routers"
)
Hack for this is -
create a new package / file with init function which looks has -
package common
import (
"os"
"strings"
)
func init() {
cwd := os.Getenv("PWD")
rootDir := strings.Split(cwd, "tests/")
os.Args[0] = rootDir[0] // path to you dir
}
here you are changing os.Args[0] and assigning your directory path
make sure you import it before beego so now import will look like
import (
_ "path/to/common"
"github.com/astaxie/beego"
_ "path/to/routers"
)
And finally you connect to DB !
You are initializing your app as
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
Where file is the caller file.
TestBeegoInit is:
func TestBeegoInit(ap string) {
os.Setenv("BEEGO_RUNMODE", "test")
appConfigPath = filepath.Join(ap, "conf", "app.conf")
os.Chdir(ap)
initBeforeHTTPRun()
}
hence the location where your tests are looking for configuration is
<this_file>/../conf/app.conf
which basically is the default config file.
Basically you are not able to connect to the database. Perhaps because you are unknowingly connecting to your default database for the tests too. I suspect this is not what you are trying to do.

Webdav Server in Go

I want to implement a webdav-server with Go and found a new "x" package here:
But I don't know how to use this package to get it done.
Can someone help me with this issue?
I tried this:
func main(){
fs := new(webdav.FileSystem)
ls := new(webdav.LockSystem)
h := new(webdav.Handler)
h.FileSystem = *fs
h.LockSystem = *ls
//then use the Handler.ServeHTTP Method as the http.HandleFunc
http.HandleFunc("/", h.ServeHTTP)
http.ListenAndServe(":5555", nil)
}
If I try to connect to the server, I get an internal server error.
What am I doing wrong?
Thanks for your help.
The x/net/webdav is still in early phase of development. Many critical parts are still being implemented, and it can not be used as such at this moment. Taking a look at the source code over half of the necessary structures and functions are still completely missing.
Unfortunately there are no Go based webdav server implementations at this moment. (In case someone can correct me, please feel free to do so!)
func main(){
fs := new(webdav.FileSystem)
ls := new(webdav.LockSystem)
h := new(webdav.Handler)
h.FileSystem = fs
h.LockSystem = ls
//then use the Handler.ServeHTTP Method as the http.HandleFunc
http.HandleFunc("/", h.ServeHTTP)
http.ListenAndServe(":5555", nil)
}
try to remove the * before "fs" and "ls" because they are already pointers.
NB : if you have to assign pointer use & and not *
Create a webdav server on http://localhost:8080/ which mounts the folder C:\myfiles.
package main
import (
"net/http"
"golang.org/x/net/webdav"
)
func main() {
handler := &webdav.Handler{
FileSystem: webdav.Dir(`C:\myfiles`),
LockSystem: webdav.NewMemLS(),
}
http.ListenAndServe("localhost:8080", handler)
}
Mount to Letter E: in windows:
net use e: http://localhost:8080/
Open mounted drive in explorer
explorer.exe e:

Resources