mirror of
https://github.com/kataras/iris.git
synced 2026-07-31 08:29:50 +00:00
Update to 7.2.0. Read https://github.com/kataras/iris/blob/master/HISTORY.md#th-15-june-2017--v720
Fix https://github.com/iris-contrib/community-board/issues/12 . Read more: https://github.com/kataras/iris/blob/master/HISTORY.md#th-15-june-2017--v720 Former-commit-id: 398cb69fdc7e5367b147693371287ffb7b912feb
This commit is contained in:
+9
-4
@@ -25,6 +25,7 @@ It doesn't contains "best ways" neither explains all its features. It's just a s
|
||||
* [Basic](beginner/routing/basic/main.go)
|
||||
* [Dynamic Path](beginner/routing/dynamic-path/main.go)
|
||||
* [Reverse routing](beginner/routing/reverse/main.go)
|
||||
* [Custom wrapper](beginner/routing/custom-wrapper/main.go)
|
||||
* [Transform any third-party handler to iris-compatible handler](beginner/convert-handlers)
|
||||
* [From func(http.ResponseWriter, *http.Request, http.HandlerFunc)](beginner/convert-handlers/negroni-like/main.go)
|
||||
* [From http.Handler or http.HandlerFunc](beginner/convert-handlers/nethttp/main.go)
|
||||
@@ -34,7 +35,11 @@ It doesn't contains "best ways" neither explains all its features. It's just a s
|
||||
* [Read JSON](beginner/read-json/main.go)
|
||||
* [Read Form](beginner/read-form/main.go)
|
||||
* [Favicon](beginner/favicon/main.go)
|
||||
* [File Server](beginner/file-server/main.go)
|
||||
* [File Server](beginner/file-server)
|
||||
* [Basic](beginner/file-server/basic/main.go)
|
||||
* [Embedding Files Into App Executable File](beginner/file-server/embedding-files-into-app/main.go)
|
||||
* [Single Page Application](beginner/file-server/single-page-application/main.go)
|
||||
* [Embedding Single Page Application](beginner/file-server/embedding-single-page-application/main.go)
|
||||
* [Send Files](beginner/send-files/main.go)
|
||||
* [Stream Writer](beginner/stream-writer/main.go)
|
||||
* [Send An E-mail](beginner/e-mail/main.go)
|
||||
@@ -51,7 +56,6 @@ It doesn't contains "best ways" neither explains all its features. It's just a s
|
||||
* [HTTP Testing](intermediate/httptest/main_test.go)
|
||||
* [Watch & Compile Typescript source files](intermediate/typescript/main.go)
|
||||
* [Cloud Editor](intermediate/cloud-editor/main.go)
|
||||
* [Serve Embedded Files](intermediate/serve-embedded-files/main.go)
|
||||
* [HTTP Access Control](intermediate/cors/main.go)
|
||||
* [Cache Markdown](intermediate/cache-markdown/main.go)
|
||||
* [Localization and Internationalization](intermediate/i18n/main.go)
|
||||
@@ -86,13 +90,14 @@ It doesn't contains "best ways" neither explains all its features. It's just a s
|
||||
* [Ridiculous Simple](intermediate/websockets/ridiculous-simple/main.go)
|
||||
* [Overview](intermediate/websockets/overview/main.go)
|
||||
* [Connection List](intermediate/websockets/connectionlist/main.go)
|
||||
* [Native Messages](intermediate/websockets/naive-messages/main.go)
|
||||
* [Native Messages](intermediate/websockets/native-messages/main.go)
|
||||
* [Secure](intermediate/websockets/secure/main.go)
|
||||
* [Custom Go Client](intermediate/websockets/custom-go-client/main.go)
|
||||
* [Subdomains](intermediate/subdomains)
|
||||
* [Single](intermediate/subdomains/single/main.go)
|
||||
* [Multi](intermediate/subdomains/multi/main.go)
|
||||
* [Wildcard](intermediate/subdomains/wildcard/main.go)
|
||||
* [Wildcard](intermediate/subdomains/wildcard/main.go)
|
||||
* [WWW](intermediate/subdomains/www/main.go)
|
||||
* [Level: Advanced](advanced)
|
||||
* [Online Visitors](advanced/online-visitors/main.go)
|
||||
* [URL Shortener using BoltDB](advanced/url-shortener/main.go)
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Favicon("./assets/favicon.ico")
|
||||
|
||||
// first parameter is the request path
|
||||
// second is the system directory
|
||||
//
|
||||
// app.StaticWeb("/css", "./assets/css")
|
||||
// app.StaticWeb("/js", "./assets/js")
|
||||
//
|
||||
app.StaticWeb("/static", "./assets")
|
||||
|
||||
// http://localhost:8080/static/css/main.css
|
||||
// http://localhost:8080/static/js/jquery-2.1.1.js
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
// Note:
|
||||
// Routing doesn't allows something .StaticWeb("/", "./assets")
|
||||
//
|
||||
// To see how you can wrap the router in order to achieve
|
||||
// wildcard on root path, see "single-page-application".
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
+9190
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// Follow these steps first:
|
||||
// $ go get -u github.com/jteeuwen/go-bindata/...
|
||||
// $ go-bindata ./assets/...
|
||||
// $ go build
|
||||
// $ ./embedding-files-into-app
|
||||
// "physical" files are not used, you can delete the "assets" folder and run the example.
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.StaticEmbedded("/static", "./assets", Asset, AssetNames)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080/static/css/bootstrap.min.css
|
||||
// http://localhost:8080/static/js/jquery-2.1.1.js
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
func (r resource) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func (r resource) strip(strip string) string {
|
||||
s := r.String()
|
||||
return strings.TrimPrefix(s, strip)
|
||||
}
|
||||
|
||||
func (r resource) loadFromBase(dir string) string {
|
||||
filename := r.String()
|
||||
|
||||
filename = r.strip("/static")
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
var urls = []resource{
|
||||
"/static/css/bootstrap.min.css",
|
||||
"/static/js/jquery-2.1.1.js",
|
||||
"/static/favicon.ico",
|
||||
}
|
||||
|
||||
// if bindata's values matches with the assets/... contents
|
||||
// and secondly if the StaticEmbedded had successfully registered
|
||||
// the routes and gave the correct response.
|
||||
func TestEmbeddingFilesIntoApp(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
for _, u := range urls {
|
||||
url := u.String()
|
||||
contents := u.loadFromBase("./assets")
|
||||
|
||||
e.GET(url).Expect().
|
||||
Status(iris.StatusOK).
|
||||
Body().Equal(contents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
// Code generated by go-bindata.
|
||||
// sources:
|
||||
// public/app.js
|
||||
// public/css/main.css
|
||||
// public/index.html
|
||||
// DO NOT EDIT!
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func bindataRead(data []byte, name string) ([]byte, error) {
|
||||
gz, err := gzip.NewReader(bytes.NewBuffer(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, err = io.Copy(&buf, gz)
|
||||
clErr := gz.Close()
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Read %q: %v", name, err)
|
||||
}
|
||||
if clErr != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
bytes []byte
|
||||
info os.FileInfo
|
||||
}
|
||||
|
||||
type bindataFileInfo struct {
|
||||
name string
|
||||
size int64
|
||||
mode os.FileMode
|
||||
modTime time.Time
|
||||
}
|
||||
|
||||
func (fi bindataFileInfo) Name() string {
|
||||
return fi.name
|
||||
}
|
||||
func (fi bindataFileInfo) Size() int64 {
|
||||
return fi.size
|
||||
}
|
||||
func (fi bindataFileInfo) Mode() os.FileMode {
|
||||
return fi.mode
|
||||
}
|
||||
func (fi bindataFileInfo) ModTime() time.Time {
|
||||
return fi.modTime
|
||||
}
|
||||
func (fi bindataFileInfo) IsDir() bool {
|
||||
return false
|
||||
}
|
||||
func (fi bindataFileInfo) Sys() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
var _publicAppJs = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x2a\xcf\xcc\x4b\xc9\x2f\xd7\x4b\xcc\x49\x2d\x2a\xd1\x50\x4a\x2c\x28\xd0\xcb\x2a\x56\xc8\xc9\x4f\x4c\x49\x4d\x51\x48\x2b\xca\xcf\x55\x88\x51\xd2\x57\xd2\xb4\x06\x04\x00\x00\xff\xff\xa9\x06\xf7\xa3\x27\x00\x00\x00")
|
||||
|
||||
func publicAppJsBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_publicAppJs,
|
||||
"public/app.js",
|
||||
)
|
||||
}
|
||||
|
||||
func publicAppJs() (*asset, error) {
|
||||
bytes, err := publicAppJsBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "public/app.js", size: 39, mode: os.FileMode(438), modTime: time.Unix(1497458456, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _publicCssMainCss = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x4a\xca\x4f\xa9\x54\xa8\xe6\xe5\x52\x50\x50\x50\x48\x4a\x4c\xce\x4e\x2f\xca\x2f\xcd\x4b\xd1\x4d\xce\xcf\xc9\x2f\xb2\x52\x48\xca\x49\x4c\xce\xb6\xe6\xe5\xaa\xe5\xe5\x02\x04\x00\x00\xff\xff\x03\x25\x9c\x89\x29\x00\x00\x00")
|
||||
|
||||
func publicCssMainCssBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_publicCssMainCss,
|
||||
"public/css/main.css",
|
||||
)
|
||||
}
|
||||
|
||||
func publicCssMainCss() (*asset, error) {
|
||||
bytes, err := publicCssMainCssBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "public/css/main.css", size: 41, mode: os.FileMode(438), modTime: time.Unix(1497455997, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
var _publicIndexHtml = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x34\x8e\x41\x0e\xc2\x20\x10\x45\xf7\x24\xdc\xe1\xa7\x07\x80\x74\x3f\xb2\x76\xe9\xc2\x0b\x60\x41\xc1\x50\x21\xc0\x42\xd3\xf4\xee\x06\x4a\x97\x93\xf7\x66\xde\x90\xab\x6b\x50\x9c\x71\x46\xce\x6a\xa3\x38\x03\x00\xaa\xbe\x06\xab\xb6\x0d\xe2\xa6\x5f\x56\xdc\xdb\x88\x7d\x27\x79\x00\xce\x48\x0e\x9d\x33\x7a\x44\xf3\x3b\x17\xdd\xac\x70\xb5\x21\x44\x3c\x73\x5c\xe1\x3f\xc6\x7e\x45\x6b\x80\xa4\x9b\xbb\x3f\xcc\xb2\x64\x9f\x2a\x4a\x5e\x2e\x93\xd4\x29\x89\x77\x99\x14\x40\xf2\x00\xbd\x31\x2e\xf7\x5c\xfb\xf3\x1f\x00\x00\xff\xff\x25\xe9\x37\x57\xae\x00\x00\x00")
|
||||
|
||||
func publicIndexHtmlBytes() ([]byte, error) {
|
||||
return bindataRead(
|
||||
_publicIndexHtml,
|
||||
"public/index.html",
|
||||
)
|
||||
}
|
||||
|
||||
func publicIndexHtml() (*asset, error) {
|
||||
bytes, err := publicIndexHtmlBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
info := bindataFileInfo{name: "public/index.html", size: 174, mode: os.FileMode(438), modTime: time.Unix(1497460815, 0)}
|
||||
a := &asset{bytes: bytes, info: info}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// Asset loads and returns the asset for the given name.
|
||||
// It returns an error if the asset could not be found or
|
||||
// could not be loaded.
|
||||
func Asset(name string) ([]byte, error) {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
if f, ok := _bindata[cannonicalName]; ok {
|
||||
a, err := f()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Asset %s can't read by error: %v", name, err)
|
||||
}
|
||||
return a.bytes, nil
|
||||
}
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
|
||||
// MustAsset is like Asset but panics when Asset would return an error.
|
||||
// It simplifies safe initialization of global variables.
|
||||
func MustAsset(name string) []byte {
|
||||
a, err := Asset(name)
|
||||
if err != nil {
|
||||
panic("asset: Asset(" + name + "): " + err.Error())
|
||||
}
|
||||
|
||||
return a
|
||||
}
|
||||
|
||||
// AssetInfo loads and returns the asset info for the given name.
|
||||
// It returns an error if the asset could not be found or
|
||||
// could not be loaded.
|
||||
func AssetInfo(name string) (os.FileInfo, error) {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
if f, ok := _bindata[cannonicalName]; ok {
|
||||
a, err := f()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AssetInfo %s can't read by error: %v", name, err)
|
||||
}
|
||||
return a.info, nil
|
||||
}
|
||||
return nil, fmt.Errorf("AssetInfo %s not found", name)
|
||||
}
|
||||
|
||||
// AssetNames returns the names of the assets.
|
||||
func AssetNames() []string {
|
||||
names := make([]string, 0, len(_bindata))
|
||||
for name := range _bindata {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// _bindata is a table, holding each asset generator, mapped to its name.
|
||||
var _bindata = map[string]func() (*asset, error){
|
||||
"public/app.js": publicAppJs,
|
||||
"public/css/main.css": publicCssMainCss,
|
||||
"public/index.html": publicIndexHtml,
|
||||
}
|
||||
|
||||
// AssetDir returns the file names below a certain
|
||||
// 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
|
||||
// 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
|
||||
// AssetDir("") will return []string{"data"}.
|
||||
func AssetDir(name string) ([]string, error) {
|
||||
node := _bintree
|
||||
if len(name) != 0 {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
pathList := strings.Split(cannonicalName, "/")
|
||||
for _, p := range pathList {
|
||||
node = node.Children[p]
|
||||
if node == nil {
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
if node.Func != nil {
|
||||
return nil, fmt.Errorf("Asset %s not found", name)
|
||||
}
|
||||
rv := make([]string, 0, len(node.Children))
|
||||
for childName := range node.Children {
|
||||
rv = append(rv, childName)
|
||||
}
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
type bintree struct {
|
||||
Func func() (*asset, error)
|
||||
Children map[string]*bintree
|
||||
}
|
||||
var _bintree = &bintree{nil, map[string]*bintree{
|
||||
"public": &bintree{nil, map[string]*bintree{
|
||||
"app.js": &bintree{publicAppJs, map[string]*bintree{}},
|
||||
"css": &bintree{nil, map[string]*bintree{
|
||||
"main.css": &bintree{publicCssMainCss, map[string]*bintree{}},
|
||||
}},
|
||||
"index.html": &bintree{publicIndexHtml, map[string]*bintree{}},
|
||||
}},
|
||||
}}
|
||||
|
||||
// RestoreAsset restores an asset under the given directory
|
||||
func RestoreAsset(dir, name string) error {
|
||||
data, err := Asset(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
info, err := AssetInfo(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(_filePath(dir, filepath.Dir(name)), os.FileMode(0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RestoreAssets restores an asset under the given directory recursively
|
||||
func RestoreAssets(dir, name string) error {
|
||||
children, err := AssetDir(name)
|
||||
// File
|
||||
if err != nil {
|
||||
return RestoreAsset(dir, name)
|
||||
}
|
||||
// Dir
|
||||
for _, child := range children {
|
||||
err = RestoreAssets(dir, filepath.Join(name, child))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func _filePath(dir, name string) string {
|
||||
cannonicalName := strings.Replace(name, "\\", "/", -1)
|
||||
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
// $ go get -u github.com/jteeuwen/go-bindata/...
|
||||
// $ go-bindata ./public/...
|
||||
// $ go build
|
||||
// $ ./embedding-single-page-application
|
||||
|
||||
var page = struct {
|
||||
Title string
|
||||
}{"Welcome"}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.AttachView(view.HTML("./public", ".html").Binary(Asset, AssetNames))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ViewData("Page", page)
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
assetHandler := app.StaticEmbeddedHandler("./public", Asset, AssetNames)
|
||||
app.SPA(assetHandler)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/index.html
|
||||
// http://localhost:8080/app.js
|
||||
// http://localhost:8080/css/main.css
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// Note that app.Use/UseGlobal/Done will be executed
|
||||
// only to the registered routes like our index (app.Get("/", ..)).
|
||||
// The file server is clean, but you can still add middleware to that by wrapping its "assetHandler".
|
||||
//
|
||||
// With this method, unlike StaticWeb("/" , "./public") which is not working by-design anymore,
|
||||
// all custom http errors and all routes are working fine with a file server that is registered
|
||||
// to the root path of the server.
|
||||
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
func (r resource) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func (r resource) strip(strip string) string {
|
||||
s := r.String()
|
||||
return strings.TrimPrefix(s, strip)
|
||||
}
|
||||
|
||||
func (r resource) loadFromBase(dir string) string {
|
||||
filename := r.String()
|
||||
|
||||
if filename == "/" {
|
||||
filename = "/index.html"
|
||||
}
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
var urls = []resource{
|
||||
"/",
|
||||
"/index.html",
|
||||
"/app.js",
|
||||
"/css/main.css",
|
||||
}
|
||||
|
||||
func TestSPAEmbedded(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
for _, u := range urls {
|
||||
url := u.String()
|
||||
contents := u.loadFromBase("./public")
|
||||
contents = strings.Replace(contents, "{{ .Page.Title }}", page.Title, 1)
|
||||
|
||||
e.GET(url).Expect().
|
||||
Status(iris.StatusOK).
|
||||
Body().Equal(contents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
window.alert("app.js loaded from \"/");
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{ .Page.Title }}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1> Hello from index.html </h1>
|
||||
|
||||
|
||||
<script src="/app.js"> </script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,16 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// first parameter is the request path
|
||||
// second is the operating system directory
|
||||
app.StaticWeb("/static", "./assets")
|
||||
|
||||
// http://localhost:8080/static/css/main.css
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/view"
|
||||
)
|
||||
|
||||
// same as embedding-single-page-application but without go-bindata, the files are "physical" stored in the
|
||||
// current system directory.
|
||||
|
||||
var page = struct {
|
||||
Title string
|
||||
}{"Welcome"}
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.AttachView(view.HTML("./public", ".html"))
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ViewData("Page", page)
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
// or just serve index.html as it is:
|
||||
// app.Get("/", func(ctx context.Context) {
|
||||
// ctx.ServeFile("index.html", false)
|
||||
// })
|
||||
|
||||
assetHandler := app.StaticHandler("./public", false, false)
|
||||
app.SPA(assetHandler)
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/index.html
|
||||
// http://localhost:8080/app.js
|
||||
// http://localhost:8080/css/main.css
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
func (r resource) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func (r resource) strip(strip string) string {
|
||||
s := r.String()
|
||||
return strings.TrimPrefix(s, strip)
|
||||
}
|
||||
|
||||
func (r resource) loadFromBase(dir string) string {
|
||||
filename := r.String()
|
||||
|
||||
if filename == "/" {
|
||||
filename = "/index.html"
|
||||
}
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
var urls = []resource{
|
||||
"/",
|
||||
"/index.html",
|
||||
"/app.js",
|
||||
"/css/main.css",
|
||||
}
|
||||
|
||||
func TestSPA(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
for _, u := range urls {
|
||||
url := u.String()
|
||||
contents := u.loadFromBase("./public")
|
||||
contents = strings.Replace(contents, "{{ .Page.Title }}", page.Title, 1)
|
||||
|
||||
e.GET(url).Expect().
|
||||
Status(iris.StatusOK).
|
||||
Body().Equal(contents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
window.alert("app.js loaded from \"/");
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{ .Page.Title }}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1> Hello from index.html </h1>
|
||||
|
||||
|
||||
<script src="/app.js"> </script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,8 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/core/host"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -16,6 +20,12 @@ func main() {
|
||||
ctx.Writef("Hello from the SECURE server on path /mypath")
|
||||
})
|
||||
|
||||
// to start a new server listening at :80 and redirects
|
||||
// to the secure address, then:
|
||||
target, _ := url.Parse("https://127.0.1:443")
|
||||
go host.NewProxy("127.0.0.1:80", target).ListenAndServe()
|
||||
|
||||
// start the server (HTTPS) on port 443, this is a blocking func
|
||||
app.Run(iris.TLS("127.0.0.1:443", "mycert.cert", "mykey.key"))
|
||||
|
||||
}
|
||||
|
||||
@@ -168,3 +168,5 @@ func notFoundHandler(ctx context.Context) {
|
||||
// A path parameter name should contain only alphabetical letters, symbols, containing '_' and numbers are NOT allowed.
|
||||
// If route failed to be registered, the app will panic without any warnings
|
||||
// if you didn't catch the second return value(error) on .Handle/.Get....
|
||||
|
||||
// See "file-server/single-page-application" to see how another feature, "WrapRouter", works.
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
// In this example you'll just see one use case of .WrapRouter.
|
||||
// You can use the .WrapRouter to add custom logic when or when not the router should
|
||||
// be executed in order to execute the registered routes' handlers.
|
||||
//
|
||||
// To see how you can serve files on root "/" without a custom wrapper
|
||||
// just navigate to the "beginner/file-server/single-page-application" example.
|
||||
//
|
||||
// This is just for the proof of concept, you can skip this tutorial if it's too much for you.
|
||||
func newApp() *iris.Application {
|
||||
|
||||
app := iris.New()
|
||||
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx context.Context) {
|
||||
ctx.HTML("<b>Resource Not found</b>")
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ServeFile("./public/index.html", false)
|
||||
})
|
||||
|
||||
app.Get("/profile/{username}", func(ctx context.Context) {
|
||||
ctx.Writef("Hello %s", ctx.Params().Get("username"))
|
||||
})
|
||||
|
||||
// serve files from the root "/", if we used .StaticWeb it could override
|
||||
// all the routes because of the underline need of wildcard.
|
||||
// Here we will see how you can by-pass this behavior
|
||||
// by creating a new file server handler and
|
||||
// setting up a wrapper for the router(like a "low-level" middleware)
|
||||
// in order to manually check if we want to process with the router as normally
|
||||
// or execute the file server handler instead.
|
||||
|
||||
// use of the .StaticHandler
|
||||
// which is the same as StaticWeb but it doesn't
|
||||
// registers the route, it just returns the handler.
|
||||
fileServer := app.StaticHandler("./public", false, false)
|
||||
|
||||
// wrap the router with a native net/http handler.
|
||||
// if url does not contain any "." (i.e: .css, .js...)
|
||||
// (depends on the app , you may need to add more file-server exceptions),
|
||||
// then the handler will execute the router that is responsible for the
|
||||
// registered routes (look "/" and "/profile/{username}")
|
||||
// if not then it will serve the files based on the root "/" path.
|
||||
app.WrapRouter(func(w http.ResponseWriter, r *http.Request, router http.HandlerFunc) {
|
||||
path := r.URL.Path
|
||||
// Note that if path has suffix of "index.html" it will auto-permant redirect to the "/",
|
||||
// so our first handler will be executed instead.
|
||||
|
||||
if !strings.Contains(path, ".") { // if it's not a resource then continue to the router as normally.
|
||||
router(w, r)
|
||||
return
|
||||
}
|
||||
// acquire and release a context in order to use it to execute
|
||||
// our file server
|
||||
// remember: we use net/http.Handler because here we are in the "low-level", before the router itself.
|
||||
ctx := app.ContextPool.Acquire(w, r)
|
||||
fileServer(ctx)
|
||||
app.ContextPool.Release(ctx)
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/index.html
|
||||
// http://localhost:8080/app.js
|
||||
// http://localhost:8080/css/main.css
|
||||
// http://localhost:8080/profile/kataras
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
// Note: In this example we just saw one use case,
|
||||
// you may want to .WrapRouter or .Downgrade in order to bypass the iris' default router, i.e:
|
||||
// you can use that method to setup custom proxies too.
|
||||
//
|
||||
// If you just want to serve static files on other path than root
|
||||
// you can just use the StaticWeb, i.e:
|
||||
// .StaticWeb("/static", "./public")
|
||||
// ________________________________requestPath, systemPath
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
func (r resource) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func (r resource) strip(strip string) string {
|
||||
s := r.String()
|
||||
return strings.TrimPrefix(s, strip)
|
||||
}
|
||||
|
||||
func (r resource) loadFromBase(dir string) string {
|
||||
filename := r.String()
|
||||
|
||||
if filename == "/" {
|
||||
filename = "/index.html"
|
||||
}
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
||||
var urls = []resource{
|
||||
"/",
|
||||
"/index.html",
|
||||
"/app.js",
|
||||
"/css/main.css",
|
||||
}
|
||||
|
||||
func TestCustomWrapper(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
for _, u := range urls {
|
||||
url := u.String()
|
||||
contents := u.loadFromBase("./public")
|
||||
|
||||
e.GET(url).Expect().
|
||||
Status(iris.StatusOK).
|
||||
Body().Equal(contents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
window.alert("app.js loaded from \"/");
|
||||
@@ -0,0 +1,3 @@
|
||||
body {
|
||||
background-color: black;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{ .Page.Title }}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1> Hello from index.html </h1>
|
||||
|
||||
|
||||
<script src="/app.js"> </script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
// $ go test -v
|
||||
func TestNewApp(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(app, t)
|
||||
e := httptest.New(t, app)
|
||||
|
||||
// redirects to /admin without basic auth
|
||||
e.GET("/").Expect().Status(iris.StatusUnauthorized)
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 264 KiB |
File diff suppressed because one or more lines are too long
@@ -1,43 +0,0 @@
|
||||
// for templates + go-bindata checkout the '_examples\intermediate\view\embedding-templates-into-app' folder.
|
||||
package main
|
||||
|
||||
// First of all, execute: $ go get https://github.com/jteeuwen/go-bindata
|
||||
// Secondly, execute the command: cd $GOPATH/src/github.com/kataras/iris/_examples/intermediate/serve-embedded-files && go-bindata ./assets/...
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.HTML("<b> Hi from index</b>")
|
||||
})
|
||||
|
||||
// executing this go-bindata command creates a source file named 'bindata.go' which
|
||||
// gives you the Asset and AssetNames funcs which we will pass into .StaticAssets
|
||||
// for more viist: https://github.com/jteeuwen/go-bindata
|
||||
// Iris gives you a way to integrade these functions to your web app
|
||||
|
||||
// For the reason that you may use go-bindata to embed more than your assets,
|
||||
// you should pass the 'virtual directory path', for example here is the : "./assets"
|
||||
// and the request path, which these files will be served to,
|
||||
// you can set as "/assets" or "/static" which resulting on http://localhost:8080/static/*anyfile.*extension
|
||||
app.StaticEmbedded("/static", "./assets", Asset, AssetNames)
|
||||
|
||||
// that's all
|
||||
// this will serve the ./assets (embedded) files to the /static request path for example the favicon.ico will be served as :
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
// Methods: GET and HEAD
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// Navigate to:
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
// http://localhost:8080/static/js/jquery-2.1.1.js
|
||||
// http://localhost:8080/static/css/bootstrap.min.css
|
||||
|
||||
// Now, these files are are stored into inside your executable program, no need to keep it in the same location with your assets folder.
|
||||
@@ -0,0 +1,25 @@
|
||||
# Copyright (c) 1993-2009 Microsoft Corp.
|
||||
#
|
||||
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
|
||||
#
|
||||
# This file contains the mappings of IP addresses to host names. Each
|
||||
# entry should be kept on an individual line. The IP address should
|
||||
# be placed in the first column followed by the corresponding host name.
|
||||
# The IP address and the host name should be separated by at least one
|
||||
# space.
|
||||
#
|
||||
# Additionally, comments (such as these) may be inserted on individual
|
||||
# lines or following the machine name denoted by a '#' symbol.
|
||||
#
|
||||
# For example:
|
||||
#
|
||||
# 102.54.94.97 rhino.acme.com # source server
|
||||
# 38.25.63.10 x.acme.com # x client host
|
||||
|
||||
# localhost name resolution is handled within DNS itself.
|
||||
127.0.0.1 localhost
|
||||
::1 localhost
|
||||
#-IRIS-For development machine, you have to configure your dns also for online, search google how to do it if you don't know
|
||||
127.0.0.1 iris-go.com
|
||||
127.0.0.1 www.iris-go.com
|
||||
#-END IRIS-
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", info)
|
||||
app.Get("/about", info)
|
||||
app.Get("/contact", info)
|
||||
|
||||
usersAPI := app.Party("/api/users")
|
||||
{
|
||||
usersAPI.Get("/", info)
|
||||
usersAPI.Get("/{id:int}", info)
|
||||
|
||||
usersAPI.Post("/", info)
|
||||
|
||||
usersAPI.Put("/{id:int}", info)
|
||||
}
|
||||
|
||||
www := app.Party("www.")
|
||||
{
|
||||
// get all routes that are registered so far, including all "Parties":
|
||||
currentRoutes := app.GetRoutes()
|
||||
// register them to the www subdomain/vhost as well:
|
||||
for _, r := range currentRoutes {
|
||||
if _, err := www.Handle(r.Method, r.Path, r.Handlers...); err != nil {
|
||||
app.Log("%s for www. failed: %v", r.Path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
// http://iris-go.com
|
||||
// http://iris-go.com/about
|
||||
// http://iris-go.com/contact
|
||||
// http://iris-go.com/api/users
|
||||
// http://iris-go.com/api/users/42
|
||||
|
||||
// http://www.iris-go.com
|
||||
// http://www.iris-go.com/about
|
||||
// http://www.iris-go.com/contact
|
||||
// http://www.iris-go.com/api/users
|
||||
// http://www.iris-go.com/api/users/42
|
||||
if err := app.Run(iris.Addr("iris-go.com:80")); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func info(ctx context.Context) {
|
||||
method := ctx.Method()
|
||||
subdomain := ctx.Subdomain()
|
||||
path := ctx.Path()
|
||||
|
||||
ctx.Writef("\nInfo\n\n")
|
||||
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s", method, subdomain, path)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type testRoute struct {
|
||||
path string
|
||||
method string
|
||||
subdomain string
|
||||
}
|
||||
|
||||
func (r testRoute) response() string {
|
||||
msg := fmt.Sprintf("\nInfo\n\nMethod: %s\nSubdomain: %s\nPath: %s", r.method, r.subdomain, r.path)
|
||||
return msg
|
||||
}
|
||||
|
||||
func TestSubdomainWWW(t *testing.T) {
|
||||
app := newApp()
|
||||
|
||||
tests := []testRoute{
|
||||
// host
|
||||
{"/", "GET", ""},
|
||||
{"/about", "GET", ""},
|
||||
{"/contact", "GET", ""},
|
||||
{"/api/users", "GET", ""},
|
||||
{"/api/users/42", "GET", ""},
|
||||
{"/api/users", "POST", ""},
|
||||
{"/api/users/42", "PUT", ""},
|
||||
// www sub domain
|
||||
{"/", "GET", "www"},
|
||||
{"/about", "GET", "www"},
|
||||
{"/contact", "GET", "www"},
|
||||
{"/api/users", "GET", "www"},
|
||||
{"/api/users/42", "GET", "www"},
|
||||
{"/api/users", "POST", "www"},
|
||||
{"/api/users/42", "PUT", "www"},
|
||||
}
|
||||
|
||||
host := "localhost:1111"
|
||||
e := httptest.New(t, app, httptest.URL("http://"+host))
|
||||
|
||||
for _, test := range tests {
|
||||
|
||||
req := e.Request(test.method, test.path)
|
||||
if subdomain := test.subdomain; subdomain != "" {
|
||||
req.WithURL("http://" + subdomain + "." + host)
|
||||
}
|
||||
|
||||
req.Expect().
|
||||
Status(iris.StatusOK).
|
||||
Body().Equal(test.response())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user