├── .gitattributes ├── .github └── workflows │ └── go.yml ├── Database ├── Connect.go ├── Find.go └── Insert.go ├── README.md ├── Utils └── Token.go ├── go.mod ├── go.sum ├── main.go └── url-api.exe /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.github/workflows/go.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a golang project 2 | # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go 3 | 4 | name: Go 5 | 6 | on: 7 | push: 8 | branches: [ "main" ] 9 | pull_request: 10 | branches: [ "main" ] 11 | 12 | jobs: 13 | 14 | build: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v3 18 | 19 | - name: Set up Go 20 | uses: actions/setup-go@v4 21 | with: 22 | go-version: '1.20' 23 | 24 | - name: Build 25 | run: go build -v ./... 26 | 27 | - name: Test 28 | run: go test -v ./... 29 | -------------------------------------------------------------------------------- /Database/Connect.go: -------------------------------------------------------------------------------- 1 | package Database 2 | 3 | import ( 4 | "database/sql" 5 | 6 | _ "github.com/go-sql-driver/mysql" 7 | ) 8 | 9 | var db *sql.DB 10 | 11 | func init() { 12 | var err error 13 | db, err = sql.Open("mysql", "root@tcp(localhost:3306)/urlshort") 14 | if err != nil { 15 | panic(err.Error()) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Database/Find.go: -------------------------------------------------------------------------------- 1 | package Database 2 | 3 | import ( 4 | _ "github.com/go-sql-driver/mysql" 5 | ) 6 | 7 | type Tag struct { 8 | Link string `json:"link"` 9 | } 10 | 11 | func Find(token string) (*Tag, error) { 12 | query := "SELECT link FROM url WHERE token = ?" 13 | var tag Tag 14 | err := db.QueryRow(query, token).Scan(&tag.Link) 15 | if err != nil { 16 | return nil, err 17 | } 18 | 19 | return &tag, nil 20 | } 21 | -------------------------------------------------------------------------------- /Database/Insert.go: -------------------------------------------------------------------------------- 1 | package Database 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func Insert(link, token string) error { 8 | query := "INSERT INTO url (link, token) VALUES (?, ?)" 9 | result, err := db.Exec(query, link, token) 10 | if err != nil { 11 | return err 12 | } 13 | 14 | rowCount, err := result.RowsAffected() 15 | if err != nil { 16 | return err 17 | } 18 | 19 | fmt.Printf("%d Added!\n", rowCount) 20 | return nil 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 👋 Link Kısaltma Apisi (web) 2 | 3 | Golang ve MySql ile yazılmış bir link kısaltma sistemidir. 4 | 5 | ### 📥 Kurulum 6 | 7 | Kurulum için terminale şu kodları yazın : 8 | 9 | ```bash 10 | go get 11 | ``` 12 | 13 | ### 🧨 Başlatmak 14 | 15 | Komut satırına bunu yazın : 16 | 17 | ```bash 18 | go run . 19 | ``` 20 | 21 | ### ⚙️ Ayarlamalar 22 | Birşey ayarlamanıza gerek yok. 23 | 24 | Çalıştırdıktan sonra doya doya kullanın 💚 25 | 26 | ### 🗨️ Discord Hesabım 27 | 28 | [![Discord](https://lanyard.cnrad.dev/api/1085964318853566524)](https://discord.com/users/1085964318853566524) 29 | -------------------------------------------------------------------------------- /Utils/Token.go: -------------------------------------------------------------------------------- 1 | package Utils 2 | 3 | import ( 4 | "math/rand" 5 | "time" 6 | ) 7 | 8 | func Token(length int) string { 9 | rand.Seed(time.Now().UnixNano()) 10 | 11 | const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 12 | result := make([]byte, length) 13 | for i := range result { 14 | result[i] = letters[rand.Intn(len(letters))] 15 | } 16 | return string(result) 17 | } -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module url-api 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/go-sql-driver/mysql v1.7.1 7 | github.com/gofiber/fiber/v2 v2.49.2 8 | ) 9 | 10 | require ( 11 | github.com/andybalholm/brotli v1.0.5 // indirect 12 | github.com/google/uuid v1.3.1 // indirect 13 | github.com/klauspost/compress v1.16.7 // indirect 14 | github.com/mattn/go-colorable v0.1.13 // indirect 15 | github.com/mattn/go-isatty v0.0.19 // indirect 16 | github.com/mattn/go-runewidth v0.0.15 // indirect 17 | github.com/rivo/uniseg v0.2.0 // indirect 18 | github.com/valyala/bytebufferpool v1.0.0 // indirect 19 | github.com/valyala/fasthttp v1.49.0 // indirect 20 | github.com/valyala/tcplisten v1.0.0 // indirect 21 | golang.org/x/sys v0.12.0 // indirect 22 | ) 23 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= 2 | github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 3 | github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= 4 | github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= 5 | github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs= 6 | github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= 7 | github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= 8 | github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 9 | github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= 10 | github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= 11 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 12 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 13 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 14 | github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= 15 | github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 16 | github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= 17 | github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= 18 | github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= 19 | github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= 20 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 21 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 22 | github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE= 23 | github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA= 24 | github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= 25 | github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= 26 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 27 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 28 | golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= 29 | golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 30 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "url-api/Database" 7 | "url-api/Utils" 8 | 9 | "github.com/gofiber/fiber/v2" 10 | "github.com/gofiber/fiber/v2/middleware/cors" 11 | "github.com/gofiber/fiber/v2/middleware/logger" 12 | ) 13 | 14 | type Link struct { 15 | Url string `json:"url"` 16 | } 17 | 18 | func main() { 19 | app := fiber.New() 20 | 21 | app.Use(cors.New(cors.Config{ 22 | AllowOrigins: "http://localhost:5173", 23 | AllowHeaders: "Origin, Content-Type, Accept", 24 | AllowCredentials: true, 25 | })) 26 | 27 | app.Use(logger.New(logger.Config{ 28 | Format: "${ip}:${port} ${status} - ${method} ${path}\n", 29 | })) 30 | 31 | app.Get("/:tokens", func(c *fiber.Ctx) error { 32 | Token :=c.Params("tokens") 33 | Find, err := Database.Find(Token) 34 | if err != nil { 35 | log.Fatal(err) 36 | } 37 | 38 | fmt.Println(Find) 39 | return c.JSON(fiber.Map{ 40 | "data": Find, 41 | "status": "OK", 42 | }) 43 | }) 44 | 45 | app.Post("/addlink", func(c *fiber.Ctx) error { 46 | var link Link 47 | if err := c.BodyParser(&link); err != nil { 48 | fmt.Println("Error parsing request:", err) 49 | return err 50 | } 51 | 52 | url := link.Url 53 | T0oken := Utils.Token(4) 54 | Database.Insert(url, T0oken) 55 | 56 | return c.JSON(fiber.Map{ 57 | "token": T0oken, 58 | "url": url, 59 | "msg": "Başarıyla eklendi!", 60 | "short": "http://localhost:3000/" + T0oken, 61 | }) 62 | }) 63 | app.Listen(":3000") 64 | } 65 | -------------------------------------------------------------------------------- /url-api.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ewriq/url-shortenler-api/6a0e8cf8d0abe302e4fa70a500b953e9da9374a2/url-api.exe --------------------------------------------------------------------------------