Compare commits

..

No commits in common. "main" and "0.1.0" have entirely different histories.
main ... 0.1.0

16 changed files with 164 additions and 233 deletions

2
.gitattributes vendored
View File

@ -1,2 +0,0 @@
*.html linguist-detectable=false

2
.gitignore vendored
View File

@ -9,8 +9,6 @@
*.dylib
.DS_Store
build
database.db
.env

View File

@ -1,7 +1,7 @@
<br />
<div id="readme-top" align="center">
<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>
<h3 align="center">ltsNinja</h3>
@ -10,6 +10,8 @@
Simple and lightwell url shortener running GO.
<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/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
```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
```
3. Create the service on systemd
@ -134,6 +136,6 @@ Now let's see how to set up an ltsNinja instance.
<!-- 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>

5
example.env Normal file
View File

@ -0,0 +1,5 @@
GITHUB_CLIENT_ID=""
GITHUB_CLIENT_SECRET=""
GITHUB_REDIRECT_URL=http://APP_URL/callback
DB_PATH=./database.db
PORT=8080

View File

View File

View File

@ -2,16 +2,129 @@ package main
import (
"database/sql"
"embed"
"encoding/json"
"fmt"
"html/template"
"io/fs"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"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) {
c.HTML(http.StatusOK, "index.html", gin.H{
"loggedIn": isLoggedIn(c),
@ -22,14 +135,9 @@ func (app *App) shortenURL(c *gin.Context) {
originalURL := c.PostForm("url")
customName := c.PostForm("custom_name")
if !isValidURL(originalURL) {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid URL"})
return
}
shortURL := customName
if shortURL == "" {
shortURL = generateShortURL()
shortURL = uuid.New().String()[:8]
}
userID := getUserID(c)
@ -91,11 +199,6 @@ func (app *App) githubCallback(c *gin.Context) {
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) {
userID := getUserID(c)
if userID == "" {
@ -103,11 +206,23 @@ func (app *App) dashboard(c *gin.Context) {
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 {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
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})
}
@ -175,3 +290,30 @@ func (app *App) redirectToOriginal(c *gin.Context) {
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)
}
}

View File

@ -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)
}

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}

View File

Before

Width:  |  Height:  |  Size: 9.6 KiB

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.4 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB