Skip to content

Commit faec133

Browse files
committed
Initial Commit
0 parents  commit faec133

File tree

7 files changed

+561
-0
lines changed

7 files changed

+561
-0
lines changed

.gitignore

Whitespace-only changes.

api.rest

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
POST http://localhost:8020/api
2+
Content-Type: application/json
3+
X-API-Key: abc
4+
5+
{
6+
"url": "https://google.com",
7+
"custom_id": "ini-google-nyet",
8+
"meta": {
9+
"title": "Google",
10+
"description": "the #1 search engine in the world",
11+
"image": "https://google.com/image"
12+
}
13+
}
14+
15+
### GET ALL
16+
17+
GET http://localhost:8020/api/a?page=1
18+
Content-Type: application/json
19+
X-API-Key: abc
20+
21+
22+
### DELETE BY ID
23+
24+
DELETE http://localhost:8020/api/a/6c9a1
25+
Content-Type: application/json
26+
X-API-Key: abc

func.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package main // Check if the custom ID exists in the database for the specified space
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"net/http"
9+
"strconv"
10+
"time"
11+
)
12+
13+
func idExists(id string) bool {
14+
var count int
15+
err := db.QueryRow(`
16+
SELECT COUNT(*) FROM short_urls WHERE id = ?
17+
`, id).Scan(&count)
18+
if err != nil {
19+
log.Println("Error checking if ID exists:", err)
20+
return true // Assume the ID exists to avoid using it
21+
}
22+
return count > 0
23+
}
24+
25+
// Generate a unique ID based on the current time and space
26+
func generateUniqueID(space string, url string) string {
27+
currentTimeNano := time.Now().UnixNano()
28+
// combine currentTime + url + space
29+
data := strconv.FormatInt(currentTimeNano, 10)+ url +space;
30+
hash := fmt.Sprintf("%x", md5.Sum([]byte(data)))
31+
32+
// get 5 last characters
33+
return hash[:5]
34+
}
35+
36+
func getHashedYear(year int) string {
37+
hashData := map[int]string{
38+
2023: "a",
39+
2024: "b",
40+
2025: "c",
41+
2026: "d",
42+
2027: "e",
43+
2028: "f",
44+
2029: "g",
45+
2030: "h",
46+
2031: "i",
47+
2032: "j",
48+
2033: "k",
49+
2034: "l",
50+
2035: "m",
51+
2036: "n",
52+
2037: "o",
53+
2038: "p",
54+
2039: "q",
55+
2040: "r",
56+
2041: "s",
57+
2042: "t",
58+
2043: "u",
59+
2044: "v",
60+
2045: "w",
61+
2046: "x",
62+
2047: "y",
63+
2048: "z",
64+
2049: "aa",
65+
2050: "ab",
66+
2051: "ac",
67+
2052: "ad",
68+
2053: "ae",
69+
2054: "af",
70+
2055: "ag",
71+
2056: "ah",
72+
2057: "ai",
73+
2058: "aj",
74+
2059: "ak",
75+
2060: "al",
76+
2061: "am",
77+
2062: "an",
78+
2063: "ao",
79+
2064: "ap",
80+
2065: "aq",
81+
2066: "ar",
82+
2067: "as",
83+
2068: "at",
84+
2069: "au",
85+
2070: "av",
86+
2071: "aw",
87+
2072: "ax",
88+
2073: "ay",
89+
2074: "az",
90+
2075: "ba",
91+
2076: "bb",
92+
2077: "bc",
93+
2078: "bd",
94+
2079: "be",
95+
2080: "bf",
96+
2081: "bg",
97+
2082: "bh",
98+
2083: "bi",
99+
2084: "bj",
100+
2085: "bk",
101+
2086: "bl",
102+
2087: "bm",
103+
2088: "bn",
104+
2089: "bo",
105+
2090: "bp",
106+
2091: "bq",
107+
2092: "br",
108+
2093: "bs",
109+
2094: "bt",
110+
2095: "bu",
111+
2096: "bv",
112+
2097: "bw",
113+
2098: "bx",
114+
2099: "by",
115+
2100: "bz",
116+
}
117+
118+
return hashData[year]
119+
}
120+
121+
// respondJSON sends a JSON response with the specified status code and data
122+
func respondJSON(w http.ResponseWriter, statusCode int, data interface{}) {
123+
responseJSON, err := json.Marshal(data)
124+
if err != nil {
125+
log.Println("Error encoding JSON response:", err)
126+
http.Error(w, "Failed to encode JSON response", http.StatusInternalServerError)
127+
return
128+
}
129+
130+
w.Header().Set("Content-Type", "application/json")
131+
w.WriteHeader(statusCode)
132+
w.Write(responseJSON)
133+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/daniwebdev/go-shortlink-sqlite
2+
3+
go 1.20
4+
5+
require (
6+
github.com/gorilla/mux v1.8.1 // indirect
7+
github.com/mattn/go-sqlite3 v1.14.19 // indirect
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
2+
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
3+
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
4+
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

0 commit comments

Comments
 (0)