Skip to content

Commit

Permalink
Merge pull request #6 from xushiwei/blog
Browse files Browse the repository at this point in the history
yapx classfile
  • Loading branch information
xushiwei authored Jan 6, 2024
2 parents 09a2ce8 + 62b771d commit c73355e
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 3 deletions.
11 changes: 11 additions & 0 deletions demo/classfile/blog.yapx.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type article struct {
ID string
}

get "/p/:id", ctx => {
ctx.yap "article", article{
ID: ctx.param("id"),
}
}

run ":8080"
23 changes: 23 additions & 0 deletions demo/classfile/gop_autogen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "github.com/goplus/yap"

type article struct {
ID string
}
type blog struct {
yap.App
}
//line demo/classfile/blog.yapx.gox:5
func (this *blog) MainEntry() {
//line demo/classfile/blog.yapx.gox:5:1
this.Get("/p/:id", func(ctx *yap.Context) {
//line demo/classfile/blog.yapx.gox:6:1
ctx.Yap__1("article", article{ID: ctx.Param("id")})
})
//line demo/classfile/blog.yapx.gox:11:1
this.Run__1(":8080")
}
func main() {
yap.Gopt_App_Main(new(blog))
}
8 changes: 8 additions & 0 deletions demo/classfile/yap/article.yap
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>
5 changes: 5 additions & 0 deletions gop.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/goplus/yap

gop 1.1

project .yapx App github.com/goplus/yap
13 changes: 10 additions & 3 deletions yap.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ func New(fs ...fs.FS) *Engine {
e := &Engine{
Mux: http.NewServeMux(),
}
e.router.init()
if fs != nil {
e.fs = fs[0]
e.tpls = make(map[string]Template)
e.initYapFS(fs[0])
}
e.router.init()
return e
}

func (p *Engine) initYapFS(fsys fs.FS) {
if sub, e := fs.Sub(fsys, "yap"); e == nil {
fsys = sub
}
p.fs = fsys
p.tpls = make(map[string]Template)
}

func (p *Engine) NewContext(w http.ResponseWriter, r *http.Request) *Context {
ctx := &Context{ResponseWriter: w, Request: r, engine: p}
return ctx
Expand Down
177 changes: 177 additions & 0 deletions yapx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* 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 (
"io/fs"
"net/http"
"os"
)

const (
GopPackage = true
)

type Apper interface {
initApp()
}

type App struct {
*Engine
}

func (p *App) initApp() {
p.Engine = New()
}

// Get is a shortcut for router.Route(http.MethodGet, path, handle)
func (p App) Get(path string, handle func(ctx *Context)) {
p.Route(http.MethodGet, path, handle)
}

// Head is a shortcut for router.Route(http.MethodHead, path, handle)
func (p App) Head(path string, handle func(ctx *Context)) {
p.Route(http.MethodHead, path, handle)
}

// Options is a shortcut for router.Route(http.MethodOptions, path, handle)
func (p App) Options(path string, handle func(ctx *Context)) {
p.Route(http.MethodOptions, path, handle)
}

// Post is a shortcut for router.Route(http.MethodPost, path, handle)
func (p App) Post(path string, handle func(ctx *Context)) {
p.Route(http.MethodPost, path, handle)
}

// Put is a shortcut for router.Route(http.MethodPut, path, handle)
func (p App) Put(path string, handle func(ctx *Context)) {
p.Route(http.MethodPut, path, handle)
}

// Patch is a shortcut for router.Route(http.MethodPatch, path, handle)
func (p App) Patch(path string, handle func(ctx *Context)) {
p.Route(http.MethodPatch, path, handle)
}

// Delete is a shortcut for router.Route(http.MethodDelete, path, handle)
func (p App) Delete(path string, handle func(ctx *Context)) {
p.Route(http.MethodDelete, path, handle)
}

// Run with specified `fsys` as yap template directory.
func (p App) Run__0(fsys fs.FS, addr string, mws ...func(h http.Handler) http.Handler) {
p.initYapFS(fsys)
p.Run(addr, mws...)
}

// Run with cwd as yap template directory.
func (p App) Run__1(addr string, mws ...func(h http.Handler) http.Handler) {
p.Run__0(os.DirFS("."), addr, mws...)
}

// Gopt_App_Main is required by Go+ compiler as the entry of a .yapx project.
func Gopt_App_Main(app Apper) {
app.initApp()
app.(interface{ MainEntry() }).MainEntry()
}

const (
mimeText = "text/plain"
mimeHtml = "text/html"
mimeBinary = "application/octet-stream"
)

func (p *Context) Text__0(code int, mime string, text string) {
p.TEXT(code, mime, text)
}

func (p *Context) Text__1(code int, text string) {
p.TEXT(code, mimeText, text)
}

func (p *Context) Text__2(text string) {
p.TEXT(200, mimeText, text)
}

func (p *Context) Text__3(code int, text []byte) {
p.DATA(code, mimeText, text)
}

func (p *Context) Text__4(text []byte) {
p.DATA(200, mimeText, text)
}

func (p *Context) Binary__0(code int, mime string, data []byte) {
p.DATA(code, mime, data)
}

func (p *Context) Binary__1(code int, data []byte) {
p.DATA(code, mimeBinary, data)
}

func (p *Context) Binary__2(data []byte) {
p.DATA(200, mimeBinary, data)
}

func (p *Context) Binary__3(code int, data string) {
p.TEXT(code, mimeBinary, data)
}

func (p *Context) Binary__4(data string) {
p.TEXT(200, mimeBinary, data)
}

func (p *Context) Html__0(code int, text string) {
p.TEXT(code, mimeHtml, text)
}

func (p *Context) Html__1(text string) {
p.TEXT(200, mimeHtml, text)
}

func (p *Context) Html__2(code int, text []byte) {
p.DATA(code, mimeHtml, text)
}

func (p *Context) Html__3(text []byte) {
p.DATA(200, mimeHtml, text)
}

func (p *Context) Json__0(code int, data interface{}) {
p.JSON(code, data)
}

func (p *Context) Json__1(data interface{}) {
p.JSON(200, data)
}

func (p *Context) PrettyJson__0(code int, data interface{}) {
p.PrettyJSON(code, data)
}

func (p *Context) PrettyJson__1(data interface{}) {
p.PrettyJSON(200, data)
}

func (p *Context) Yap__0(code int, yapFile string, data interface{}) {
p.YAP(code, yapFile, data)
}

func (p *Context) Yap__1(yapFile string, data interface{}) {
p.YAP(200, yapFile, data)
}

0 comments on commit c73355e

Please sign in to comment.