2017-07-11 18:09:08 +02:00
|
|
|
// Package main shows how you can create a simple URL Shortener.
|
|
|
|
//
|
|
|
|
// Article: https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7
|
2017-03-14 13:54:04 +01:00
|
|
|
//
|
2020-04-28 04:51:28 +02:00
|
|
|
// $ go get go.etcd.io/bbolt/...
|
2020-05-28 15:20:58 +02:00
|
|
|
// $ go get github.com/google/uuid
|
2020-06-07 14:26:06 +02:00
|
|
|
// $ cd $GOPATH/src/github.com/kataras/iris/_examples/url-shortener
|
2017-07-11 18:09:08 +02:00
|
|
|
// $ go build
|
|
|
|
// $ ./url-shortener
|
2017-03-13 14:16:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2017-06-04 20:24:05 +02:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
)
|
2017-03-14 13:54:04 +01:00
|
|
|
|
2017-03-13 14:16:12 +01:00
|
|
|
func main() {
|
2017-07-10 17:32:42 +02:00
|
|
|
// assign a variable to the DB so we can use its features later.
|
2017-03-14 13:54:04 +01:00
|
|
|
db := NewDB("shortener.db")
|
2017-07-10 17:32:42 +02:00
|
|
|
// Pass that db to our app, in order to be able to test the whole app with a different database later on.
|
|
|
|
app := newApp(db)
|
|
|
|
|
|
|
|
// release the "db" connection when server goes off.
|
|
|
|
iris.RegisterOnInterrupt(db.Close)
|
|
|
|
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-07-10 17:32:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newApp(db *DB) *iris.Application {
|
|
|
|
app := iris.Default() // or app := iris.New()
|
|
|
|
|
|
|
|
// create our factory, which is the manager for the object creation.
|
|
|
|
// between our web app and the db.
|
2017-03-14 13:54:04 +01:00
|
|
|
factory := NewFactory(DefaultGenerator, db)
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
// serve the "./templates" directory's "*.html" files with the HTML std view engine.
|
2017-07-11 18:09:08 +02:00
|
|
|
tmpl := iris.HTML("./templates", ".html").Reload(true)
|
2017-07-10 17:32:42 +02:00
|
|
|
// register any template func(s) here.
|
2017-03-14 13:54:04 +01:00
|
|
|
//
|
2017-07-10 17:32:42 +02:00
|
|
|
// Look ./templates/index.html#L16
|
|
|
|
tmpl.AddFunc("IsPositive", func(n int) bool {
|
2017-03-14 13:54:04 +01:00
|
|
|
if n > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
})
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
app.RegisterView(tmpl)
|
2017-03-14 13:54:04 +01:00
|
|
|
|
2017-03-13 14:16:12 +01:00
|
|
|
// Serve static files (css)
|
2020-07-24 12:03:49 +02:00
|
|
|
app.HandleDir("/static", iris.Dir("./resources"))
|
2017-03-13 14:16:12 +01:00
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
indexHandler := func(ctx iris.Context) {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.ViewData("URL_COUNT", db.Len())
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.View("index.html")
|
2017-07-10 17:32:42 +02:00
|
|
|
}
|
|
|
|
app.Get("/", indexHandler)
|
2017-03-13 14:16:12 +01:00
|
|
|
|
|
|
|
// find and execute a short url by its key
|
2017-03-14 13:54:04 +01:00
|
|
|
// used on http://localhost:8080/u/dsaoj41u321dsa
|
2017-08-27 19:35:23 +02:00
|
|
|
execShortURL := func(ctx iris.Context, key string) {
|
2017-03-13 14:16:12 +01:00
|
|
|
if key == "" {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.StatusCode(iris.StatusBadRequest)
|
2017-03-13 14:16:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-14 13:54:04 +01:00
|
|
|
value := db.Get(key)
|
|
|
|
if value == "" {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.StatusCode(iris.StatusNotFound)
|
2017-03-13 14:16:12 +01:00
|
|
|
ctx.Writef("Short URL for key: '%s' not found", key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(value, iris.StatusTemporaryRedirect)
|
|
|
|
}
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/u/{shortkey}", func(ctx iris.Context) {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
execShortURL(ctx, ctx.Params().Get("shortkey"))
|
2017-03-13 14:16:12 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Post("/shorten", func(ctx iris.Context) {
|
2017-03-14 13:54:04 +01:00
|
|
|
formValue := ctx.FormValue("url")
|
|
|
|
if formValue == "" {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.ViewData("FORM_RESULT", "You need to a enter a URL")
|
|
|
|
ctx.StatusCode(iris.StatusLengthRequired)
|
2017-03-13 14:16:12 +01:00
|
|
|
} else {
|
2017-03-14 13:54:04 +01:00
|
|
|
key, err := factory.Gen(formValue)
|
2017-03-13 14:16:12 +01:00
|
|
|
if err != nil {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.ViewData("FORM_RESULT", "Invalid URL")
|
|
|
|
ctx.StatusCode(iris.StatusBadRequest)
|
2017-03-13 14:16:12 +01:00
|
|
|
} else {
|
2017-03-14 13:54:04 +01:00
|
|
|
if err = db.Set(key, formValue); err != nil {
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.ViewData("FORM_RESULT", "Internal error while saving the URL")
|
|
|
|
app.Logger().Infof("while saving URL: " + err.Error())
|
|
|
|
ctx.StatusCode(iris.StatusInternalServerError)
|
2017-03-14 13:54:04 +01:00
|
|
|
} else {
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
ctx.StatusCode(iris.StatusOK)
|
|
|
|
shortenURL := "http://" + app.ConfigurationReadOnly().GetVHost() + "/u/" + key
|
2017-07-10 17:32:42 +02:00
|
|
|
ctx.ViewData("FORM_RESULT",
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
template.HTML("<pre><a target='_new' href='"+shortenURL+"'>"+shortenURL+" </a></pre>"))
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
2017-03-14 13:54:04 +01:00
|
|
|
}
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|
2017-07-10 17:32:42 +02:00
|
|
|
|
|
|
|
indexHandler(ctx) // no redirect, we need the FORM_RESULT.
|
2017-03-13 14:16:12 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Post("/clear_cache", func(ctx iris.Context) {
|
2017-07-10 17:32:42 +02:00
|
|
|
db.Clear()
|
|
|
|
ctx.Redirect("/")
|
|
|
|
})
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
return app
|
2017-03-13 14:16:12 +01:00
|
|
|
}
|