Skip to content

Commit

Permalink
Merge pull request #94 from xushiwei/q
Browse files Browse the repository at this point in the history
yap: classfile v2
  • Loading branch information
xushiwei authored Mar 8, 2024
2 parents 0485a60 + 8474063 commit f31eacf
Show file tree
Hide file tree
Showing 26 changed files with 284 additions and 111 deletions.
4 changes: 4 additions & 0 deletions classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
GopPackage = true
)

// App is project class of YAP classfile (old version).
type App struct {
Engine
}
Expand Down Expand Up @@ -86,6 +87,9 @@ func (p *App) Static__2(pattern string, fs http.FileSystem, allowRedirect ...boo
type AppType interface {
InitYap(fs ...fs.FS)
SetLAS(listenAndServe func(addr string, handler http.Handler) error)
Route(method, path string, handle func(ctx *Context))
Handle(pattern string, f func(ctx *Context))
Run(addr string, mws ...func(h http.Handler) http.Handler) error
}

var (
Expand Down
70 changes: 70 additions & 0 deletions classfile_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package yap

import (
"strings"
)

// Handler is worker class of YAP classfile (v2).
type Handler struct {
Context
}

func (p *Handler) Main(ctx *Context) {
p.Context = *ctx
}

var (
repl = strings.NewReplacer("_", "/", "#", ":")
)

func parseClassfname(name string) (method, path string) {
pos := strings.IndexByte(name, '_')
if pos < 0 {
return name, "/"
}
return name[:pos], repl.Replace(name[pos:])
}

// AppV2 is project class of YAP classfile (v2).
type AppV2 struct {
App
}

type iHandler interface {
Main(ctx *Context)
Classfname() string
}

// Gopt_AppV2_Main is required by Go+ compiler as the entry of a YAP project.
func Gopt_AppV2_Main(app AppType, handlers ...iHandler) {
app.InitYap()
for _, h := range handlers {
switch method, path := parseClassfname(h.Classfname()); method {
case "handle":
app.Handle(path, h.Main)
default:
app.Route(strings.ToUpper(method), path, h.Main)
}
}
if me, ok := app.(interface{ MainEntry() }); ok {
me.MainEntry()
} else {
app.Run(":8080")
}
}
3 changes: 3 additions & 0 deletions demo/classfile2_blog/get_p_#id.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yap "article", {
"id": param("id"),
}
24 changes: 24 additions & 0 deletions demo/classfile2_blog/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions demo/classfile2_blog/yap/article_yap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
Article {{.id}}
</body>
</html>
1 change: 1 addition & 0 deletions demo/classfile2_hello/get.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
html `<html><body>Hello, <a href="/p/123">Yap</a>!</body></html>`
3 changes: 3 additions & 0 deletions demo/classfile2_hello/get_p_#id.yap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json {
"id": param("id"),
}
36 changes: 36 additions & 0 deletions demo/classfile2_hello/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions demo/classfile2_static/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions demo/classfile2_statichttp/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "github.com/qiniu/x/http/fs"

static "/", fs.http("https://goplus.org"), false
run ":8888"
run ":8080"
20 changes: 10 additions & 10 deletions demo/classfile_hello/gop_autogen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
23 changes: 0 additions & 23 deletions demo/classfile_static/gop_autogen.go

This file was deleted.

24 changes: 0 additions & 24 deletions demo/classfile_statichttp/gop_autogen.go

This file was deleted.

4 changes: 4 additions & 0 deletions gop.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
gop 1.2

project .yap AppV2 github.com/goplus/yap

class .yap Handler

project _yap.gox App github.com/goplus/yap

project _yapt.gox App github.com/goplus/yap/ytest github.com/goplus/yap/test
Expand Down
Loading

0 comments on commit f31eacf

Please sign in to comment.