Commit d1def6d1 authored by duanjinfei's avatar duanjinfei

init commit

parent 7da56320
Pipeline #653 canceled with stages
.idea
abigen
api-server
go.sum
nohup.out
appname = token-uri
httpport = 8888
runmode = dev
autorender = false
copyrequestbody = true
imageUri = "https://lightcycle.city/og.mp4"
\ No newline at end of file
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
)
type BaseController struct {
beego.Controller
}
func (d *BaseController) ResponseInfo(code int, msg interface{}, result interface{}) {
switch code {
case 500:
logs.Error(msg, result)
d.Data["json"] = map[string]interface{}{"code": "500", "msg": msg, "data": result}
case 200:
logs.Info(msg, result)
d.Data["json"] = map[string]interface{}{"code": "200", "msg": msg, "data": result}
}
d.ServeJSON()
}
package controllers
import (
"fmt"
"github.com/astaxie/beego"
"strconv"
"strings"
"token-uri/models"
)
type TokenController struct {
BaseController
}
func (c *TokenController) GetTokenUri() {
param := c.Ctx.Input.Param(":file")
if param == "" {
c.ResponseInfo(500, "Param invalid failed", nil)
return
}
split := strings.Split(param, ".")
if len(split) != 2 {
c.ResponseInfo(500, "Param invalid failed", nil)
return
}
tokenIdStr := split[0]
tokenIdInt64, err := strconv.ParseInt(tokenIdStr, 10, 64)
if err != nil {
c.ResponseInfo(500, "Param invalid failed", nil)
return
}
suffix := split[1]
if suffix != "json" {
c.ResponseInfo(500, "Param invalid failed", nil)
return
}
imageUri := beego.AppConfig.String("imageUri")
res := &models.TokenInfoRep{
Id: tokenIdInt64,
Name: fmt.Sprintf("LightCycle OG #%d", tokenIdInt64),
ImageUri: imageUri,
}
c.ResponseInfo(200, "Get token info success", res)
}
module token-uri
go 1.19
require github.com/astaxie/beego v1.12.1
require github.com/smartystreets/goconvey v1.6.4
require (
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02 // indirect
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
golang.org/x/text v0.3.0 // indirect
gopkg.in/yaml.v2 v2.2.1 // indirect
)
package main
import (
_ "token-uri/routers"
"github.com/astaxie/beego"
)
func main() {
beego.Run()
}
package models
type TokenInfoRep struct {
Id int64 `json:"id"`
Name string `json:"name"`
ImageUri string `json:"image"`
}
package routers
import (
"github.com/astaxie/beego"
"token-uri/controllers"
)
func init() {
beego.Router("/ipfs/og/:file", &controllers.TokenController{}, "get:GetTokenUri")
}
package test
import (
"net/http"
"net/http/httptest"
"testing"
"runtime"
"path/filepath"
_ "token-uri/routers"
"github.com/astaxie/beego"
. "github.com/smartystreets/goconvey/convey"
)
func init() {
_, file, _, _ := runtime.Caller(0)
apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
beego.TestBeegoInit(apppath)
}
// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
beego.BeeApp.Handlers.ServeHTTP(w, r)
beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())
Convey("Subject: Test Station Endpoint\n", t, func() {
Convey("Status Code Should Be 200", func() {
So(w.Code, ShouldEqual, 200)
})
Convey("The Result Should Not Be Empty", func() {
So(w.Body.Len(), ShouldBeGreaterThan, 0)
})
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment