diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 97dfb360..db06aafa 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -23,9 +23,12 @@ jobs: with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Set branch name to an output variable + id: set_branch + run: echo "BRANCH_NAME=${GITHUB_REF##*/}" >> $GITHUB_ENV - name: Build and push uses: docker/build-push-action@v5 with: push: true - tags: ${{ secrets.DOCKERHUB_USERNAME }}/goplus-community-${{ github.ref_name }}:latest \ No newline at end of file + tags: ${{ secrets.DOCKERHUB_USERNAME }}/goplus-community-${{ env.BRANCH_NAME }}:latest \ No newline at end of file diff --git a/cmd/gopcomm/community_yap.gox b/cmd/gopcomm/community_yap.gox index 11197bf8..d382178f 100644 --- a/cmd/gopcomm/community_yap.gox +++ b/cmd/gopcomm/community_yap.gox @@ -11,9 +11,9 @@ import ( "github.com/goplus/community/markdown" "github.com/goplus/community/translation" "github.com/casdoor/casdoor-go-sdk/casdoorsdk" - "go.uber.org/zap" language "golang.org/x/text/language" _ "github.com/joho/godotenv/autoload" + "github.com/qiniu/x/xlog" ) var ( @@ -28,11 +28,11 @@ const ( todo := context.TODO() endpoint := os.Getenv("GOP_COMMUNITY_ENDPOINT") domain := os.Getenv("GOP_COMMUNITY_DOMAIN") -logger, _ := zap.NewProduction() -defer logger.Sync() -zlog := logger.Sugar() +xLog := xlog.New("") -static "/" +// Modify / to /static +// Support 404 handle +static "/static" get "/success", ctx => { ctx.yap "2xx", {} @@ -46,6 +46,10 @@ get "/failed", ctx => { ctx.yap "5xx", {} } +get "/demo", ctx => { + ctx.yap "demo", {} +} + get "/p/:id", ctx => { // todo middleware // Get User Info @@ -54,7 +58,7 @@ get "/p/:id", ctx => { if err == nil { user, err = community.getUser(token.Value) if err != nil { - zlog.Error("get user error:", err) + xLog.Error("get user error:", err) } } @@ -87,7 +91,7 @@ get "/user/:id", ctx => { // Get current User Info by id userClaim, err := community.getUserClaim(id) if err != nil { - zlog.Error("get current user error:", err) + xLog.Error("get current user error:", err) } // todo middleware // get user by token @@ -96,7 +100,7 @@ get "/user/:id", ctx => { if err == nil { user, err = community.getUser(token.Value) if err != nil { - zlog.Error("get user error:", err) + xLog.Error("get user error:", err) } } // get article list published by uid @@ -124,7 +128,7 @@ get "/", ctx => { if err == nil { user, err = community.getUser(token.Value) if err != nil { - zlog.Error("get user error:", err) + xLog.Error("get user error:", err) } } @@ -172,7 +176,7 @@ get "/search", ctx => { if err == nil { user, err = community.getUser(token.Value) if err != nil { - zlog.Error("get user error:", err) + xLog.Error("get user error:", err) } } @@ -206,7 +210,7 @@ get "/edit/:id", ctx => { id := ctx.param("id") if id != "" { if editable, _ := community.canEditable(todo, uid, id); !editable { - zlog.Error("no permissions") + xLog.Error("no permissions") http.Redirect(ctx.ResponseWriter, ctx.Request, "/error", http.StatusTemporaryRedirect) } article, _ := community.article(todo, id) @@ -340,7 +344,7 @@ post "/upload", ctx => { ctx.ParseMultipartForm(10 << 20) if err != nil { - zlog.Error("upload file error:", filename) + xLog.Error("upload file error:", filename) ctx.JSON(500, err.Error()) return } @@ -348,7 +352,7 @@ post "/upload", ctx => { dst, err := os.Create(filename) if err != nil { - zlog.Error("create file error:", file) + xLog.Error("create file error:", file) ctx.JSON(500, err.Error()) return } @@ -357,7 +361,7 @@ post "/upload", ctx => { dst.Close() err = os.Remove(filename) if err != nil { - zlog.Error("delete file error:", filename) + xLog.Error("delete file error:", filename) return } }() @@ -365,13 +369,13 @@ post "/upload", ctx => { _, err = io.Copy(dst, file) if err != nil { - zlog.Error("copy file errer:", filename) + xLog.Error("copy file errer:", filename) ctx.JSON(500, err.Error()) return } bytes, err := os.ReadFile(filename) if err != nil { - zlog.Error("read file errer:", filename) + xLog.Error("read file errer:", filename) ctx.JSON(500, err.Error()) return } @@ -391,7 +395,7 @@ post "/upload", ctx => { } id,err:=community.SaveMedia(context.Background(), uid, bytes) if err!=nil { - zlog.Error("save file",err.Error()) + xLog.Error("save file",err.Error()) ctx.JSON(500, err.Error()) return } @@ -402,11 +406,28 @@ post "/upload", ctx => { get "/login", ctx => { // Get URL from query string - redirectURL := ctx.URL.Query().Get("redirect_url") + // redirectURL := ctx.URL.Query().Get("redirect_url") + // Get current request page URL from + // Concatenate the current request page URL from refer + redirectURL := fmt.Sprintf("%s/%s", ctx.Request.Referer() + "callback") + loginURL := community.RedirectToCasdoor(redirectURL) ctx.Redirect loginURL, http.StatusFound } +get "/logout", ctx => { + tokenCookie, err := ctx.Request.Cookie("token") + if err != nil { + xLog.Error("get token error:", err) + } + + // Delete token + tokenCookie.MaxAge = -1 + http.SetCookie(ctx.ResponseWriter, tokenCookie) + + // Redirect to home page + http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080"), http.StatusFound) +} get "/callback", ctx => { code :=ctx.URL.Query().Get("code") @@ -414,7 +435,7 @@ get "/callback", ctx => { token, error := community.GetAccessToken(code, state) if error != nil { - zlog.Error("err",error) // Redirect to login + xLog.Error("err",error) // Redirect to login } cookie := http.Cookie{ @@ -427,7 +448,7 @@ get "/callback", ctx => { // Redirect to home page // TODO: Get redirect URL from state - http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080?token=%s", token.AccessToken), http.StatusFound) + http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080"), http.StatusFound) } conf := &core.Config{} @@ -436,11 +457,11 @@ trans = translation.New(os.Getenv("NIUTRANS_API_KEY"), "", "") core.CasdoorConfigInit() // 404 -// handle "/",ctx => { -// ctx.yap "4xx", {} -// } +handle "/",ctx => { + ctx.yap "4xx", {} +} -zlog.Info "Started in endpoint: ", endpoint +xLog.Info "Started in endpoint: ", endpoint // 500 run(endpoint, func(h http.Handler) http.Handler { diff --git a/cmd/gopcomm/gop_autogen.go b/cmd/gopcomm/gop_autogen.go index 0d9838c3..06e553a0 100644 --- a/cmd/gopcomm/gop_autogen.go +++ b/cmd/gopcomm/gop_autogen.go @@ -10,9 +10,9 @@ import ( "net/http" "github.com/goplus/community/internal/core" "github.com/goplus/community/translation" - "go.uber.org/zap" "golang.org/x/text/language" _ "github.com/joho/godotenv/autoload" + "github.com/qiniu/x/xlog" ) const layoutUS = "January 2, 2006" @@ -31,13 +31,9 @@ func (this *community) MainEntry() { //line cmd/gopcomm/community_yap.gox:30:1 domain := os.Getenv("GOP_COMMUNITY_DOMAIN") //line cmd/gopcomm/community_yap.gox:31:1 - logger, _ := zap.NewProduction() -//line cmd/gopcomm/community_yap.gox:32:1 - defer logger.Sync() -//line cmd/gopcomm/community_yap.gox:33:1 - zlog := logger.Sugar() + xLog := xlog.New("") //line cmd/gopcomm/community_yap.gox:35:1 - this.Static__0("/") + this.Static__0("/static") //line cmd/gopcomm/community_yap.gox:37:1 this.Get("/success", func(ctx *yap.Context) { //line cmd/gopcomm/community_yap.gox:38:1 @@ -57,475 +53,503 @@ func (this *community) MainEntry() { }{}) }) //line cmd/gopcomm/community_yap.gox:49:1 + this.Get("/demo", func(ctx *yap.Context) { +//line cmd/gopcomm/community_yap.gox:50:1 + ctx.Yap__1("demo", map[string]interface { + }{}) + }) +//line cmd/gopcomm/community_yap.gox:53:1 this.Get("/p/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:52:1 +//line cmd/gopcomm/community_yap.gox:56:1 // todo middleware // Get User Info var user *core.User -//line cmd/gopcomm/community_yap.gox:53:1 +//line cmd/gopcomm/community_yap.gox:57:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:54:1 +//line cmd/gopcomm/community_yap.gox:58:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:55:1 +//line cmd/gopcomm/community_yap.gox:59:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:56:1 +//line cmd/gopcomm/community_yap.gox:60:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:57:1 - zlog.Error("get user error:", err) +//line cmd/gopcomm/community_yap.gox:61:1 + xLog.Error("get user error:", err) } } -//line cmd/gopcomm/community_yap.gox:61:1 +//line cmd/gopcomm/community_yap.gox:65:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:62:1 +//line cmd/gopcomm/community_yap.gox:66:1 article, _ := this.community.Article(todo, id) -//line cmd/gopcomm/community_yap.gox:63:1 +//line cmd/gopcomm/community_yap.gox:67:1 ctx.Yap__1("article", map[string]interface { }{"User": user, "ID": id, "Title": article.Title, "Content": article.HtmlUrl, "Tags": article.Tags, "Cover": article.Cover, "Mtime": article.Mtime.Format(layoutUS), "Author": article.User}) }) -//line cmd/gopcomm/community_yap.gox:76:1 +//line cmd/gopcomm/community_yap.gox:80:1 this.Get("/getArticle/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:77:1 +//line cmd/gopcomm/community_yap.gox:81:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:78:1 +//line cmd/gopcomm/community_yap.gox:82:1 article, _ := this.community.Article(todo, id) -//line cmd/gopcomm/community_yap.gox:79:1 +//line cmd/gopcomm/community_yap.gox:83:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": article}) }) -//line cmd/gopcomm/community_yap.gox:85:1 +//line cmd/gopcomm/community_yap.gox:89:1 this.Get("/user/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:86:1 +//line cmd/gopcomm/community_yap.gox:90:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:88:1 +//line cmd/gopcomm/community_yap.gox:92:1 userClaim, err := this.community.GetUserClaim(id) -//line cmd/gopcomm/community_yap.gox:89:1 +//line cmd/gopcomm/community_yap.gox:93:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:90:1 - zlog.Error("get current user error:", err) - } //line cmd/gopcomm/community_yap.gox:94:1 + xLog.Error("get current user error:", err) + } +//line cmd/gopcomm/community_yap.gox:98:1 // todo middleware // get user by token var user *core.User -//line cmd/gopcomm/community_yap.gox:95:1 +//line cmd/gopcomm/community_yap.gox:99:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:96:1 +//line cmd/gopcomm/community_yap.gox:100:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:97:1 +//line cmd/gopcomm/community_yap.gox:101:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:98:1 +//line cmd/gopcomm/community_yap.gox:102:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:99:1 - zlog.Error("get user error:", err) +//line cmd/gopcomm/community_yap.gox:103:1 + xLog.Error("get user error:", err) } } -//line cmd/gopcomm/community_yap.gox:103:1 +//line cmd/gopcomm/community_yap.gox:107:1 items, _ := this.community.GetArticlesByUid(todo, id) -//line cmd/gopcomm/community_yap.gox:104:1 +//line cmd/gopcomm/community_yap.gox:108:1 ctx.Yap__1("user", map[string]interface { }{"Id": id, "CurrentUser": userClaim, "User": user, "Items": items}) }) -//line cmd/gopcomm/community_yap.gox:112:1 +//line cmd/gopcomm/community_yap.gox:116:1 this.Get("/add", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:113:1 +//line cmd/gopcomm/community_yap.gox:117:1 ctx.Yap__1("edit", map[string]interface { }{}) }) -//line cmd/gopcomm/community_yap.gox:116:1 +//line cmd/gopcomm/community_yap.gox:120:1 this.Get("/", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:118:1 +//line cmd/gopcomm/community_yap.gox:122:1 from := ctx.Param("page") -//line cmd/gopcomm/community_yap.gox:119:1 +//line cmd/gopcomm/community_yap.gox:123:1 limit := ctx.Param("limit") -//line cmd/gopcomm/community_yap.gox:122:1 +//line cmd/gopcomm/community_yap.gox:126:1 // todo middleware // Get User Info var user *core.User -//line cmd/gopcomm/community_yap.gox:123:1 +//line cmd/gopcomm/community_yap.gox:127:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:124:1 +//line cmd/gopcomm/community_yap.gox:128:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:125:1 +//line cmd/gopcomm/community_yap.gox:129:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:126:1 +//line cmd/gopcomm/community_yap.gox:130:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:127:1 - zlog.Error("get user error:", err) +//line cmd/gopcomm/community_yap.gox:131:1 + xLog.Error("get user error:", err) } } -//line cmd/gopcomm/community_yap.gox:131:1 +//line cmd/gopcomm/community_yap.gox:135:1 page, err := strconv.Atoi(from) -//line cmd/gopcomm/community_yap.gox:132:1 +//line cmd/gopcomm/community_yap.gox:136:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:133:1 +//line cmd/gopcomm/community_yap.gox:137:1 page = 1 } -//line cmd/gopcomm/community_yap.gox:136:1 +//line cmd/gopcomm/community_yap.gox:140:1 limitInt, err := strconv.Atoi(limit) -//line cmd/gopcomm/community_yap.gox:137:1 +//line cmd/gopcomm/community_yap.gox:141:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:138:1 +//line cmd/gopcomm/community_yap.gox:142:1 limitInt = 20 } -//line cmd/gopcomm/community_yap.gox:141:1 +//line cmd/gopcomm/community_yap.gox:145:1 articles, next, _ := this.community.Articles(todo, page, limitInt, "") -//line cmd/gopcomm/community_yap.gox:142:1 +//line cmd/gopcomm/community_yap.gox:146:1 ctx.Yap__1("home", map[string]interface { }{"User": user, "Items": articles, "Next": next}) }) -//line cmd/gopcomm/community_yap.gox:149:1 +//line cmd/gopcomm/community_yap.gox:153:1 this.Get("/search", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:150:1 +//line cmd/gopcomm/community_yap.gox:154:1 searchValue := ctx.Param("value") -//line cmd/gopcomm/community_yap.gox:151:1 +//line cmd/gopcomm/community_yap.gox:155:1 if searchValue == "" { -//line cmd/gopcomm/community_yap.gox:152:1 +//line cmd/gopcomm/community_yap.gox:156:1 ctx.Json__1(map[string]interface { }{"code": 400, "err": "value can not be ''."}) } -//line cmd/gopcomm/community_yap.gox:158:1 +//line cmd/gopcomm/community_yap.gox:162:1 from := ctx.Param("page") -//line cmd/gopcomm/community_yap.gox:159:1 +//line cmd/gopcomm/community_yap.gox:163:1 limit := ctx.Param("limit") -//line cmd/gopcomm/community_yap.gox:160:1 +//line cmd/gopcomm/community_yap.gox:164:1 limitInt, err := strconv.Atoi(limit) -//line cmd/gopcomm/community_yap.gox:161:1 +//line cmd/gopcomm/community_yap.gox:165:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:162:1 +//line cmd/gopcomm/community_yap.gox:166:1 limitInt = 10 } -//line cmd/gopcomm/community_yap.gox:164:1 +//line cmd/gopcomm/community_yap.gox:168:1 page, err := strconv.Atoi(from) -//line cmd/gopcomm/community_yap.gox:165:1 +//line cmd/gopcomm/community_yap.gox:169:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:166:1 +//line cmd/gopcomm/community_yap.gox:170:1 page = 1 } -//line cmd/gopcomm/community_yap.gox:170:1 +//line cmd/gopcomm/community_yap.gox:174:1 // todo middleware var user *core.User -//line cmd/gopcomm/community_yap.gox:171:1 +//line cmd/gopcomm/community_yap.gox:175:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:172:1 +//line cmd/gopcomm/community_yap.gox:176:1 if err == nil { -//line cmd/gopcomm/community_yap.gox:173:1 +//line cmd/gopcomm/community_yap.gox:177:1 user, err = this.community.GetUser(token.Value) -//line cmd/gopcomm/community_yap.gox:174:1 +//line cmd/gopcomm/community_yap.gox:178:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:175:1 - zlog.Error("get user error:", err) +//line cmd/gopcomm/community_yap.gox:179:1 + xLog.Error("get user error:", err) } } -//line cmd/gopcomm/community_yap.gox:179:1 +//line cmd/gopcomm/community_yap.gox:183:1 articles, _ := this.community.SearchArticle(todo, searchValue) -//line cmd/gopcomm/community_yap.gox:180:1 +//line cmd/gopcomm/community_yap.gox:184:1 articles, total, _ := this.community.Articles(todo, page, limitInt, searchValue) -//line cmd/gopcomm/community_yap.gox:181:1 +//line cmd/gopcomm/community_yap.gox:185:1 ctx.Yap__1("home", map[string]interface { }{"User": user, "Items": articles, "Total": total, "Value": searchValue}) }) -//line cmd/gopcomm/community_yap.gox:189:1 +//line cmd/gopcomm/community_yap.gox:193:1 this.Get("/edit/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:190:1 +//line cmd/gopcomm/community_yap.gox:194:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:191:1 +//line cmd/gopcomm/community_yap.gox:195:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:192:1 +//line cmd/gopcomm/community_yap.gox:196:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "no token"}) } -//line cmd/gopcomm/community_yap.gox:198:1 +//line cmd/gopcomm/community_yap.gox:202:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:199:1 +//line cmd/gopcomm/community_yap.gox:203:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:200:1 +//line cmd/gopcomm/community_yap.gox:204:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:206:1 +//line cmd/gopcomm/community_yap.gox:210:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:207:1 +//line cmd/gopcomm/community_yap.gox:211:1 if id != "" { -//line cmd/gopcomm/community_yap.gox:208:1 +//line cmd/gopcomm/community_yap.gox:212:1 if -//line cmd/gopcomm/community_yap.gox:208:1 +//line cmd/gopcomm/community_yap.gox:212:1 editable, _ := this.community.CanEditable(todo, uid, id); !editable { -//line cmd/gopcomm/community_yap.gox:209:1 - zlog.Error("no permissions") -//line cmd/gopcomm/community_yap.gox:210:1 +//line cmd/gopcomm/community_yap.gox:213:1 + xLog.Error("no permissions") +//line cmd/gopcomm/community_yap.gox:214:1 http.Redirect(ctx.ResponseWriter, ctx.Request, "/error", http.StatusTemporaryRedirect) } -//line cmd/gopcomm/community_yap.gox:212:1 +//line cmd/gopcomm/community_yap.gox:216:1 article, _ := this.community.Article(todo, id) -//line cmd/gopcomm/community_yap.gox:213:1 +//line cmd/gopcomm/community_yap.gox:217:1 ctx.Yap__1("edit", article) } }) -//line cmd/gopcomm/community_yap.gox:217:1 +//line cmd/gopcomm/community_yap.gox:221:1 this.Get("/getTrans", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:218:1 +//line cmd/gopcomm/community_yap.gox:222:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:219:1 +//line cmd/gopcomm/community_yap.gox:223:1 htmlUrl, err := this.community.TransHtmlUrl(todo, id) -//line cmd/gopcomm/community_yap.gox:220:1 +//line cmd/gopcomm/community_yap.gox:224:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:221:1 +//line cmd/gopcomm/community_yap.gox:225:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:226:1 +//line cmd/gopcomm/community_yap.gox:230:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": htmlUrl}) }) -//line cmd/gopcomm/community_yap.gox:233:1 +//line cmd/gopcomm/community_yap.gox:237:1 this.Post("/commit", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:235:1 +//line cmd/gopcomm/community_yap.gox:239:1 trans := ctx.Param("trans") -//line cmd/gopcomm/community_yap.gox:236:1 +//line cmd/gopcomm/community_yap.gox:240:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:237:1 +//line cmd/gopcomm/community_yap.gox:241:1 mdData := ctx.Param("content") -//line cmd/gopcomm/community_yap.gox:238:1 +//line cmd/gopcomm/community_yap.gox:242:1 htmlData := ctx.Param("html") -//line cmd/gopcomm/community_yap.gox:240:1 +//line cmd/gopcomm/community_yap.gox:244:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:241:1 +//line cmd/gopcomm/community_yap.gox:245:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:242:1 +//line cmd/gopcomm/community_yap.gox:246:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "no token"}) } -//line cmd/gopcomm/community_yap.gox:247:1 +//line cmd/gopcomm/community_yap.gox:251:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:248:1 +//line cmd/gopcomm/community_yap.gox:252:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:249:1 +//line cmd/gopcomm/community_yap.gox:253:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:256:1 +//line cmd/gopcomm/community_yap.gox:260:1 article := &core.Article{ArticleEntry: core.ArticleEntry{ID: id, Title: ctx.Param("title"), UId: uid, Cover: ctx.Param("cover"), Tags: ctx.Param("tags")}, Content: mdData, HtmlData: htmlData} -//line cmd/gopcomm/community_yap.gox:267:1 +//line cmd/gopcomm/community_yap.gox:271:1 id, _ = this.community.PutArticle(todo, uid, trans, article) -//line cmd/gopcomm/community_yap.gox:268:1 +//line cmd/gopcomm/community_yap.gox:272:1 ctx.Json__1(map[string]interface { }{"code": 200, "data": id}) }) -//line cmd/gopcomm/community_yap.gox:276:1 +//line cmd/gopcomm/community_yap.gox:280:1 this.Post("/translate", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:278:1 +//line cmd/gopcomm/community_yap.gox:282:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:279:1 +//line cmd/gopcomm/community_yap.gox:283:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:280:1 +//line cmd/gopcomm/community_yap.gox:284:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "no token"}) } -//line cmd/gopcomm/community_yap.gox:285:1 +//line cmd/gopcomm/community_yap.gox:289:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:286:1 +//line cmd/gopcomm/community_yap.gox:290:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:287:1 +//line cmd/gopcomm/community_yap.gox:291:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:293:1 +//line cmd/gopcomm/community_yap.gox:297:1 mdData := ctx.Param("content") -//line cmd/gopcomm/community_yap.gox:294:1 +//line cmd/gopcomm/community_yap.gox:298:1 htmlData := ctx.Param("html") -//line cmd/gopcomm/community_yap.gox:295:1 +//line cmd/gopcomm/community_yap.gox:299:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:297:1 +//line cmd/gopcomm/community_yap.gox:301:1 transData, err := this.trans.TranslateMarkdownText(mdData, language.Chinese, language.English) -//line cmd/gopcomm/community_yap.gox:298:1 +//line cmd/gopcomm/community_yap.gox:302:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:299:1 +//line cmd/gopcomm/community_yap.gox:303:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:304:1 +//line cmd/gopcomm/community_yap.gox:308:1 id, _ = this.community.SaveHtml(todo, uid, htmlData, mdData, id) -//line cmd/gopcomm/community_yap.gox:305:1 +//line cmd/gopcomm/community_yap.gox:309:1 ctx.Json__1(map[string]interface { }{"code": 200, "id": id, "data": transData}) }) -//line cmd/gopcomm/community_yap.gox:312:1 +//line cmd/gopcomm/community_yap.gox:316:1 this.Get("/getMedia/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:313:1 +//line cmd/gopcomm/community_yap.gox:317:1 mediaId := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:315:1 +//line cmd/gopcomm/community_yap.gox:319:1 fileKey, _ := this.community.GetMediaUrl(context.Background(), mediaId) -//line cmd/gopcomm/community_yap.gox:317:1 +//line cmd/gopcomm/community_yap.gox:321:1 http.Redirect(ctx.ResponseWriter, ctx.Request, domain+fileKey, http.StatusTemporaryRedirect) }) -//line cmd/gopcomm/community_yap.gox:320:1 +//line cmd/gopcomm/community_yap.gox:324:1 this.Get("/getMediaUrl/:id", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:321:1 +//line cmd/gopcomm/community_yap.gox:325:1 id := ctx.Param("id") -//line cmd/gopcomm/community_yap.gox:322:1 +//line cmd/gopcomm/community_yap.gox:326:1 fileKey, err := this.community.GetMediaUrl(todo, id) -//line cmd/gopcomm/community_yap.gox:323:1 +//line cmd/gopcomm/community_yap.gox:327:1 htmlUrl := fmt.Sprintf("%s%s", domain, fileKey) -//line cmd/gopcomm/community_yap.gox:324:1 +//line cmd/gopcomm/community_yap.gox:328:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:325:1 +//line cmd/gopcomm/community_yap.gox:329:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "have no html media"}) } -//line cmd/gopcomm/community_yap.gox:330:1 +//line cmd/gopcomm/community_yap.gox:334:1 ctx.Json__1(map[string]interface { }{"code": 200, "url": htmlUrl}) }) -//line cmd/gopcomm/community_yap.gox:336:1 +//line cmd/gopcomm/community_yap.gox:340:1 this.Post("/upload", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:337:1 +//line cmd/gopcomm/community_yap.gox:341:1 file, header, err := ctx.FormFile("file") -//line cmd/gopcomm/community_yap.gox:338:1 +//line cmd/gopcomm/community_yap.gox:342:1 filename := header.Filename -//line cmd/gopcomm/community_yap.gox:340:1 +//line cmd/gopcomm/community_yap.gox:344:1 ctx.ParseMultipartForm(10 << 20) -//line cmd/gopcomm/community_yap.gox:342:1 +//line cmd/gopcomm/community_yap.gox:346:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:343:1 - zlog.Error("upload file error:", filename) -//line cmd/gopcomm/community_yap.gox:344:1 +//line cmd/gopcomm/community_yap.gox:347:1 + xLog.Error("upload file error:", filename) +//line cmd/gopcomm/community_yap.gox:348:1 ctx.JSON(500, err.Error()) -//line cmd/gopcomm/community_yap.gox:345:1 +//line cmd/gopcomm/community_yap.gox:349:1 return } -//line cmd/gopcomm/community_yap.gox:349:1 +//line cmd/gopcomm/community_yap.gox:353:1 dst, err := os.Create(filename) -//line cmd/gopcomm/community_yap.gox:350:1 +//line cmd/gopcomm/community_yap.gox:354:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:351:1 - zlog.Error("create file error:", file) -//line cmd/gopcomm/community_yap.gox:352:1 +//line cmd/gopcomm/community_yap.gox:355:1 + xLog.Error("create file error:", file) +//line cmd/gopcomm/community_yap.gox:356:1 ctx.JSON(500, err.Error()) -//line cmd/gopcomm/community_yap.gox:353:1 +//line cmd/gopcomm/community_yap.gox:357:1 return } -//line cmd/gopcomm/community_yap.gox:355:1 +//line cmd/gopcomm/community_yap.gox:359:1 defer func() { -//line cmd/gopcomm/community_yap.gox:356:1 +//line cmd/gopcomm/community_yap.gox:360:1 file.Close() -//line cmd/gopcomm/community_yap.gox:357:1 +//line cmd/gopcomm/community_yap.gox:361:1 dst.Close() -//line cmd/gopcomm/community_yap.gox:358:1 +//line cmd/gopcomm/community_yap.gox:362:1 err = os.Remove(filename) -//line cmd/gopcomm/community_yap.gox:359:1 +//line cmd/gopcomm/community_yap.gox:363:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:360:1 - zlog.Error("delete file error:", filename) -//line cmd/gopcomm/community_yap.gox:361:1 +//line cmd/gopcomm/community_yap.gox:364:1 + xLog.Error("delete file error:", filename) +//line cmd/gopcomm/community_yap.gox:365:1 return } }() -//line cmd/gopcomm/community_yap.gox:366:1 +//line cmd/gopcomm/community_yap.gox:370:1 _, err = io.Copy(dst, file) -//line cmd/gopcomm/community_yap.gox:367:1 +//line cmd/gopcomm/community_yap.gox:371:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:368:1 - zlog.Error("copy file errer:", filename) -//line cmd/gopcomm/community_yap.gox:369:1 +//line cmd/gopcomm/community_yap.gox:372:1 + xLog.Error("copy file errer:", filename) +//line cmd/gopcomm/community_yap.gox:373:1 ctx.JSON(500, err.Error()) -//line cmd/gopcomm/community_yap.gox:370:1 +//line cmd/gopcomm/community_yap.gox:374:1 return } -//line cmd/gopcomm/community_yap.gox:372:1 +//line cmd/gopcomm/community_yap.gox:376:1 bytes, err := os.ReadFile(filename) -//line cmd/gopcomm/community_yap.gox:373:1 +//line cmd/gopcomm/community_yap.gox:377:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:374:1 - zlog.Error("read file errer:", filename) -//line cmd/gopcomm/community_yap.gox:375:1 +//line cmd/gopcomm/community_yap.gox:378:1 + xLog.Error("read file errer:", filename) +//line cmd/gopcomm/community_yap.gox:379:1 ctx.JSON(500, err.Error()) -//line cmd/gopcomm/community_yap.gox:376:1 +//line cmd/gopcomm/community_yap.gox:380:1 return } -//line cmd/gopcomm/community_yap.gox:378:1 +//line cmd/gopcomm/community_yap.gox:382:1 token, err := ctx.Request.Cookie("token") -//line cmd/gopcomm/community_yap.gox:379:1 +//line cmd/gopcomm/community_yap.gox:383:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:380:1 +//line cmd/gopcomm/community_yap.gox:384:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": "no token"}) } -//line cmd/gopcomm/community_yap.gox:385:1 +//line cmd/gopcomm/community_yap.gox:389:1 uid, err := this.community.ParseJwtToken(token.Value) -//line cmd/gopcomm/community_yap.gox:386:1 +//line cmd/gopcomm/community_yap.gox:390:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:387:1 +//line cmd/gopcomm/community_yap.gox:391:1 ctx.Json__1(map[string]interface { }{"code": 500, "err": err.Error()}) } -//line cmd/gopcomm/community_yap.gox:392:1 +//line cmd/gopcomm/community_yap.gox:396:1 id, err := this.community.SaveMedia(context.Background(), uid, bytes) -//line cmd/gopcomm/community_yap.gox:393:1 +//line cmd/gopcomm/community_yap.gox:397:1 if err != nil { -//line cmd/gopcomm/community_yap.gox:394:1 - zlog.Error("save file", err.Error()) -//line cmd/gopcomm/community_yap.gox:395:1 +//line cmd/gopcomm/community_yap.gox:398:1 + xLog.Error("save file", err.Error()) +//line cmd/gopcomm/community_yap.gox:399:1 ctx.JSON(500, err.Error()) -//line cmd/gopcomm/community_yap.gox:396:1 +//line cmd/gopcomm/community_yap.gox:400:1 return } -//line cmd/gopcomm/community_yap.gox:400:1 +//line cmd/gopcomm/community_yap.gox:404:1 ctx.JSON(200, id) }) -//line cmd/gopcomm/community_yap.gox:403:1 +//line cmd/gopcomm/community_yap.gox:407:1 this.Get("/login", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:405:1 - redirectURL := ctx.URL.Query().Get("redirect_url") -//line cmd/gopcomm/community_yap.gox:406:1 +//line cmd/gopcomm/community_yap.gox:412:1 + redirectURL := fmt.Sprintf("%s/%s", ctx.Request.Referer()+"callback") +//line cmd/gopcomm/community_yap.gox:414:1 loginURL := this.community.RedirectToCasdoor(redirectURL) -//line cmd/gopcomm/community_yap.gox:407:1 +//line cmd/gopcomm/community_yap.gox:415:1 ctx.Redirect(loginURL, http.StatusFound) }) -//line cmd/gopcomm/community_yap.gox:411:1 +//line cmd/gopcomm/community_yap.gox:418:1 + this.Get("/logout", func(ctx *yap.Context) { +//line cmd/gopcomm/community_yap.gox:419:1 + tokenCookie, err := ctx.Request.Cookie("token") +//line cmd/gopcomm/community_yap.gox:420:1 + if err != nil { +//line cmd/gopcomm/community_yap.gox:421:1 + xLog.Error("get token error:", err) + } +//line cmd/gopcomm/community_yap.gox:425:1 + tokenCookie.MaxAge = -1 +//line cmd/gopcomm/community_yap.gox:426:1 + http.SetCookie(ctx.ResponseWriter, tokenCookie) +//line cmd/gopcomm/community_yap.gox:429:1 + http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080"), http.StatusFound) + }) +//line cmd/gopcomm/community_yap.gox:432:1 this.Get("/callback", func(ctx *yap.Context) { -//line cmd/gopcomm/community_yap.gox:412:1 +//line cmd/gopcomm/community_yap.gox:433:1 code := ctx.URL.Query().Get("code") -//line cmd/gopcomm/community_yap.gox:413:1 +//line cmd/gopcomm/community_yap.gox:434:1 state := ctx.URL.Query().Get("state") -//line cmd/gopcomm/community_yap.gox:415:1 +//line cmd/gopcomm/community_yap.gox:436:1 token, error := this.community.GetAccessToken(code, state) -//line cmd/gopcomm/community_yap.gox:416:1 +//line cmd/gopcomm/community_yap.gox:437:1 if error != nil { -//line cmd/gopcomm/community_yap.gox:417:1 - zlog.Error("err", error) +//line cmd/gopcomm/community_yap.gox:438:1 + xLog.Error("err", error) } -//line cmd/gopcomm/community_yap.gox:420:1 +//line cmd/gopcomm/community_yap.gox:441:1 cookie := http.Cookie{Name: "token", Value: token.AccessToken, Path: "/", MaxAge: 3600} -//line cmd/gopcomm/community_yap.gox:426:1 +//line cmd/gopcomm/community_yap.gox:447:1 http.SetCookie(ctx.ResponseWriter, &cookie) -//line cmd/gopcomm/community_yap.gox:430:1 - http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080?token=%s", token.AccessToken), http.StatusFound) +//line cmd/gopcomm/community_yap.gox:451:1 + http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080"), http.StatusFound) }) -//line cmd/gopcomm/community_yap.gox:433:1 +//line cmd/gopcomm/community_yap.gox:454:1 conf := &core.Config{} -//line cmd/gopcomm/community_yap.gox:434:1 +//line cmd/gopcomm/community_yap.gox:455:1 this.community, _ = core.New(todo, conf) -//line cmd/gopcomm/community_yap.gox:435:1 +//line cmd/gopcomm/community_yap.gox:456:1 this.trans = translation.New(os.Getenv("NIUTRANS_API_KEY"), "", "") -//line cmd/gopcomm/community_yap.gox:436:1 +//line cmd/gopcomm/community_yap.gox:457:1 core.CasdoorConfigInit() -//line cmd/gopcomm/community_yap.gox:443:1 - zlog.Info("Started in endpoint: ", endpoint) -//line cmd/gopcomm/community_yap.gox:446:1 +//line cmd/gopcomm/community_yap.gox:460:1 + this.Handle("/", func(ctx *yap.Context) { +//line cmd/gopcomm/community_yap.gox:461:1 + ctx.Yap__1("4xx", map[string]interface { + }{}) + }) +//line cmd/gopcomm/community_yap.gox:464:1 + xLog.Info("Started in endpoint: ", endpoint) +//line cmd/gopcomm/community_yap.gox:467:1 this.Run(endpoint, func(h http.Handler) http.Handler { -//line cmd/gopcomm/community_yap.gox:448:1 +//line cmd/gopcomm/community_yap.gox:469:1 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { -//line cmd/gopcomm/community_yap.gox:449:1 +//line cmd/gopcomm/community_yap.gox:470:1 defer func() { -//line cmd/gopcomm/community_yap.gox:450:1 +//line cmd/gopcomm/community_yap.gox:471:1 if -//line cmd/gopcomm/community_yap.gox:450:1 +//line cmd/gopcomm/community_yap.gox:471:1 err := recover(); err != nil { -//line cmd/gopcomm/community_yap.gox:451:1 +//line cmd/gopcomm/community_yap.gox:472:1 http.Redirect(w, r, "/failed", http.StatusFound) } }() -//line cmd/gopcomm/community_yap.gox:455:1 +//line cmd/gopcomm/community_yap.gox:476:1 h.ServeHTTP(w, r) }) }) diff --git a/cmd/gopcomm/yap/2xx_yap.html b/cmd/gopcomm/yap/2xx_yap.html index 0b9d8f32..fb43a7d4 100644 --- a/cmd/gopcomm/yap/2xx_yap.html +++ b/cmd/gopcomm/yap/2xx_yap.html @@ -13,7 +13,7 @@