Tuesday, December 3, 2013

Part II: Setup a basic admin panel template

First of all you have to read the short tutorial for Go at the Googles App Engine Tutorial Section. If you have done it, we can go on.

Our app is called myCanteen.
To be sure your folder is setup correct:
  • myCanteen
    •  myCanteen
      • myCanteen.go
    • app.yaml


Here is the app.yaml for the project. The *.yaml file is mandatory for the development with the engine.


application: mycanteen
version: 1
runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

Application is the unique identifier of our package respectively of our app. Version is explains its self. Runtime is go, e.g if you develop in python you have to set python. API_VERSION should be go1, because go2 hasn't yet been released. In addition we specifiy our handlers which are responsible for the "routing".

So then we can start with our adminpanel :) We write down a basic Go - Application to get the first succeed.



package mycanteen
 
import(
 "net/http"
 "html/template"
)
 
// No main function. Go App Engine use the init-func
func init() {
    http.HandleFunc("/", root)
    http.HandleFunc("/admin", admin);
}
 
func root(w http.ResponseWriter, r *http.Request) {    
    // Here we will add the Userpanel, but not know 
}
 
func admin(w http.ResponseWriter, r *http.Request) {    
     
    // Execute the parsing. We pass the ResponseWriter and a second value, which should be fill the gaps at the template. 
    // We have no gaps, so we have nothing to fill in. 
    if err := adminPanelTemplate.Execute(w, ""); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
}
// Parse the HTMLTemplate, despite it is not neccesary yet.
var adminPanelTemplate = template.Must(template.New("adminPanelHTML").Parse(adminPanelHTML))
 
// Here you define the HTML which will be parsed.
const adminPanelHTML= `
<html>
<head>
</head>
<body>
Name: Price: Date:
</body> </html> `
After all we can start our appengine with the command:  
directory/of/the/engine/goapp.bat serve mycanteen

Open your browser and type: http://localhost:8080/admin 

Congratulations!

No comments:

Post a Comment