mirror of https://github.com/itsmrval/ltsNinja
Compare commits
No commits in common. "main" and "0.1.0" have entirely different histories.
|
|
@ -1,2 +0,0 @@
|
||||||
*.html linguist-detectable=false
|
|
||||||
|
|
||||||
|
|
@ -9,8 +9,6 @@
|
||||||
*.dylib
|
*.dylib
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
build
|
|
||||||
|
|
||||||
database.db
|
database.db
|
||||||
.env
|
.env
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<br />
|
<br />
|
||||||
<div id="readme-top" align="center">
|
<div id="readme-top" align="center">
|
||||||
<a href="https://github.com/itsmrval/ltsninja">
|
<a href="https://github.com/itsmrval/ltsninja">
|
||||||
<img src="https://raw.githubusercontent.com/itsmrval/ltsNinja/main/src/static/img/logo.svg" alt="Logo" width="120" height="120">
|
<img src="https://raw.githubusercontent.com/itsmrval/ltsNinja/main/static/img/logo.svg" alt="Logo" width="120" height="120">
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
<h3 align="center">ltsNinja</h3>
|
<h3 align="center">ltsNinja</h3>
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
Simple and lightwell url shortener running GO.
|
Simple and lightwell url shortener running GO.
|
||||||
<br />
|
<br />
|
||||||
<br />
|
<br />
|
||||||
|
<a href="https://lts.ninja">Explore demo</a>
|
||||||
|
·
|
||||||
<a href="https://github.com/itsmrval/ltsninja/issues">Report Bug</a>
|
<a href="https://github.com/itsmrval/ltsninja/issues">Report Bug</a>
|
||||||
·
|
·
|
||||||
<a href="https://github.com/itsmrval/ltsninja/pulls">Pull request</a>
|
<a href="https://github.com/itsmrval/ltsninja/pulls">Pull request</a>
|
||||||
|
|
@ -81,7 +83,7 @@ Now let's see how to set up an ltsNinja instance.
|
||||||
```
|
```
|
||||||
2. Download the latest release and apply permissions
|
2. Download the latest release and apply permissions
|
||||||
```sh
|
```sh
|
||||||
wget -O ltsNinja https://github.com/itsmrval/ltsNinja/releases/download/0.1.1/ltsNinja_linux_amd64
|
wget -O ltsNinja https://github.com/itsmrval/ltsNinja/releases/download/0.1.0/ltsNinja_linux_amd64
|
||||||
chmod +x ltsNinja
|
chmod +x ltsNinja
|
||||||
```
|
```
|
||||||
3. Create the service on systemd
|
3. Create the service on systemd
|
||||||
|
|
@ -134,6 +136,6 @@ Now let's see how to set up an ltsNinja instance.
|
||||||
<!-- LICENSE -->
|
<!-- LICENSE -->
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Distributed under the MIT License. See `LICENSE` for more information.
|
Distributed under the MIT License. See `LICENSE.txt` for more information.
|
||||||
|
|
||||||
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
GITHUB_CLIENT_ID=""
|
||||||
|
GITHUB_CLIENT_SECRET=""
|
||||||
|
GITHUB_REDIRECT_URL=http://APP_URL/callback
|
||||||
|
DB_PATH=./database.db
|
||||||
|
PORT=8080
|
||||||
|
|
@ -2,16 +2,129 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/github"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed templates/*
|
||||||
|
var templatesFS embed.FS
|
||||||
|
|
||||||
|
//go:embed static/*
|
||||||
|
var staticFS embed.FS
|
||||||
|
|
||||||
|
type Link struct {
|
||||||
|
ID string
|
||||||
|
OriginalURL string
|
||||||
|
ShortURL string
|
||||||
|
UserID string
|
||||||
|
}
|
||||||
|
|
||||||
|
type App struct {
|
||||||
|
DB *sql.DB
|
||||||
|
GithubOAuthConfig *oauth2.Config
|
||||||
|
Router *gin.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func initialize() (*App, error) {
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
if err := godotenv.Load(); err != nil {
|
||||||
|
log.Println("No .env file found")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := initDB()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||||
|
}
|
||||||
|
oauthConfig := initGithubOAuth()
|
||||||
|
|
||||||
|
router := gin.Default()
|
||||||
|
tmpl := template.Must(template.ParseFS(templatesFS, "templates/*"))
|
||||||
|
router.SetHTMLTemplate(tmpl)
|
||||||
|
|
||||||
|
return &App{
|
||||||
|
DB: db,
|
||||||
|
GithubOAuthConfig: oauthConfig,
|
||||||
|
Router: router,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDB() (*sql.DB, error) {
|
||||||
|
dbPath := os.Getenv("DB_PATH")
|
||||||
|
if dbPath == "" {
|
||||||
|
return nil, fmt.Errorf("DB_PATH not set in environment")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = db.Ping(); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to ping database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func initGithubOAuth() *oauth2.Config {
|
||||||
|
return &oauth2.Config{
|
||||||
|
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
||||||
|
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
||||||
|
RedirectURL: os.Getenv("GITHUB_REDIRECT_URL"),
|
||||||
|
Scopes: []string{"user:email"},
|
||||||
|
Endpoint: github.Endpoint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) SetupRoutes() {
|
||||||
|
staticContent, _ := fs.Sub(staticFS, "static")
|
||||||
|
app.Router.StaticFS("/static", http.FS(staticContent))
|
||||||
|
app.Router.GET("/", app.homePage)
|
||||||
|
app.Router.POST("/", app.shortenURL)
|
||||||
|
app.Router.GET("/login", app.loginGithub)
|
||||||
|
app.Router.GET("/callback", app.githubCallback)
|
||||||
|
app.Router.GET("/logout", app.logout)
|
||||||
|
app.Router.GET("/dashboard", app.dashboard)
|
||||||
|
app.Router.DELETE("/dashboard", app.deleteLink)
|
||||||
|
app.Router.PUT("/dashboard", app.updateLink)
|
||||||
|
app.Router.GET("/:shortURL", app.redirectToOriginal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) Run() error {
|
||||||
|
port := os.Getenv("PORT")
|
||||||
|
log.Println("Server running on port :" + port)
|
||||||
|
return app.Router.Run(":" + port)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) createTable() error {
|
||||||
|
_, err := app.DB.Exec(`CREATE TABLE IF NOT EXISTS links (
|
||||||
|
id TEXT PRIMARY KEY NOT NULL UNIQUE,
|
||||||
|
original_url TEXT NOT NULL,
|
||||||
|
short_url TEXT NOT NULL UNIQUE,
|
||||||
|
user_id TEXT
|
||||||
|
)`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) logout(c *gin.Context) {
|
||||||
|
c.SetCookie("user_id", "", -1, "/", "", false, true)
|
||||||
|
c.Redirect(http.StatusFound, "/")
|
||||||
|
}
|
||||||
|
|
||||||
func (app *App) homePage(c *gin.Context) {
|
func (app *App) homePage(c *gin.Context) {
|
||||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||||
"loggedIn": isLoggedIn(c),
|
"loggedIn": isLoggedIn(c),
|
||||||
|
|
@ -22,14 +135,9 @@ func (app *App) shortenURL(c *gin.Context) {
|
||||||
originalURL := c.PostForm("url")
|
originalURL := c.PostForm("url")
|
||||||
customName := c.PostForm("custom_name")
|
customName := c.PostForm("custom_name")
|
||||||
|
|
||||||
if !isValidURL(originalURL) {
|
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid URL"})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
shortURL := customName
|
shortURL := customName
|
||||||
if shortURL == "" {
|
if shortURL == "" {
|
||||||
shortURL = generateShortURL()
|
shortURL = uuid.New().String()[:8]
|
||||||
}
|
}
|
||||||
|
|
||||||
userID := getUserID(c)
|
userID := getUserID(c)
|
||||||
|
|
@ -91,11 +199,6 @@ func (app *App) githubCallback(c *gin.Context) {
|
||||||
c.Redirect(http.StatusFound, "/")
|
c.Redirect(http.StatusFound, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) logout(c *gin.Context) {
|
|
||||||
c.SetCookie("user_id", "", -1, "/", "", false, true)
|
|
||||||
c.Redirect(http.StatusFound, "/")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) dashboard(c *gin.Context) {
|
func (app *App) dashboard(c *gin.Context) {
|
||||||
userID := getUserID(c)
|
userID := getUserID(c)
|
||||||
if userID == "" {
|
if userID == "" {
|
||||||
|
|
@ -103,11 +206,23 @@ func (app *App) dashboard(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
links, err := app.getUserLinks(userID)
|
rows, err := app.DB.Query("SELECT id, original_url, short_url FROM links WHERE user_id = ?", userID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
|
||||||
|
var links []Link
|
||||||
|
for rows.Next() {
|
||||||
|
var link Link
|
||||||
|
err := rows.Scan(&link.ID, &link.OriginalURL, &link.ShortURL)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
links = append(links, link)
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "dashboard.html", gin.H{"links": links, "userId": userID})
|
c.HTML(http.StatusOK, "dashboard.html", gin.H{"links": links, "userId": userID})
|
||||||
}
|
}
|
||||||
|
|
@ -175,3 +290,30 @@ func (app *App) redirectToOriginal(c *gin.Context) {
|
||||||
|
|
||||||
c.Redirect(http.StatusFound, originalURL)
|
c.Redirect(http.StatusFound, originalURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isLoggedIn(c *gin.Context) bool {
|
||||||
|
_, err := c.Cookie("user_id")
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserID(c *gin.Context) string {
|
||||||
|
userID, _ := c.Cookie("user_id")
|
||||||
|
return userID
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app, err := initialize()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to initialize app: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.createTable(); err != nil {
|
||||||
|
log.Fatalf("Failed to create table: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.SetupRoutes()
|
||||||
|
|
||||||
|
if err := app.Run(); err != nil {
|
||||||
|
log.Fatalf("Failed to run app: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
79
src/index.go
79
src/index.go
|
|
@ -1,79 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
"embed"
|
|
||||||
"fmt"
|
|
||||||
"html/template"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
"golang.org/x/oauth2/github"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:embed templates/*
|
|
||||||
var templatesFS embed.FS
|
|
||||||
|
|
||||||
//go:embed static/*
|
|
||||||
var staticFS embed.FS
|
|
||||||
|
|
||||||
func initialize() (*App, error) {
|
|
||||||
gin.SetMode(gin.ReleaseMode)
|
|
||||||
if err := godotenv.Load(); err != nil {
|
|
||||||
log.Println("No .env file found")
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := initDB()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
|
||||||
}
|
|
||||||
oauthConfig := initGithubOAuth()
|
|
||||||
|
|
||||||
router := gin.Default()
|
|
||||||
tmpl := template.Must(template.ParseFS(templatesFS, "templates/*"))
|
|
||||||
router.SetHTMLTemplate(tmpl)
|
|
||||||
|
|
||||||
return &App{
|
|
||||||
DB: db,
|
|
||||||
GithubOAuthConfig: oauthConfig,
|
|
||||||
Router: router,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func initDB() (*sql.DB, error) {
|
|
||||||
dbPath := os.Getenv("DB_PATH")
|
|
||||||
if dbPath == "" {
|
|
||||||
return nil, fmt.Errorf("DB_PATH not set in environment")
|
|
||||||
}
|
|
||||||
|
|
||||||
db, err := sql.Open("sqlite3", dbPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to open database: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = db.Ping(); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to ping database: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return db, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func initGithubOAuth() *oauth2.Config {
|
|
||||||
return &oauth2.Config{
|
|
||||||
ClientID: os.Getenv("GITHUB_CLIENT_ID"),
|
|
||||||
ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"),
|
|
||||||
RedirectURL: os.Getenv("GITHUB_REDIRECT_URL"),
|
|
||||||
Scopes: []string{"user:email"},
|
|
||||||
Endpoint: github.Endpoint,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) Run() error {
|
|
||||||
port := os.Getenv("PORT")
|
|
||||||
log.Println("Server running on port :" + port)
|
|
||||||
return app.Router.Run(":" + port)
|
|
||||||
}
|
|
||||||
22
src/main.go
22
src/main.go
|
|
@ -1,22 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
app, err := initialize()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to initialize app: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := app.createTable(); err != nil {
|
|
||||||
log.Fatalf("Failed to create table: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.SetupRoutes()
|
|
||||||
|
|
||||||
if err := app.Run(); err != nil {
|
|
||||||
log.Fatalf("Failed to run app: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"database/sql"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"golang.org/x/oauth2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type App struct {
|
|
||||||
DB *sql.DB
|
|
||||||
GithubOAuthConfig *oauth2.Config
|
|
||||||
Router *gin.Engine
|
|
||||||
}
|
|
||||||
|
|
||||||
type Link struct {
|
|
||||||
ID string
|
|
||||||
OriginalURL string
|
|
||||||
ShortURL string
|
|
||||||
UserID string
|
|
||||||
CreatedAt string
|
|
||||||
}
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (app *App) SetupRoutes() {
|
|
||||||
staticContent, _ := fs.Sub(staticFS, "static")
|
|
||||||
app.Router.StaticFS("/static", http.FS(staticContent))
|
|
||||||
|
|
||||||
app.Router.GET("/", app.homePage)
|
|
||||||
app.Router.POST("/", app.shortenURL)
|
|
||||||
|
|
||||||
app.Router.GET("/login", app.loginGithub)
|
|
||||||
app.Router.GET("/callback", app.githubCallback)
|
|
||||||
app.Router.GET("/logout", app.logout)
|
|
||||||
|
|
||||||
app.Router.GET("/dashboard", app.dashboard)
|
|
||||||
app.Router.DELETE("/dashboard", app.deleteLink)
|
|
||||||
app.Router.PUT("/dashboard", app.updateLink)
|
|
||||||
app.Router.GET("/:shortURL", app.redirectToOriginal)
|
|
||||||
|
|
||||||
}
|
|
||||||
67
src/utils.go
67
src/utils.go
|
|
@ -1,67 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"net/url"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
func isLoggedIn(c *gin.Context) bool {
|
|
||||||
_, err := c.Cookie("user_id")
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUserID(c *gin.Context) string {
|
|
||||||
userID, _ := c.Cookie("user_id")
|
|
||||||
return userID
|
|
||||||
}
|
|
||||||
|
|
||||||
func isValidURL(str string) bool {
|
|
||||||
u, err := url.Parse(str)
|
|
||||||
return err == nil && u.Scheme != "" && u.Host != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateShortURL() string {
|
|
||||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
||||||
const length = 8
|
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
result := make([]byte, length)
|
|
||||||
for i := range result {
|
|
||||||
result[i] = charset[rand.Intn(len(charset))]
|
|
||||||
}
|
|
||||||
return string(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) getUserLinks(userID string) ([]Link, error) {
|
|
||||||
rows, err := app.DB.Query("SELECT id, original_url, short_url FROM links WHERE user_id = ?", userID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
|
|
||||||
var links []Link
|
|
||||||
for rows.Next() {
|
|
||||||
var link Link
|
|
||||||
err := rows.Scan(&link.ID, &link.OriginalURL, &link.ShortURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
links = append(links, link)
|
|
||||||
}
|
|
||||||
|
|
||||||
return links, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (app *App) createTable() error {
|
|
||||||
_, err := app.DB.Exec(`CREATE TABLE IF NOT EXISTS links (
|
|
||||||
id TEXT PRIMARY KEY NOT NULL UNIQUE,
|
|
||||||
original_url TEXT NOT NULL,
|
|
||||||
short_url TEXT NOT NULL UNIQUE,
|
|
||||||
user_id TEXT,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)`)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
Before Width: | Height: | Size: 9.6 KiB After Width: | Height: | Size: 9.6 KiB |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Loading…
Reference in New Issue