2017-06-11 22:07:50 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-28 00:58:56 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2020-04-27 14:48:09 +02:00
|
|
|
"github.com/kataras/iris/v12/middleware/logger"
|
2017-06-11 22:07:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2020-04-28 00:58:56 +02:00
|
|
|
// Set Logger level to "debug",
|
|
|
|
// see your terminal and the created file.
|
2020-04-26 16:51:27 +02:00
|
|
|
app.Logger().SetLevel("debug")
|
2017-06-11 22:07:50 +02:00
|
|
|
|
2020-04-28 00:58:56 +02:00
|
|
|
// Write logs to a file too.
|
|
|
|
f := newLogFile()
|
|
|
|
defer f.Close()
|
|
|
|
app.Logger().AddOutput(f)
|
|
|
|
|
2020-04-27 14:48:09 +02:00
|
|
|
// Register a request logger middleware to the application.
|
|
|
|
app.Use(logger.New())
|
|
|
|
|
2017-06-11 22:07:50 +02:00
|
|
|
// GET: http://localhost:8080
|
|
|
|
app.Get("/", info)
|
2017-06-12 17:23:35 +02:00
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
// GET: http://localhost:8080/profile/anyusername
|
2017-10-04 02:12:50 +02:00
|
|
|
//
|
|
|
|
// Want to use a custom regex expression instead?
|
|
|
|
// Easy: app.Get("/profile/{username:string regexp(^[a-zA-Z ]+$)}")
|
2017-06-11 22:07:50 +02:00
|
|
|
app.Get("/profile/{username:string}", info)
|
2017-10-04 02:12:50 +02:00
|
|
|
|
|
|
|
// If parameter type is missing then it's string which accepts anything,
|
|
|
|
// i.e: /{paramname} it's exactly the same as /{paramname:string}.
|
|
|
|
// The below is exactly the same as
|
|
|
|
// {username:string}
|
|
|
|
//
|
2017-07-10 17:32:42 +02:00
|
|
|
// GET: http://localhost:8080/profile/anyusername/backups/any/number/of/paths/here
|
2017-10-04 02:12:50 +02:00
|
|
|
app.Get("/profile/{username}/backups/{filepath:path}", info)
|
|
|
|
|
2017-06-11 22:07:50 +02:00
|
|
|
// Favicon
|
|
|
|
|
|
|
|
// GET: http://localhost:8080/favicon.ico
|
|
|
|
app.Favicon("./public/images/favicon.ico")
|
|
|
|
|
|
|
|
// Static assets
|
|
|
|
|
2020-04-26 16:23:38 +02:00
|
|
|
// GET: http://localhost:8080/assets/css/main.css
|
|
|
|
// maps to ./public/assets/css/main.css file at system location.
|
2020-07-24 12:03:49 +02:00
|
|
|
app.HandleDir("/assets", iris.Dir("./public/assets"))
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
/* OR
|
|
|
|
|
2020-04-26 16:23:38 +02:00
|
|
|
// GET: http://localhost:8080/css/main.css
|
|
|
|
// maps to ./public/assets/css/main.css file at system location.
|
2020-07-24 12:03:49 +02:00
|
|
|
app.HandleDir("/css", iris.Dir("./public/assets/css"))
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
// GET: http://localhost:8080/css/bootstrap.min.css
|
|
|
|
// maps to ./public/assets/css/bootstrap.min.css file at system location.
|
2020-07-24 12:03:49 +02:00
|
|
|
app.HandleDir("/css", iris.Dir("./public/assets/css"))
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Grouping
|
|
|
|
|
|
|
|
usersRoutes := app.Party("/users")
|
|
|
|
// GET: http://localhost:8080/users/help
|
2017-08-27 19:35:23 +02:00
|
|
|
usersRoutes.Get("/help", func(ctx iris.Context) {
|
2017-06-11 22:07:50 +02:00
|
|
|
ctx.Writef("GET / -- fetch all users\n")
|
|
|
|
ctx.Writef("GET /$ID -- fetch a user by id\n")
|
|
|
|
ctx.Writef("POST / -- create new user\n")
|
|
|
|
ctx.Writef("PUT /$ID -- update an existing user\n")
|
|
|
|
ctx.Writef("DELETE /$ID -- delete an existing user\n")
|
|
|
|
})
|
|
|
|
|
|
|
|
// GET: http://localhost:8080/users
|
2017-08-27 19:35:23 +02:00
|
|
|
usersRoutes.Get("/", func(ctx iris.Context) {
|
2017-06-11 22:07:50 +02:00
|
|
|
ctx.Writef("get all users")
|
|
|
|
})
|
|
|
|
|
|
|
|
// GET: http://localhost:8080/users/42
|
2017-07-10 17:32:42 +02:00
|
|
|
// **/users/42 and /users/help works after iris version 7.0.5**
|
2018-10-21 18:20:05 +02:00
|
|
|
usersRoutes.Get("/{id:uint64}", func(ctx iris.Context) {
|
|
|
|
id, _ := ctx.Params().GetUint64("id")
|
2017-06-11 22:07:50 +02:00
|
|
|
ctx.Writef("get user by id: %d", id)
|
|
|
|
})
|
|
|
|
|
|
|
|
// POST: http://localhost:8080/users
|
2017-08-27 19:35:23 +02:00
|
|
|
usersRoutes.Post("/", func(ctx iris.Context) {
|
2017-06-11 22:07:50 +02:00
|
|
|
username, password := ctx.PostValue("username"), ctx.PostValue("password")
|
|
|
|
ctx.Writef("create user for username= %s and password= %s", username, password)
|
|
|
|
})
|
|
|
|
|
|
|
|
// PUT: http://localhost:8080/users
|
2018-10-21 18:20:05 +02:00
|
|
|
usersRoutes.Put("/{id:uint64}", func(ctx iris.Context) {
|
|
|
|
id, _ := ctx.Params().GetUint64("id") // or .Get to get its string represatantion.
|
2017-06-11 22:07:50 +02:00
|
|
|
username := ctx.PostValue("username")
|
|
|
|
ctx.Writef("update user for id= %d and new username= %s", id, username)
|
|
|
|
})
|
|
|
|
|
|
|
|
// DELETE: http://localhost:8080/users/42
|
2018-10-21 18:20:05 +02:00
|
|
|
usersRoutes.Delete("/{id:uint64}", func(ctx iris.Context) {
|
|
|
|
id, _ := ctx.Params().GetUint64("id")
|
2017-06-11 22:07:50 +02:00
|
|
|
ctx.Writef("delete user by id: %d", id)
|
2020-04-28 21:34:36 +02:00
|
|
|
}).Describe("deletes a user")
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
// Subdomains, depends on the host, you have to edit the hosts or nginx/caddy's configuration if you use them.
|
|
|
|
//
|
2020-06-07 14:26:06 +02:00
|
|
|
// See more subdomains examples at _examples/routing/subdomains folder.
|
2017-06-11 22:07:50 +02:00
|
|
|
adminRoutes := app.Party("admin.")
|
|
|
|
|
|
|
|
// GET: http://admin.localhost:8080
|
|
|
|
adminRoutes.Get("/", info)
|
|
|
|
// GET: http://admin.localhost:8080/settings
|
|
|
|
adminRoutes.Get("/settings", info)
|
|
|
|
|
|
|
|
// Wildcard/dynamic subdomain
|
2021-01-06 00:52:39 +01:00
|
|
|
dynamicSubdomainRoutes := app.WildcardSubdomain()
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
// GET: http://any_thing_here.localhost:8080
|
|
|
|
dynamicSubdomainRoutes.Get("/", info)
|
|
|
|
|
2017-09-15 14:05:35 +02:00
|
|
|
app.Delete("/something", func(ctx iris.Context) {
|
|
|
|
name := ctx.URLParam("name")
|
|
|
|
ctx.Writef(name)
|
|
|
|
})
|
|
|
|
|
2020-04-27 14:48:09 +02:00
|
|
|
app.None("/secret", privateHandler)
|
|
|
|
app.Get("/public", execPrivateHandler)
|
|
|
|
|
2017-06-11 22:07:50 +02:00
|
|
|
// GET: http://localhost:8080/
|
2017-07-10 17:32:42 +02:00
|
|
|
// GET: http://localhost:8080/profile/anyusername
|
|
|
|
// GET: http://localhost:8080/profile/anyusername/backups/any/number/of/paths/here
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
// GET: http://localhost:8080/users/help
|
|
|
|
// GET: http://localhost:8080/users
|
|
|
|
// GET: http://localhost:8080/users/42
|
|
|
|
// POST: http://localhost:8080/users
|
|
|
|
// PUT: http://localhost:8080/users
|
|
|
|
// DELETE: http://localhost:8080/users/42
|
2017-09-15 14:05:35 +02:00
|
|
|
// DELETE: http://localhost:8080/something?name=iris
|
2017-06-11 22:07:50 +02:00
|
|
|
|
|
|
|
// GET: http://admin.localhost:8080
|
|
|
|
// GET: http://admin.localhost:8080/settings
|
|
|
|
// GET: http://any_thing_here.localhost:8080
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-06-11 22:07:50 +02:00
|
|
|
}
|
|
|
|
|
2020-04-27 14:48:09 +02:00
|
|
|
func privateHandler(ctx iris.Context) {
|
|
|
|
ctx.WriteString(`This can only be executed programmatically through server's another route:
|
|
|
|
ctx.Exec(iris.MethodNone, "/secret")`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execPrivateHandler(ctx iris.Context) {
|
|
|
|
ctx.Exec(iris.MethodNone, "/secret")
|
|
|
|
}
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
func info(ctx iris.Context) {
|
2017-06-11 22:07:50 +02:00
|
|
|
method := ctx.Method() // the http method requested a server's resource.
|
|
|
|
subdomain := ctx.Subdomain() // the subdomain, if any.
|
|
|
|
|
|
|
|
// the request path (without scheme and host).
|
|
|
|
path := ctx.Path()
|
|
|
|
// how to get all parameters, if we don't know
|
|
|
|
// the names:
|
|
|
|
paramsLen := ctx.Params().Len()
|
|
|
|
|
|
|
|
ctx.Params().Visit(func(name string, value string) {
|
|
|
|
ctx.Writef("%s = %s\n", name, value)
|
|
|
|
})
|
|
|
|
ctx.Writef("\nInfo\n\n")
|
|
|
|
ctx.Writef("Method: %s\nSubdomain: %s\nPath: %s\nParameters length: %d", method, subdomain, path, paramsLen)
|
|
|
|
}
|
2020-04-28 00:58:56 +02:00
|
|
|
|
|
|
|
// get a filename based on the date, file logs works that way the most times
|
|
|
|
// but these are just a sugar.
|
|
|
|
func todayFilename() string {
|
|
|
|
today := time.Now().Format("Jan 02 2006")
|
|
|
|
return today + ".txt"
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogFile() *os.File {
|
|
|
|
filename := todayFilename()
|
|
|
|
// open an output file, this will append to the today's file if server restarted.
|
|
|
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|