加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[bigdata-088] go+ubuntu 一个最简单的api返回json格式数据的web

发布时间:2020-12-14 03:09:37 所属栏目:大数据 来源:网络整理
导读:1. 参考代码 https://github.com/beego/samples/tree/master/shorturl 2. 目录结构 ├── conf │ ? └── app.conf ├── controllers │ ? └── object.go ├── main.go └── models ? ? └── model.go 3. main.go内容如下: package mainimpor

1. 参考代码

https://github.com/beego/samples/tree/master/shorturl


2. 目录结构

├── conf
│ ? └── app.conf
├── controllers
│ ? └── object.go
├── main.go
└── models
? ? └── model.go


3. main.go内容如下:

package main

import (
	"beegoapidemo1/controllers"

	"github.com/astaxie/beego"
)

func main() {
	beego.Router("/v1/shorten",&controllers.ShortController{})
	beego.Run()
}

4. models/model.go内容如下:
package models

import (
	"crypto/md5"
	"fmt"
	"io"
)

var (
	globalnum int
)

func init() {
	globalnum = 100000000
}

func GetMD5(lurl string) string {
	h := md5.New()
	salt1 := "salt4shorturl"
	io.WriteString(h,lurl+salt1)
	urlmd5 := fmt.Sprintf("%x",h.Sum(nil))
	return urlmd5
}

func Generate() (tiny string) {
	globalnum++
	num := globalnum
	fmt.Println(num)
	alpha := merge(getRange(48,57),getRange(65,90))
	alpha = merge(alpha,getRange(97,122))
	if num < 62 {
		tiny = string(alpha[num])
		return tiny
	} else {
		var runes []rune
		runes = append(runes,alpha[num%62])
		num = num / 62
		for num >= 1 {
			if num < 62 {
				runes = append(runes,alpha[num-1])
			} else {
				runes = append(runes,alpha[num%62])
			}
			num = num / 62

		}
		for i,j := 0,len(runes)-1; i < j; i,j = i+1,j-1 {
			runes[i],runes[j] = runes[j],runes[i]
		}
		tiny = string(runes)
		return tiny
	}
	return tiny
}

func getRange(start,end rune) (ran []rune) {
	for i := start; i <= end; i++ {
		ran = append(ran,i)
	}
	return ran
}

func merge(a,b []rune) []rune {
	c := make([]rune,len(a)+len(b))
	copy(c,a)
	copy(c[len(a):],b)
	return c
}

5. controllers/object.go内容如下:

package controllers

import (
	"beegoapidemo1/models"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/cache"
)

var (
	urlcache cache.Cache
)

func init() {
	urlcache,_ = cache.NewCache("memory",`{"interval":0}`)
}

type ShortResult struct {
	UrlShort string
	UrlLong  string
}

type ShortController struct {
	beego.Controller
}

// Use Get rather than Post so that we can simulate easier in the browser
func (this *ShortController) Get() {
	var result ShortResult
	longurl := this.Input().Get("longurl")
	beego.Info(longurl)
	result.UrlLong = longurl
	urlmd5 := models.GetMD5(longurl)
	beego.Info(urlmd5)
	if urlcache.IsExist(urlmd5) {
		result.UrlShort = urlcache.Get(urlmd5).(string)
	} else {
		result.UrlShort = models.Generate()
		err := urlcache.Put(urlmd5,result.UrlShort,0)
		if err != nil {
			beego.Info(err)
		}
		err = urlcache.Put(result.UrlShort,longurl,0)
		if err != nil {
			beego.Info(err)
		}
	}
	this.Data["json"] = result
	this.ServeJSON()
}

6. conf/app.conf内容如下

appname = shorturl
httpport = 8080
runmode = dev
autorender = false
copyrequestbody = true


7. 编译后运行。


8. 在浏览器输入http://127.0.0.1:8080/v1/shorten/?longurl=http://google.com

返回值是json结构

{
  "UrlShort": "5laZF","UrlLong": "http://google.com"
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读