@ -6,12 +6,13 @@ Dr. Klaus Wendel
"Go ahead! Was wird kommen, wenn Java wieder eine Insel ist? – Ein Kurs zur Softwareentwicklung mit Go"
Lektion 11 : Ganz einfacher Webserver
Lektion 11 : Apache ? Go kann das selber und viel besser !
* /
package main
import (
"fmt"
"log"
"net/http"
)
@ -27,8 +28,12 @@ func handler01(w http.ResponseWriter, r *http.Request) {
< meta charset = "UTF-8" / >
< title > Testseite 01 < / title >
< / head >
< body > Du befindest Dich auf Seite ` +"%s!"+ ` < / body >
< / html > ` , r . URL . Path [ 1 : ] )
< body > Du befindest Dich auf Seite ` +"%s"+ ` ( ` +"%s"+ ` - Request ) < / body >
< / html > ` , r . URL . Path [ 1 : ] , r . Method )
r . ParseForm ( )
x := r . Form
log . Println ( x )
}
func handler02 ( w http . ResponseWriter , r * http . Request ) {
@ -37,16 +42,155 @@ func handler02(w http.ResponseWriter, r *http.Request) {
< html xmlns = "http://www.w3.org/1999/xhtml" lang = "de" xml : lang = "de" >
< head >
< meta charset = "UTF-8" / >
< title > Testseite 01 < / title >
< title > Testseite 02 < / title >
< / head >
< body > < b > Du < / b > befindest Dich jetzt auf < i > Seite ` +"%s!"+ ` < / i > < / body >
< / html > ` , r . URL . Path [ 1 : ] )
< body > Du befindest Dich auf Seite ` +"%s"+ ` ( ` +"%s"+ ` - Request ) < / body >
< / html > ` , r . URL . Path [ 1 : ] , r . Method )
r . ParseForm ( )
x := r . Form
fmt . Println ( x )
}
func main ( ) {
func handler03 ( w http . ResponseWriter , r * http . Request ) {
fmt . Fprintf ( w , ` < ! DOCTYPE html >
< html >
< head >
< meta charset = "UTF-8" >
< title > Basic CRUD Application - jQuery EasyUI CRUD Demo < / title >
< link rel = "stylesheet" type = "text/css" href = "./easyui/themes/default/easyui.css" >
< link rel = "stylesheet" type = "text/css" href = "./easyui2/themes/icon.css" >
< link rel = "stylesheet" type = "text/css" href = "http://localhost:8082/easyui/themes/color.css" >
< link rel = "stylesheet" type = "text/css" href = "https://www.jeasyui.com/easyui/demo/demo.css" >
< script type = "text/javascript" src = "https://code.jquery.com/jquery-1.9.1.min.js" > < / script >
< script type = "text/javascript" src = "https://www.jeasyui.com/easyui/jquery.easyui.min.js" > < / script >
< / head >
< body >
< h2 > Basic CRUD Application < / h2 >
< p > Click the buttons on datagrid toolbar to do crud actions . < / p >
< table id = "dg" title = "My Users" class = "easyui-datagrid" style = "width:700px;height:250px"
url = "getSomeJson"
toolbar = "#toolbar" pagination = "true"
rownumbers = "true" fitColumns = "true" singleSelect = "true" >
< thead >
< tr >
< th field = "firstname" width = "50" > First Name < / th >
< th field = "lastname" width = "50" > Last Name < / th >
< th field = "phone" width = "50" > Phone < / th >
< th field = "email" width = "50" > Email < / th >
< / tr >
< / thead >
< / table >
< div id = "toolbar" >
< a href = "javascript:void(0)" class = "easyui-linkbutton" iconCls = "icon-add" plain = "true" onclick = "newUser()" > New User < / a >
< a href = "javascript:void(0)" class = "easyui-linkbutton" iconCls = "icon-edit" plain = "true" onclick = "editUser()" > Edit User < / a >
< a href = "javascript:void(0)" class = "easyui-linkbutton" iconCls = "icon-remove" plain = "true" onclick = "destroyUser()" > Remove User < / a >
< / div >
< div id = "dlg" class = "easyui-dialog" style = "width:400px"
closed = "true" buttons = "#dlg-buttons" >
< form id = "fm" method = "post" novalidate style = "margin:0;padding:20px 50px" >
< div style = "margin-bottom:20px;font-size:14px;border-bottom:1px solid #ccc" > User Information < / div >
< div style = "margin-bottom:10px" >
< input name = "firstname" class = "easyui-textbox" required = "true" label = "First Name:" style = "width:100%" >
< / div >
< div style = "margin-bottom:10px" >
< input name = "lastname" class = "easyui-textbox" required = "true" label = "Last Name:" style = "width:100%" >
< / div >
< div style = "margin-bottom:10px" >
< input name = "phone" class = "easyui-textbox" required = "true" label = "Phone:" style = "width:100%" >
< / div >
< div style = "margin-bottom:10px" >
< input name = "email" class = "easyui-textbox" required = "true" validType = "email" label = "Email:" style = "width:100%" >
< / div >
< / form >
< / div >
< div id = "dlg-buttons" >
< a href = "javascript:void(0)" class = "easyui-linkbutton c6" iconCls = "icon-ok" onclick = "saveUser()" style = "width:90px" > Save < / a >
< a href = "javascript:void(0)" class = "easyui-linkbutton" iconCls = "icon-cancel" onclick = "javascript:$('#dlg').dialog('close')" style = "width:90px" > Cancel < / a >
< / div >
< script type = "text/javascript" >
var url ;
function newUser ( ) {
$ ( ' # dlg ' ) . dialog ( ' open ' ) . dialog ( ' center ' ) . dialog ( ' setTitle ',' New User ' ) ;
$ ( ' # fm ' ) . form ( ' clear ' ) ;
url = ' aa ' ;
}
function editUser ( ) {
var row = $ ( ' # dg ' ) . datagrid ( ' getSelected ' ) ;
if ( row ) {
$ ( ' # dlg ' ) . dialog ( ' open ' ) . dialog ( ' center ' ) . dialog ( ' setTitle ',' Edit User ' ) ;
$ ( ' # fm ' ) . form ( ' load ' , row ) ;
url = ' bb ? id = ' + row . id ;
}
}
function saveUser ( ) {
$ ( ' # fm ' ) . form ( ' submit ' , {
url : url ,
onSubmit : function ( ) {
return $ ( this ) . form ( ' validate ' ) ;
} ,
success : function ( result ) {
var result = eval ( '(' + result + ')' ) ;
if ( result . errorMsg ) {
$ . messager . show ( {
title : ' Error ' ,
msg : result . errorMsg
} ) ;
} else {
$ ( ' # dlg ' ) . dialog ( ' close ' ) ; // close the dialog
$ ( ' # dg ' ) . datagrid ( ' reload ' ) ; // reload the user data
}
}
} ) ;
}
function destroyUser ( ) {
var row = $ ( ' # dg ' ) . datagrid ( ' getSelected ' ) ;
if ( row ) {
$ . messager . confirm ( ' Confirm ',' Are you sure you want to destroy this user ? ' , function ( r ) {
if ( r ) {
$ . post ( ' destroy_user . php ' , { id : row . id } , function ( result ) {
if ( result . success ) {
$ ( ' # dg ' ) . datagrid ( ' reload ' ) ; // reload the user data
} else {
$ . messager . show ( { // show error message
title : ' Error ' ,
msg : result . errorMsg
} ) ;
}
} , ' json ' ) ;
}
} ) ;
}
}
< / script >
< / body >
< / html > ` )
}
func handler04 ( w http . ResponseWriter , r * http . Request ) {
fmt . Fprintf ( w , ` { "total":"3","rows":[ { "id":"1","firstname":"Schnulli","lastname":"Bert","phone":"0711-110","email":"s.bert@schluffi.com"}, { "id":"2","firstname":"Schnick","lastname":"Schnack","phone":"00306669666","email":"schnacki@gdotmail.cum"}, { "id":"3","firstname":"Susi","lastname":"Schneckenschiß","phone":"036742235141","email":"susi@com.ma"}]} ` )
}
func handler05 ( w http . ResponseWriter , r * http . Request ) {
fmt . Fprintf ( w , "Guckuck %s!" , r . URL . Path [ 1 : ] )
fmt . Println ( r . URL )
}
func main ( ) {
http . HandleFunc ( "/" , handler00 )
http . HandleFunc ( "/aa" , handler01 )
http . HandleFunc ( "/bb" , handler02 )
http . ListenAndServe ( ":8080" , nil )
http . HandleFunc ( "/cc" , handler03 )
http . HandleFunc ( "/getSomeJson" , handler04 )
http . HandleFunc ( "/easyui/" , func ( w http . ResponseWriter , r * http . Request ) { // wichtig: "/easyui/", nicht "/easyui"
http . ServeFile ( w , r , r . URL . Path [ 1 : ] )
} )
// Alternative 2
fs := http . FileServer ( http . Dir ( "./easyui/" ) )
http . Handle ( "/easyui2/" , http . StripPrefix ( "/easyui2" , fs ) )
// Alternative 3
go http . ListenAndServe ( ":8082" , http . FileServer ( http . Dir ( "./" ) ) ) // Ein zweiter Server für den Dateizugriff im Hintergrund könnte auch als Dateihandler für jedes File dienen!
http . ListenAndServe ( ":8081" , nil )
}