xiaochang 5 年 前
コミット
8d4aa26451
6 ファイル変更95 行追加22 行削除
  1. 2 1
      config.conf
  2. 4 19
      main.go
  3. 1 0
      views/layout/health.html
  4. 68 0
      web/api/generic.go
  5. 7 0
      web/api/shortUrl.go
  6. 13 2
      web/server.go

+ 2 - 1
config.conf

@@ -30,7 +30,8 @@ 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 = "Ds3K9ZNvWmHcakr1oPnxh4qpMEzAye8wX5IdJ2LFujUgtC07lOTb6GYBQViSfR"
+base_string = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 # Short url service domain name. This is used to filter short url loop.
 domain_name = "127.0.0.1:8080"

+ 4 - 19
main.go

@@ -1,22 +1,7 @@
 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()
+import "git.sfnt.net/sfnt/cnlink/web"
+
+func main() {
+	web.Start()
 }

+ 1 - 0
views/layout/health.html

@@ -0,0 +1 @@
+ok

+ 68 - 0
web/api/generic.go

@@ -0,0 +1,68 @@
+package api
+
+import (
+	"git.sfnt.net/sfnt/cnlink/conf"
+	"github.com/gin-gonic/gin"
+	"net/http"
+)
+type version struct {
+	Version string `json:"version"`
+}
+
+type errorResp struct {
+	Msg string `json:"msg"`
+}
+
+type shortReq struct {
+	LongURL string `json:"longURL"`
+}
+
+type shortResp struct {
+	ShortURL string `json:"shortURL"`
+}
+
+type expandReq struct {
+	ShortURL string `json:"shortURL"`
+}
+
+type expandResp struct {
+	LongURL string `json:"longURL"`
+}
+
+func CheckVersion(c *gin.Context) {
+	//versionInfo, _ := json.Marshal(version{Version: conf.Version})
+	c.JSON(200, gin.H{
+		"message": conf.Version,
+	})
+
+}
+
+func CheckHealth(c *gin.Context) {
+	//tpl := template.New("health.html")
+	/*
+	var err error
+	tpl, err = tpl.ParseFiles("views/health.html")
+	if err != nil {
+		log.Printf("parse views error. %v", err)
+		//w.WriteHeader(http.StatusInternalServerError)
+		c.JSON(http.StatusInternalServerError, gin.H{
+			"message": http.StatusText(http.StatusInternalServerError),
+		})
+		//w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
+		return
+	}
+	*/
+	c.HTML(http.StatusOK, "health.html",nil)
+	/*
+	err = tpl.Execute(c.Writer, nil)
+	if err != nil {
+		log.Printf("execute views error. %v", err)
+		c.JSON(http.StatusInternalServerError, gin.H{
+			"message": http.StatusText(http.StatusInternalServerError),
+		})
+		//w.WriteHeader(http.StatusInternalServerError)
+		//w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
+		return
+	}
+	*/
+}

+ 7 - 0
web/api/shortUrl.go

@@ -0,0 +1,7 @@
+package api
+
+import "github.com/gin-gonic/gin"
+
+func redirect(c *gin.Context){
+
+}

+ 13 - 2
web/server.go

@@ -1,21 +1,32 @@
 package web
-import "github.com/gin-gonic/gin"
-func server() {
+
+import (
+	"git.sfnt.net/sfnt/cnlink/web/api"
+	"github.com/gin-gonic/gin"
+)
+func Start() {
 	r := gin.Default()
+	//r.LoadHTMLGlob("views/*")
+	r.LoadHTMLGlob("views/**/*")
 	r.GET("/", func(c *gin.Context) {
 		c.JSON(200, gin.H{
 			"message": "home page",
 		})
 	})
+	r.GET("/ver", api.CheckVersion)
+	r.GET("/health", api.CheckHealth)
+	/*
 	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"),
 		})
+		//c.Redirect(http.StatusFound,"")
 	})
 
 	r.Run()