Compare commits

...

8 Commits
0.1.0 ... main

Author SHA1 Message Date
Valentin f830345c82
no more domain 2025-08-15 19:05:42 +02:00
itsmrval 2490bab1cd fix(README) correct url 2024-08-02 15:28:20 +02:00
itsmrval 6f8a1977ee fix(README) correct logo 2024-08-02 15:25:11 +02:00
itsmrval 90e9be931f feat(struct) moving into src directory 2024-08-02 15:24:28 +02:00
itsmrval 93094f8d1f fix(readme) wrong license name 2024-08-02 15:19:54 +02:00
itsmrval a0df1ad22f del(example.env) removing useless file 2024-08-02 15:19:22 +02:00
itsmrval 0013f17d54 fix(gitignore) removed build 2024-08-02 15:18:57 +02:00
itsmrval 104d773716 feat(struct) better structure 2024-08-02 15:17:22 +02:00
16 changed files with 233 additions and 164 deletions

2
.gitattributes vendored Normal file
View File

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

2
.gitignore vendored
View File

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

View File

@ -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/static/img/logo.svg" alt="Logo" width="120" height="120"> <img src="https://raw.githubusercontent.com/itsmrval/ltsNinja/main/src/static/img/logo.svg" alt="Logo" width="120" height="120">
</a> </a>
<h3 align="center">ltsNinja</h3> <h3 align="center">ltsNinja</h3>
@ -10,8 +10,6 @@
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>
@ -83,7 +81,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.0/ltsNinja_linux_amd64 wget -O ltsNinja https://github.com/itsmrval/ltsNinja/releases/download/0.1.1/ltsNinja_linux_amd64
chmod +x ltsNinja chmod +x ltsNinja
``` ```
3. Create the service on systemd 3. Create the service on systemd
@ -136,6 +134,6 @@ Now let's see how to set up an ltsNinja instance.
<!-- LICENSE --> <!-- LICENSE -->
## License ## License
Distributed under the MIT License. See `LICENSE.txt` for more information. Distributed under the MIT License. See `LICENSE` 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>

View File

@ -1,5 +0,0 @@
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,129 +2,16 @@ 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),
@ -135,9 +22,14 @@ 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 = uuid.New().String()[:8] shortURL = generateShortURL()
} }
userID := getUserID(c) userID := getUserID(c)
@ -199,6 +91,11 @@ 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 == "" {
@ -206,23 +103,11 @@ func (app *App) dashboard(c *gin.Context) {
return return
} }
rows, err := app.DB.Query("SELECT id, original_url, short_url FROM links WHERE user_id = ?", userID) links, err := app.getUserLinks(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})
} }
@ -290,30 +175,3 @@ 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 Normal file
View File

@ -0,0 +1,79 @@
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 Normal file
View File

@ -0,0 +1,22 @@
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)
}
}

22
src/models.go Normal file
View File

@ -0,0 +1,22 @@
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
}

24
src/routes.go Normal file
View File

@ -0,0 +1,24 @@
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

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

67
src/utils.go Normal file
View File

@ -0,0 +1,67 @@
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
}