mirror of
https://github.com/kataras/iris.git
synced 2026-07-31 16:39:52 +00:00
replace ioutil with io package and other minor improvements
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -105,5 +104,5 @@ func BindResponse(resp *http.Response, dest interface{}) error {
|
||||
func RawResponse(resp *http.Response) ([]byte, error) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
return ioutil.ReadAll(resp.Body)
|
||||
return io.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
@@ -51,7 +51,7 @@ func getExample() {
|
||||
}
|
||||
defer cr.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(cr)
|
||||
body, err := io.ReadAll(cr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func postExample() {
|
||||
}
|
||||
defer cr.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(cr)
|
||||
body, err := io.ReadAll(cr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
@@ -42,7 +42,7 @@ func getExample() {
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(r)
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -93,7 +93,7 @@ func postExample() {
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(r)
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -11,8 +11,8 @@ import (
|
||||
const addr = "127.0.0.1:8080"
|
||||
|
||||
/*
|
||||
$ go build -mod=mod -ldflags -H=windowsgui -o myapp.exe
|
||||
$ ./myapp.exe # run the app
|
||||
$ go build -mod=mod -ldflags -H=windowsgui -o myapp.exe
|
||||
$ ./myapp.exe # run the app
|
||||
*/
|
||||
func main() {
|
||||
go runServer()
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
const addr = "127.0.0.1:8080"
|
||||
|
||||
/*
|
||||
$ go build -mod=mod -ldflags="-H windowsgui" -o myapp.exe # build for windows
|
||||
$ ./myapp.exe # run
|
||||
$ go build -mod=mod -ldflags="-H windowsgui" -o myapp.exe # build for windows
|
||||
$ ./myapp.exe # run
|
||||
*/
|
||||
func main() {
|
||||
go runServer()
|
||||
|
||||
@@ -8,21 +8,21 @@ import (
|
||||
const addr = "127.0.0.1:8080"
|
||||
|
||||
/*
|
||||
# Windows requires special linker flags for GUI apps.
|
||||
# It's also recommended to use TDM-GCC-64 compiler for CGo.
|
||||
# http://tdm-gcc.tdragon.net/download
|
||||
#
|
||||
#
|
||||
$ go build -mod=mod -ldflags="-H windowsgui" -o myapp.exe # build for windows
|
||||
$ ./myapp.exe # run
|
||||
#
|
||||
# MacOS uses app bundles for GUI apps
|
||||
$ mkdir -p example.app/Contents/MacOS
|
||||
$ go build -o example.app/Contents/MacOS/example
|
||||
$ open example.app # Or click on the app in Finder
|
||||
#
|
||||
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
|
||||
# please refer to: https://github.com/webview/webview/issues/188
|
||||
# Windows requires special linker flags for GUI apps.
|
||||
# It's also recommended to use TDM-GCC-64 compiler for CGo.
|
||||
# http://tdm-gcc.tdragon.net/download
|
||||
#
|
||||
#
|
||||
$ go build -mod=mod -ldflags="-H windowsgui" -o myapp.exe # build for windows
|
||||
$ ./myapp.exe # run
|
||||
#
|
||||
# MacOS uses app bundles for GUI apps
|
||||
$ mkdir -p example.app/Contents/MacOS
|
||||
$ go build -o example.app/Contents/MacOS/example
|
||||
$ open example.app # Or click on the app in Finder
|
||||
#
|
||||
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
|
||||
# please refer to: https://github.com/webview/webview/issues/188
|
||||
*/
|
||||
func main() {
|
||||
go runServer()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -47,7 +47,7 @@ func (r resource) loadFromBase(dir string, strip string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// assets/css/main.css
|
||||
// assets/favicon.ico
|
||||
// assets/js/main.js
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -288,11 +287,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -348,7 +349,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -46,7 +46,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// ../embedding-files-into-app/assets/css/main.css
|
||||
// ../embedding-files-into-app/assets/favicon.ico
|
||||
// ../embedding-files-into-app/assets/js/main.js
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -288,11 +287,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -348,7 +349,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -48,7 +48,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// ../http2push/assets/app2/app2app3/css/main.css
|
||||
// ../http2push/assets/app2/app2app3/dirs/dir1/text.txt
|
||||
// ../http2push/assets/app2/app2app3/dirs/dir2/text.txt
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -464,11 +463,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -546,7 +547,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// ../http2push/assets/app2/app2app3/css/main.css
|
||||
// ../http2push/assets/app2/app2app3/dirs/dir1/text.txt
|
||||
// ../http2push/assets/app2/app2app3/dirs/dir2/text.txt
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -464,11 +463,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -546,7 +547,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+9
-8
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// public/app.js
|
||||
// public/css/main.css
|
||||
// public/index.html
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -288,11 +287,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -346,7 +347,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+8
-7
@@ -12,7 +12,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -310,11 +309,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
|
||||
@@ -377,7 +378,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
@@ -41,7 +41,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -40,7 +40,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
/*Package main is a proxy + accesslog example.
|
||||
/*
|
||||
Package main is a proxy + accesslog example.
|
||||
In this example we will make a small proxy which listens requests on "/proxy/+path".
|
||||
With two accesslog instances, one for the main application and one for the /proxy/ requests.
|
||||
Of cource, you could a single accesslog for the whole application, but for the sake of the example
|
||||
let's log them separately.
|
||||
|
||||
We will make use of iris.StripPrefix and host.ProxyHandler.*/
|
||||
We will make use of iris.StripPrefix and host.ProxyHandler.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
@@ -5,15 +5,15 @@ import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
pb "github.com/kataras/iris/v12/_examples/mvc/grpc-compatible/helloworld"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b, err := ioutil.ReadFile("../server.crt")
|
||||
b, err := os.ReadFile("../server.crt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -30,14 +30,16 @@ type UsersController struct {
|
||||
// curl -i -u admin:password http://localhost:8080/users
|
||||
//
|
||||
// The correct way if you have sensitive data:
|
||||
// func (c *UsersController) Get() (results []viewmodels.User) {
|
||||
// data := c.Service.GetAll()
|
||||
//
|
||||
// for _, user := range data {
|
||||
// results = append(results, viewmodels.User{user})
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// func (c *UsersController) Get() (results []viewmodels.User) {
|
||||
// data := c.Service.GetAll()
|
||||
//
|
||||
// for _, user := range data {
|
||||
// results = append(results, viewmodels.User{user})
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// otherwise just return the datamodels.
|
||||
func (c *UsersController) Get() (results []datamodels.User) {
|
||||
return c.Service.GetAll()
|
||||
|
||||
@@ -9,9 +9,11 @@ else as well, otherwise I am going with the first one:
|
||||
|
||||
```go
|
||||
// 1
|
||||
mvc.Configure(app.Party("/user"), func(m *mvc.Application) {
|
||||
m.Router.Use(cache.Handler(10*time.Second))
|
||||
})
|
||||
|
||||
mvc.Configure(app.Party("/user"), func(m *mvc.Application) {
|
||||
m.Router.Use(cache.Handler(10*time.Second))
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
```go
|
||||
@@ -32,10 +34,12 @@ mvc.Configure(userRouter, ...)
|
||||
```go
|
||||
// 4
|
||||
// same:
|
||||
app.PartyFunc("/user", func(r iris.Party){
|
||||
r.Use(cache.Handler(10*time.Second))
|
||||
mvc.Configure(r, ...)
|
||||
})
|
||||
|
||||
app.PartyFunc("/user", func(r iris.Party){
|
||||
r.Use(cache.Handler(10*time.Second))
|
||||
mvc.Configure(r, ...)
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
If you want to use a middleware for a single route,
|
||||
@@ -48,16 +52,18 @@ then you just call it on the method:
|
||||
var myMiddleware := myMiddleware.New(...) // this should return an iris/context.Handler
|
||||
|
||||
type UserController struct{}
|
||||
func (c *UserController) GetSomething(ctx iris.Context) {
|
||||
// ctx.Proceed checks if myMiddleware called `ctx.Next()`
|
||||
// inside it and returns true if so, otherwise false.
|
||||
nextCalled := ctx.Proceed(myMiddleware)
|
||||
if !nextCalled {
|
||||
return
|
||||
}
|
||||
|
||||
// else do the job here, it's allowed
|
||||
}
|
||||
func (c *UserController) GetSomething(ctx iris.Context) {
|
||||
// ctx.Proceed checks if myMiddleware called `ctx.Next()`
|
||||
// inside it and returns true if so, otherwise false.
|
||||
nextCalled := ctx.Proceed(myMiddleware)
|
||||
if !nextCalled {
|
||||
return
|
||||
}
|
||||
|
||||
// else do the job here, it's allowed
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
And last, if you want to add a middleware on a specific method
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
/*Package main shows how to add done handlers in an MVC application without
|
||||
/*
|
||||
Package main shows how to add done handlers in an MVC application without
|
||||
the necessity of `ctx.Next()` inside the controller's methods.
|
||||
|
||||
When we want the `Done` handlers of that specific mvc app's `Party`
|
||||
to be executed but we don't want to add `ctx.Next()` on the `exampleController#EndRequest`*/
|
||||
to be executed but we don't want to add `ctx.Next()` on the `exampleController#EndRequest`
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
|
||||
@@ -28,42 +28,45 @@ func pong(ctx iris.Context) {
|
||||
}
|
||||
|
||||
/*
|
||||
+-------------------+
|
||||
| Env (DEV, PROD) |
|
||||
+---------+---------+
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
DEV | | | PROD
|
||||
+-------------------+
|
||||
| Env (DEV, PROD) |
|
||||
+---------+---------+
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
DEV | | | PROD
|
||||
|
||||
-------------------+---------------------+ | +----------------------+-------------------
|
||||
| | |
|
||||
| | |
|
||||
+---+-----+ +----------------v------------------+ +----+----+
|
||||
| sqlite | | NewDB(Env) DB | | mysql |
|
||||
+---+-----+ +----------------+---+--------------+ +----+----+
|
||||
| | | |
|
||||
| | | |
|
||||
| | | |
|
||||
+--------------+-----+ +-------------------v---v-----------------+ +----+------+
|
||||
| greeterWithLogging | | NewGreetService(Env, DB) GreetService | | greeter |
|
||||
+--------------+-----+ +---------------------------+-------------+ +----+------+
|
||||
| | |
|
||||
| | |
|
||||
| +-----------------------------------------+ |
|
||||
| | GreetController | | |
|
||||
| | | | |
|
||||
| | - Service GreetService <-- | |
|
||||
| | | |
|
||||
| +-------------------+---------------------+ |
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
| +-----------+-----------+ |
|
||||
| | HTTP Request | |
|
||||
| +-----------------------+ |
|
||||
| | /greet?name=kataras | |
|
||||
| +-----------+-----------+ |
|
||||
| | |
|
||||
|
||||
| | |
|
||||
| | |
|
||||
+---+-----+ +----------------v------------------+ +----+----+
|
||||
| sqlite | | NewDB(Env) DB | | mysql |
|
||||
+---+-----+ +----------------+---+--------------+ +----+----+
|
||||
| | | |
|
||||
| | | |
|
||||
| | | |
|
||||
+--------------+-----+ +-------------------v---v-----------------+ +----+------+
|
||||
| greeterWithLogging | | NewGreetService(Env, DB) GreetService | | greeter |
|
||||
+--------------+-----+ +---------------------------+-------------+ +----+------+
|
||||
| | |
|
||||
| | |
|
||||
| +-----------------------------------------+ |
|
||||
| | GreetController | | |
|
||||
| | | | |
|
||||
| | - Service GreetService <-- | |
|
||||
| | | |
|
||||
| +-------------------+---------------------+ |
|
||||
| | |
|
||||
| | |
|
||||
| | |
|
||||
| +-----------+-----------+ |
|
||||
| | HTTP Request | |
|
||||
| +-----------------------+ |
|
||||
| | /greet?name=kataras | |
|
||||
| +-----------+-----------+ |
|
||||
| | |
|
||||
|
||||
+------------------+--------+ +------------+------------+ +-------+------------------+
|
||||
| model.Response (JSON) | | Response (JSON, error) | | Bad Request |
|
||||
+---------------------------+ +-------------------------+ +--------------------------+
|
||||
|
||||
@@ -23,14 +23,16 @@ type MovieController struct {
|
||||
// curl -i http://localhost:8080/movies
|
||||
//
|
||||
// The correct way if you have sensitive data:
|
||||
// func (c *MovieController) Get() (results []viewmodels.Movie) {
|
||||
// data := c.Service.GetAll()
|
||||
//
|
||||
// for _, movie := range data {
|
||||
// results = append(results, viewmodels.Movie{movie})
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// func (c *MovieController) Get() (results []viewmodels.Movie) {
|
||||
// data := c.Service.GetAll()
|
||||
//
|
||||
// for _, movie := range data {
|
||||
// results = append(results, viewmodels.Movie{movie})
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// otherwise just return the datamodels.
|
||||
func (c *MovieController) Get() (results []datamodels.Movie) {
|
||||
return c.Service.GetAll()
|
||||
|
||||
@@ -18,7 +18,7 @@ func (srv *Server) buildRouter() {
|
||||
ServerName: srv.config.ServerName,
|
||||
Env: srv.config.Env,
|
||||
Developer: "kataras",
|
||||
TimeLocation: time.FixedZone("Greece/Athens", 10800),
|
||||
TimeLocation: time.FixedZone("Greece/Athens", 7200),
|
||||
}))
|
||||
|
||||
api := srv.Party("/api")
|
||||
|
||||
@@ -8,7 +8,7 @@ func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Post("/", logAllBody, logJSON, logFormValues, func(ctx iris.Context) {
|
||||
// body, err := ioutil.ReadAll(ctx.Request().Body) once or
|
||||
// body, err := io.ReadAll(ctx.Request().Body) once or
|
||||
body, err := ctx.GetBody() // as many times as you need.
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/xml"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
@@ -69,7 +69,7 @@ func TestContentNegotiation(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rawResponse, err := ioutil.ReadAll(zr)
|
||||
rawResponse, err := io.ReadAll(zr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -7,14 +7,15 @@ import (
|
||||
"github.com/kataras/iris/v12/core/router"
|
||||
)
|
||||
|
||||
/* A Router should contain all three of the following methods:
|
||||
- HandleRequest should handle the request based on the Context.
|
||||
HandleRequest(ctx iris.Context)
|
||||
- Build should builds the handler, it's being called on router's BuildRouter.
|
||||
Build(provider router.RoutesProvider) error
|
||||
- RouteExists reports whether a particular route exists.
|
||||
RouteExists(ctx iris.Context, method, path string) bool
|
||||
- FireErrorCode(ctx iris.Context) should handle the given ctx.GetStatusCode().
|
||||
/*
|
||||
A Router should contain all three of the following methods:
|
||||
- HandleRequest should handle the request based on the Context.
|
||||
HandleRequest(ctx iris.Context)
|
||||
- Build should builds the handler, it's being called on router's BuildRouter.
|
||||
Build(provider router.RoutesProvider) error
|
||||
- RouteExists reports whether a particular route exists.
|
||||
RouteExists(ctx iris.Context, method, path string) bool
|
||||
- FireErrorCode(ctx iris.Context) should handle the given ctx.GetStatusCode().
|
||||
|
||||
For a more detailed, complete and useful example
|
||||
you can take a look at the iris' router itself which is located at:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -29,7 +29,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
b, err := os.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
/*Package main is a simple example of the behavior change of the execution flow of the handlers,
|
||||
/*
|
||||
Package main is a simple example of the behavior change of the execution flow of the handlers,
|
||||
normally we need the `ctx.Next()` to call the next handler in a route's handler chain,
|
||||
but with the `ExecutionRules` we can change this default behavior.
|
||||
Please read below before continue.
|
||||
@@ -8,11 +9,11 @@ The `Party#SetExecutionRules` alters the execution flow of the route handlers.
|
||||
For example, if for some reason the desired result is the (done or all) handlers
|
||||
to be executed no matter what, even if no `ctx.Next()` is called in the previous handlers:
|
||||
|
||||
app.SetExecutionRules(iris.ExecutionRules {
|
||||
Begin: iris.ExecutionOptions{Force: true}, # begin handlers(.Use)
|
||||
Main: iris.ExecutionOptions{Force: true}, # main handler (.Handle/Get...)
|
||||
Done: iris.ExecutionOptions{Force: true}, # done handlers (.Done)
|
||||
})
|
||||
app.SetExecutionRules(iris.ExecutionRules {
|
||||
Begin: iris.ExecutionOptions{Force: true}, # begin handlers(.Use)
|
||||
Main: iris.ExecutionOptions{Force: true}, # main handler (.Handle/Get...)
|
||||
Done: iris.ExecutionOptions{Force: true}, # done handlers (.Done)
|
||||
})
|
||||
|
||||
Note that if `true` then the only remained way to "break" the handler chain
|
||||
is by calling the `ctx.StopExecution()` (now that `ctx.Next()` doesn't even matter).
|
||||
@@ -22,7 +23,6 @@ the same rules will be applied to that as well.
|
||||
|
||||
Reset of these rules to their defaults (before `Party#Handle`) can be done
|
||||
with `Party#SetExecutionRules(iris.ExecutionRules{})`.
|
||||
|
||||
*/
|
||||
package main
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -14,7 +13,7 @@ import (
|
||||
// The rest possible checks is up to you, take it as as an exercise!
|
||||
func TestURLShortener(t *testing.T) {
|
||||
// temp db file
|
||||
f, err := ioutil.TempFile("", "shortener")
|
||||
f, err := os.CreateTemp("", "shortener")
|
||||
if err != nil {
|
||||
t.Fatalf("creating temp file for database failed: %v", err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by go-bindata. (@generated) DO NOT EDIT.
|
||||
|
||||
//Package main generated by go-bindata.// sources:
|
||||
// Package main generated by go-bindata.// sources:
|
||||
// templates/layouts/layout.html
|
||||
// templates/layouts/mylayout.html
|
||||
// templates/page1.html
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -310,11 +309,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("notexist") would return an error
|
||||
@@ -371,7 +372,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -266,11 +265,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
|
||||
@@ -323,7 +324,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -332,11 +331,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
|
||||
@@ -394,7 +395,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -310,11 +309,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
|
||||
@@ -371,7 +372,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -266,11 +265,13 @@ var _bindata = map[string]func() (*asset, error){
|
||||
// directory embedded in the file by go-bindata.
|
||||
// For example if you run go-bindata on data/... and data contains the
|
||||
// following hierarchy:
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// data/
|
||||
// foo.txt
|
||||
// img/
|
||||
// a.png
|
||||
// b.png
|
||||
//
|
||||
// then AssetDir("data") would return []string{"foo.txt", "img"}
|
||||
// AssetDir("data/img") would return []string{"a.png", "b.png"}
|
||||
// AssetDir("foo.txt") and AssetDir("nonexistent") would return an error
|
||||
@@ -321,7 +322,7 @@ func RestoreAsset(dir, name string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
err = os.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
@@ -53,7 +52,7 @@ func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
|
||||
if !fi.ModTime().After(lastMod) {
|
||||
return nil, lastMod, nil
|
||||
}
|
||||
p, err := ioutil.ReadFile(filename)
|
||||
p, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fi.ModTime(), err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user