xiaochang vor 5 Jahren
Ursprung
Commit
6487ff6680
4 geänderte Dateien mit 76 neuen und 8 gelöschten Zeilen
  1. 6 6
      config.conf
  2. 45 0
      lib/tools.go
  3. 19 1
      main.go
  4. 6 1
      web/server.go

+ 6 - 6
config.conf

@@ -4,7 +4,7 @@ listen = "0.0.0.0:8080"
 
 [sequence_db]
 # Mysql sequence generator DSN
-dsn = "sequence:sequence@tcp(127.0.0.1:3306)/sequence"
+dsn = "root:rong360.com@tcp(10.12.20.143:3550)/cnlink"
 
 # Mysql connection pool max idle connection
 max_idle_conns = 4
@@ -14,10 +14,10 @@ max_open_conns = 4
 
 [short_db]
 # Mysql short service read db DSN
-read_dsn = "shortme_w:shortme_w@tcp(127.0.0.1:3306)/shortme"
+read_dsn = "root:rong360.com@tcp(10.12.20.143:3550)/cnlink"
 
 # Mysql short service write db DSN
-write_dsn = "shortme_r:shortme_r@tcp(127.0.0.1:3306)/shortme"
+write_dsn = "root:rong360.com@tcp(10.12.20.143:3550)/cnlink"
 
 # Mysql connection pool max idle connection
 max_idle_conns = 8
@@ -30,11 +30,11 @@ max_open_conns = 8
 black_short_urls = ["version","health","short","expand","css","js","fuck","stupid"]
 
 # Base string used to generate short url
-# base_string = "Ds3K9ZNvWmHcakr1oPnxh4qpMEzAye8wX5IdJ2LFujUgtC07lOTb6GYBQViSfR"
-base_string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
+base_string = "FnvUcfRyxsKuEMraDOPSe9Y8BZh0Tmk1bV65ld42XgGzNC7wp3JWLqAtiQjHoI"
+# base_string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 # Short url service domain name. This is used to filter short url loop.
 domain_name = "127.0.0.1:8080"
 
 # Short url service schema: http or https.
-schema = "http"
+schema = "https"

+ 45 - 0
lib/tools.go

@@ -0,0 +1,45 @@
+package lib
+
+import (
+	"math"
+	"strings"
+
+	"git.sfnt.net/sfnt/cnlink/conf"
+)
+
+// Int2String converts an unsigned 64bit integer to a string.
+func Int2String(seq uint64) (shortURL string) {
+	charSeq := []rune{}
+	if seq != 0 {
+		for seq != 0 {
+			mod := seq % conf.Conf.Common.BaseStringLength
+			div := seq / conf.Conf.Common.BaseStringLength
+			charSeq = append(charSeq, rune(conf.Conf.Common.BaseString[mod]))
+			seq = div
+		}
+	} else {
+		charSeq = append(charSeq, rune(conf.Conf.Common.BaseString[seq]))
+	}
+
+	tmpShortURL := string(charSeq)
+	shortURL = reverse(tmpShortURL)
+	return
+}
+
+// String2Int converts a short URL string to an unsigned 64bit integer.
+func String2Int(shortURL string) (seq uint64) {
+	shortURL = reverse(shortURL)
+	for index, char := range shortURL {
+		base := uint64(math.Pow(float64(conf.Conf.Common.BaseStringLength), float64(index)))
+		seq += uint64(strings.Index(conf.Conf.Common.BaseString, string(char))) * base
+	}
+	return
+}
+
+func reverse(s string) string {
+	r := []rune(s)
+	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
+		r[i], r[j] = r[j], r[i]
+	}
+	return string(r)
+}

+ 19 - 1
main.go

@@ -1,7 +1,25 @@
 package main
 
-import "git.sfnt.net/sfnt/cnlink/web"
+import (
+	"flag"
+	"fmt"
+	"git.sfnt.net/sfnt/cnlink/conf"
+	"git.sfnt.net/sfnt/cnlink/web"
+	"os"
+)
 
 func main() {
+	cfgFile := flag.String("c", "config.conf", "configuration file")
+	version := flag.Bool("v", false, "Version")
+
+	flag.Parse()
+
+	if *version {
+		fmt.Println(conf.Version)
+		os.Exit(0)
+	}
+
+	// parse config
+	conf.MustParseConfig(*cfgFile)
 	web.Start()
 }

+ 6 - 1
web/server.go

@@ -1,6 +1,7 @@
 package web
 
 import (
+	"git.sfnt.net/sfnt/cnlink/lib"
 	"git.sfnt.net/sfnt/cnlink/web/api"
 	"github.com/gin-gonic/gin"
 )
@@ -23,8 +24,12 @@ func Start() {
 	})
 	*/
 	r.GET("/u:sid", func(c *gin.Context) {
+		sid := c.Params.ByName("sid")
+		//intSid,_ := strconv.ParseUint(sid,10,64)
+
+
 		c.JSON(200, gin.H{
-			"message": c.Params.ByName("sid"),
+			"message": lib.String2Int(sid),
 		})
 		//c.Redirect(http.StatusFound,"")
 	})