-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
yap: classfile v2
- Loading branch information
There are no files selected for viewing
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") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
yap "article", { | ||
"id": param("id"), | ||
} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
</head> | ||
<body> | ||
Article {{.id}} | ||
</body> | ||
</html> |
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>` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
json { | ||
"id": param("id"), | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
This file was deleted.