Browse Source

Add OVERRIDE_DB_PATH and OVERRIDE_REFERRER

master
Alfred 5 months ago
parent
commit
89d50a8e6d
2 changed files with 29 additions and 12 deletions
  1. 3
    1
      README.md
  2. 26
    11
      shortener.go

+ 3
- 1
README.md View File

@@ -20,4 +20,6 @@ And to run it:
20 20
 docker run -p 6660:6660 shortener
21 21
 ```
22 22
 
23
-It was done in 30 mins, so do not judge its results...
23
+It was done in 30 mins, so do not judge its results...
24
+
25
+**Note**: The shortener accepts to change the domain in which it will publish the urls and the data base path using OVERRIDE_DB_PATH and OVERRIDE_REFERRER environment variables.

+ 26
- 11
shortener.go View File

@@ -1,21 +1,31 @@
1 1
 package main
2 2
 
3
-import "github.com/gin-gonic/gin"
4
-import "database/sql"
5
-import "net/http"
6
-import "strings"
7
-import "math/rand"
8
-import "time"
9 3
 import (
4
+	"database/sql"
5
+	"math/rand"
6
+	"net/http"
7
+	"os"
8
+	"strings"
9
+	"time"
10
+
11
+	"github.com/gin-gonic/gin"
12
+
10 13
 	_ "github.com/mattn/go-sqlite3"
11 14
 )
12 15
 
13 16
 func main() {
14 17
 	rand.Seed(time.Now().UnixNano())
15
-	db, err := sql.Open("sqlite3", "/srv/data/database.db")
18
+	db_path := "/srv/data"
19
+	override_db_path := os.Getenv("OVERRIDE_DB_PATH")
20
+	if override_db_path != "" {
21
+		db_path = override_db_path
22
+	}
23
+
24
+	db, err := sql.Open("sqlite3", db_path+"/database.db")
16 25
 	if err != nil {
17 26
 		panic(err)
18 27
 	}
28
+
19 29
 	statement, err := db.Prepare("CREATE TABLE IF NOT EXISTS urls (identifier CHAR(50) PRIMARY KEY, url TEXT)")
20 30
 	if err != nil {
21 31
 		panic(err)
@@ -44,13 +54,13 @@ func main() {
44 54
 	})
45 55
 	engine.POST("", func(context *gin.Context) {
46 56
 		max := func(v1 int, v2 int) int {
47
-			if (v1 > v2) {
57
+			if v1 > v2 {
48 58
 				return v1
49 59
 			}
50 60
 			return v2
51 61
 		}
52 62
 
53
-		get_identifier := func (n int) string {
63
+		get_identifier := func(n int) string {
54 64
 			possible := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
55 65
 			b := make([]byte, n)
56 66
 			for i := range b {
@@ -68,7 +78,7 @@ func main() {
68 78
 		idx := max(idx1, max(idx2, idx3))
69 79
 		if len(url) < len("http://a.es") || !(url[:7] == "http://" || url[:8] == "https://") || idx > -1 {
70 80
 			context.Redirect(http.StatusFound, "")
71
-			return;
81
+			return
72 82
 		}
73 83
 
74 84
 		identifier := ""
@@ -89,7 +99,12 @@ func main() {
89 99
 				letters = letters + 1
90 100
 			}
91 101
 		}
92
-		context.Data(http.StatusOK, "application/json", []byte("{\"url\": \"" + context.Request.Header.Get("Referer") + identifier + "\"}"))
102
+		domain := context.Request.Header.Get("Referer")
103
+		override_referrer := os.Getenv("OVERRIDE_REFERER")
104
+		if override_referrer != "" {
105
+			domain = override_referrer
106
+		}
107
+		context.Data(http.StatusOK, "application/json", []byte("{\"url\": \""+domain+identifier+"\"}"))
93 108
 	})
94 109
 	engine.GET("/:identifier", func(context *gin.Context) {
95 110
 		identifier := context.Param("identifier")

Loading…
Cancel
Save