gok

command module
v0.0.0-...-92f2019 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2014 License: Apache-2.0 Imports: 20 Imported by: 0

README

gok

Build web apps in go, php style.

<html>
	<body>
		<?go for i := 0; i < 5; i++ { ?>
			<p>Iteration <?go gok.Echo(i) ?></p>
		<?go } ?>
	</body>
</html>

everything between <?go ?> can be valid go code

installation:

	go get https://github.com/YuaShizuki/gok.git
	echo "PATH=$PATH:$GOPATH/bin" >> ~/.bash_profile

the last command makes sure that $GOPATH/bin is in your standard path

gok tags:


  1. can scope any valid go code. Code scoped in this tag executes inside a render function.

  2. allows you to define a global function thats accessible by all ".gok" scripts. functions defined in this tag cannot be methods of objects or types. hence: <?gofn (type) funcName( args ) (return types) { } ?>
    would be incorect.


  3. tag allows for imports in gok scripts. example

    	<?goimp 
    		"fmt"
    		"io/ioutil"
    	?>
    	<html>
    		<body>
    			...
    

  4. gouse tag allows to define global variables, functions and types.

    	<?gouse
    		type nstring string 
    		var x nstring = "Hello World"
    		func (self nstring) PrintMe() { 
    			fmt.Println(self)
    		}
    		...
    	?>    
    

  5. go ajax accessed function.
    this is a special function, that is only accessed through javascript, hence the limitations on the parameters. Calling this function from javascript involves including gok.js in html. example:

    	<?go@fn Swap(args []string) ([]string, error) {
    		return []string{args[1], args[0]}, nil
    	}?>
    	<script src="/gok.js"></script>
    	<script>
    		function callback(response) {
    			console.log(response) //prints => ["Hello World", "1"]
    		}
    		gok.Swap(1, "Hello World", callback);
    	</script>
    

    this allows for quick ajax.

commands:

  1. $gok build
    converts .gok file to valid go files and them compiles them to the final executable.

  2. $gok run
    gok run builds the executable and then runs it, any new changes made to the source would update the executable, hence restarting it.

  3. $gok src
    src converts .gok files to .go files, running $go build in this directory would result in the final executable.

API:

  • gok: Core struct encapsulates the http.Request and http.ResponseWriter.
    Instance present in all <?go ?> tags as gok.

    	type Gok struct { ... }
    
  • Echo: Equivalent to php echo. echo accepts any type of parameters.

    	func (self *Gok) Echo(a ...interface{}) { ... }
    
  • Redirect: redirects the request to a new url

    	func (self *Gok) Redirect(newUrl string) { ... }
    
  • Die: sends the msg as error, and undoes everything echoed so far.

    	func (self *Gok) Die(msg string) { ... }
    
  • Server Functions PHP equivalent to $_SERVER[' ']

        //returns current path, without the leadind '/'
    	func (self *Gok) ServerSelf() string { ... }
    	func (self *Gok) ServerSelf() string { ... }
    	func (self *Gok) ServerHttpUserAgent() string { ... }
    	func (self *Gok) ServerHttpReferer() string { ... }
    	func (self *Gok) ServerHttps() bool { ... }
    	func (self *Gok) ServerRemoteAddr() string { ... }
        func (self *Gok) ServerRemotePort() string { ... }
        func (self *Gok) ServerPort() int { ... }
        func (self *Gok) ServerHttpAcceptEncoding() string { ... }
        func (self *Gok) ServerProtocol() string { ... }
        //returns "POST" or "GET"
        func (self *Gok) ServerRequestMethod() string { ... }
        func (self *Gok) ServerQueryString() string { ... }
        func (self *Gok) ServerHttpAccept() string { ... }
        func (self *Gok) ServerHttpAcceptCharset() string { ... }
        func (self *Gok) ServerHttpAcceptLanguage() string { ... }
        func (self *Gok) ServerHttpConnection() string { ... }
        func (self *Gok) ServerHttpHost() string { ... }
    
  • Get and Post: PHP equivalent to $_GET[' '] and $_POST[' ']

    	// parses the post request and returns the post value of `name`,
    	// in case for post containing file date. use gok.File('fileName')
    	func (self *Gok) Post(name string) string { ... }
    
    	// the same as above but for get requests.
    	func (self *Gok) Get(name string) string { ... }
    
  • Cookies: PHP equivalent of $_COOKIE[' ']

    	// returns the cookie value for `name`, if no cookie is set returns an
    	// empty string ""
    	func (self *Gok) Cookie(name string) string { ... }
    
    	// sets cookie with name, value, and duration to expire, if duration is 0
    	// cookie is permanent
    	func (self *Gok) SetCookie(name string, value string, duration int64) { ... }
    
    	//delets the cookie set with `name`
    	func (self *Gok) DeleteCookie(name string) { ... }
    
    	//set cookie with extra params
    	func (self *Gok) SetCookie_4(name string, value string, duration int64,
                                urlPath string){ ... }
        func (self *Gok) SetCookie_5(name string, value string, duration int64,
                                urlPath string, domain string) { ... }
        func (self *Gok) SetCookie_7(name string, value string, duration int64,
                                urlPath string, domain string, secure bool,
                                httpOnly bool) { ... }
    
  • File Uploads: PHP equivalent of $_FILE[' ']

    	// File saves the file upload to disk and returns 
    	// (`file path`,`uploaded file name`, `file content type`, `size`).
    	// its important that file uploads occure from form with 
    	// enctype='multipart/form-data' set.
    	func (self *Gok) File(name string) (string, string, string, int64) { ... }
    
  • Header: PHP equivalent of $_HEADER[' ']

    	// sets the header for response.	
    	// ex: gok.Header("Connection:Close")
    	func (self *Gok) Header(header string) { ... }
    
  • Go http.Header: returns http.Header for Gok instance

    	//go htt.Header for request
    	func (self *Gok) RequestHeader() http.Header { ... }
    
    	//go http.Header for Response
    	func (self *Gok) ResponseHeader() http.Header { ... }
    
  • http.ResponseWriter and *http.Request:

    	// http.ResponseWriter for gok instance
    	func (self *Gok) ResponseWriter() http.ResponseWriter { ... }
    
    	// *http.Request for gok instance
    	func (self *Gok) HttpRequest() *http.Request { ... }
    

Sublime text syntax:

To get syntax higlighting for sublime text, move gok.tmLanguage to sublime text data directory.

  • windows: %APPDATA%\Sublime Text 3\Package\User\
  • osx: ~/Library/Application Support/Sublime Text 3/Package/User
  • linux:~/.config/sublime-text-3/Package/User

TODO:

  • https support, next week.
  • websocket support
  • template genration. making gok easly integratable with martini, and other web serever frame works.

Copyright 2014 Yua Shizuki. 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.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL