xiaochang 4 years ago
parent
commit
402d1fc8a7
5 changed files with 167 additions and 0 deletions
  1. 1 0
      README.md
  2. 83 0
      conf/conf.go
  3. 39 0
      config.conf
  4. 22 0
      main.go
  5. 22 0
      web/server.go

+ 1 - 0
README.md

@@ -1,2 +1,3 @@
 # cnlink
 
+参考:[gin](https://github.com/gin-gonic/gin) [shortme](https://github.com/andyxning/shortme)

+ 83 - 0
conf/conf.go

@@ -0,0 +1,83 @@
+package conf
+
+import (
+	"bytes"
+	"io/ioutil"
+	"log"
+	"os"
+	"runtime"
+	"github.com/BurntSushi/toml"
+)
+
+type sequenceDB struct {
+	DSN          string `toml:"dsn"`
+	MaxIdleConns int    `toml:"max_idle_conns"`
+	MaxOpenConns int    `toml:"max_open_conns"`
+}
+
+type http struct {
+	Port string `toml:"port"`
+}
+
+type shortDB struct {
+	ReadDSN      string `toml:"read_dsn"`
+	WriteDSN     string `toml:"write_dsn"`
+	MaxIdleConns int    `toml:"max_idle_conns"`
+	MaxOpenConns int    `toml:"max_open_conns"`
+}
+
+type common struct {
+	BlackShortURLs    []string `toml:"black_short_urls"`
+	BlackShortURLsMap map[string]bool
+	BaseString        string `toml:"base_string"`
+	BaseStringLength  uint64
+	DomainName        string `toml:"domain_name"`
+	Schema            string `toml:"schema"`
+}
+
+type config struct {
+	Http       http       `toml:"http"`
+	SequenceDB sequenceDB `toml:"sequence_db"`
+	ShortDB    shortDB    `toml:"short_db"`
+	Common     common     `toml:"common"`
+}
+
+var Conf config
+var Version string
+func MustParseConfig(configFile string) {
+	if fileInfo, err := os.Stat(configFile); err != nil {
+		if os.IsNotExist(err) {
+			log.Panicf("configuration file %v does not exist.", configFile)
+		} else {
+			log.Panicf("configuration file %v can not be stated. %v", configFile, err)
+		}
+	} else {
+		if fileInfo.IsDir() {
+			log.Panicf("%v is a directory name", configFile)
+		}
+	}
+
+	content, err := ioutil.ReadFile(configFile)
+	if err != nil {
+		log.Panicf("read configuration file error. %v", err)
+	}
+	content = bytes.TrimSpace(content)
+
+	err = toml.Unmarshal(content, &Conf)
+	if err != nil {
+		log.Panicf("unmarshal toml object error. %v", err)
+	}
+
+	// short url black list
+	Conf.Common.BlackShortURLsMap = make(map[string]bool)
+	for _, blackShortURL := range Conf.Common.BlackShortURLs {
+		Conf.Common.BlackShortURLsMap[blackShortURL] = true
+	}
+
+	// base string
+	Conf.Common.BaseStringLength = uint64(len(Conf.Common.BaseString))
+}
+func init() {
+	runtime.GOMAXPROCS(runtime.NumCPU())
+	log.SetFlags(log.LstdFlags | log.Lshortfile)
+}

+ 39 - 0
config.conf

@@ -0,0 +1,39 @@
+[http]
+# Listen address
+listen = "0.0.0.0:8080"
+
+[sequence_db]
+# Mysql sequence generator DSN
+dsn = "sequence:sequence@tcp(127.0.0.1:3306)/sequence"
+
+# Mysql connection pool max idle connection
+max_idle_conns = 4
+
+# Mysql connection pool max open connection
+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"
+
+# Mysql short service write db DSN
+write_dsn = "shortme_r:shortme_r@tcp(127.0.0.1:3306)/shortme"
+
+# Mysql connection pool max idle connection
+max_idle_conns = 8
+
+# Mysql connection pool max open connection
+max_open_conns = 8
+
+[common]
+# short urls that will be filtered to use
+black_short_urls = ["version","health","short","expand","css","js","fuck","stupid"]
+
+# Base string used to generate short url
+base_string = "Ds3K9ZNvWmHcakr1oPnxh4qpMEzAye8wX5IdJ2LFujUgtC07lOTb6GYBQViSfR"
+
+# 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"

+ 22 - 0
main.go

@@ -0,0 +1,22 @@
+package main
+import "github.com/gin-gonic/gin"
+func main() {
+	r := gin.Default()
+	r.GET("/", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": "home page",
+		})
+	})
+	r.GET("/create", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": "create page",
+		})
+	})
+	r.GET("/u:sid", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": c.Params.ByName("sid"),
+		})
+	})
+
+	r.Run()
+}

+ 22 - 0
web/server.go

@@ -0,0 +1,22 @@
+package web
+import "github.com/gin-gonic/gin"
+func server() {
+	r := gin.Default()
+	r.GET("/", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": "home page",
+		})
+	})
+	r.GET("/create", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": "create page",
+		})
+	})
+	r.GET("/u:sid", func(c *gin.Context) {
+		c.JSON(200, gin.H{
+			"message": c.Params.ByName("sid"),
+		})
+	})
+
+	r.Run()
+}