github.com/goplus/yap@v0.8.1/classfile.go (about) 1 /* 2 * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package yap 18 19 import ( 20 "context" 21 "io/fs" 22 "net/http" 23 24 "github.com/qiniu/x/http/fsx" 25 ) 26 27 const ( 28 GopPackage = true 29 ) 30 31 // App is project class of YAP classfile (old version). 32 type App struct { 33 Engine 34 } 35 36 // Get is a shortcut for router.Route(http.MethodGet, path, handle) 37 func (p *App) Get(path string, handle func(ctx *Context)) { 38 p.Route(http.MethodGet, path, handle) 39 } 40 41 // Head is a shortcut for router.Route(http.MethodHead, path, handle) 42 func (p *App) Head(path string, handle func(ctx *Context)) { 43 p.Route(http.MethodHead, path, handle) 44 } 45 46 // Options is a shortcut for router.Route(http.MethodOptions, path, handle) 47 func (p *App) Options(path string, handle func(ctx *Context)) { 48 p.Route(http.MethodOptions, path, handle) 49 } 50 51 // Post is a shortcut for router.Route(http.MethodPost, path, handle) 52 func (p *App) Post(path string, handle func(ctx *Context)) { 53 p.Route(http.MethodPost, path, handle) 54 } 55 56 // Put is a shortcut for router.Route(http.MethodPut, path, handle) 57 func (p *App) Put(path string, handle func(ctx *Context)) { 58 p.Route(http.MethodPut, path, handle) 59 } 60 61 // Patch is a shortcut for router.Route(http.MethodPatch, path, handle) 62 func (p *App) Patch(path string, handle func(ctx *Context)) { 63 p.Route(http.MethodPatch, path, handle) 64 } 65 66 // Delete is a shortcut for router.Route(http.MethodDelete, path, handle) 67 func (p *App) Delete(path string, handle func(ctx *Context)) { 68 p.Route(http.MethodDelete, path, handle) 69 } 70 71 // Static serves static files from a dir (default is "$YapFS/static"). 72 func (p *App) Static__0(pattern string, dir ...fs.FS) { 73 p.Static(pattern, dir...) 74 } 75 76 // Static serves static files from a http file system scheme (url). 77 // See https://pkg.go.dev/github.com/qiniu/x/http/fsx for more information. 78 func (p *App) Static__1(pattern string, ctx context.Context, url string) (closer fsx.Closer, err error) { 79 fs, closer, err := fsx.Open(ctx, url) 80 if err == nil { 81 p.StaticHttp(pattern, fs, false) 82 } 83 return 84 } 85 86 // Static serves static files from a http file system. 87 func (p *App) Static__2(pattern string, fs http.FileSystem, allowRedirect ...bool) { 88 p.StaticHttp(pattern, fs, allowRedirect...) 89 } 90 91 // AppType represents an abstract of YAP applications. 92 type AppType interface { 93 InitYap(fs ...fs.FS) 94 SetLAS(listenAndServe func(addr string, handler http.Handler) error) 95 Route(method, path string, handle func(ctx *Context)) 96 Handle(pattern string, f func(ctx *Context)) 97 Run(addr string, mws ...func(h http.Handler) http.Handler) error 98 } 99 100 var ( 101 _ AppType = (*App)(nil) 102 _ AppType = (*Engine)(nil) 103 ) 104 105 // Gopt_App_Main is required by Go+ compiler as the entry of a YAP project. 106 func Gopt_App_Main(app AppType) { 107 app.InitYap() 108 app.(interface{ MainEntry() }).MainEntry() 109 } 110 111 const ( 112 mimeText = "text/plain" 113 mimeHtml = "text/html" 114 mimeBinary = "application/octet-stream" 115 ) 116 117 func (p *Context) Text__0(code int, mime string, text string) { 118 p.TEXT(code, mime, text) 119 } 120 121 func (p *Context) Text__1(code int, text string) { 122 p.TEXT(code, mimeText, text) 123 } 124 125 func (p *Context) Text__2(text string) { 126 p.TEXT(200, mimeText, text) 127 } 128 129 func (p *Context) Text__3(code int, text []byte) { 130 p.DATA(code, mimeText, text) 131 } 132 133 func (p *Context) Text__4(text []byte) { 134 p.DATA(200, mimeText, text) 135 } 136 137 func (p *Context) Binary__0(code int, mime string, data []byte) { 138 p.DATA(code, mime, data) 139 } 140 141 func (p *Context) Binary__1(code int, data []byte) { 142 p.DATA(code, mimeBinary, data) 143 } 144 145 func (p *Context) Binary__2(data []byte) { 146 p.DATA(200, mimeBinary, data) 147 } 148 149 func (p *Context) Binary__3(code int, data string) { 150 p.TEXT(code, mimeBinary, data) 151 } 152 153 func (p *Context) Binary__4(data string) { 154 p.TEXT(200, mimeBinary, data) 155 } 156 157 func (p *Context) Html__0(code int, text string) { 158 p.TEXT(code, mimeHtml, text) 159 } 160 161 func (p *Context) Html__1(text string) { 162 p.TEXT(200, mimeHtml, text) 163 } 164 165 func (p *Context) Html__2(code int, text []byte) { 166 p.DATA(code, mimeHtml, text) 167 } 168 169 func (p *Context) Html__3(text []byte) { 170 p.DATA(200, mimeHtml, text) 171 } 172 173 func (p *Context) Json__0(code int, data interface{}) { 174 p.JSON(code, data) 175 } 176 177 func (p *Context) Json__1(data interface{}) { 178 p.JSON(200, data) 179 } 180 181 func (p *Context) PrettyJson__0(code int, data interface{}) { 182 p.PrettyJSON(code, data) 183 } 184 185 func (p *Context) PrettyJson__1(data interface{}) { 186 p.PrettyJSON(200, data) 187 } 188 189 func (p *Context) Yap__0(code int, yapFile string, data interface{}) { 190 p.YAP(code, yapFile, data) 191 } 192 193 func (p *Context) Yap__1(yapFile string, data interface{}) { 194 p.YAP(200, yapFile, data) 195 }