GopherWeb
Open-source, high-performance web framework for the Golang.
Contents:
Getting started
Install
go get -t github.com/Galagoshin/GopherWeb
Example usage
package main
import (
"fmt"
"github.com/Galagoshin/GoUtils/requests"
"github.com/Galagoshin/GopherWeb/web"
"github.com/Galagoshin/GopherWeb/web/server"
)
func main(){
web.Init()
server.Route("/", []requests.Method{requests.GET}, index)
web.Run()
}
func index(request *requests.Request) (requests.Response, error) {
val := request.Data.Get("field")
return requests.Response{
Body: []byte(fmt.Sprintf("field value is %s", val)),
}, nil
}
After the first launch, GopherWeb will create configuration files.
Web server configuration
server.gconf
#v=1
host: 0.0.0.0
port: 80
domain: localhost
check-hostname: false
enable-ssl: false
crt-file: file.crt
key-file: file.key
host
- The IP address where the web server will run
port
- The port where the web server will run
domain
- Server domain name
check-hostname
- Allows accessing the webserver only with the hostname from the domain
parameter
enable-ssl
- Enable SSL
crt-file
- Path to crt file (works with enable-ssl: true
)
key-file
- Path to key file (works with enable-ssl: true
)
Framework configuration
gopher.gconf
#v=1
live-reload-enabled: true
hot-reload-enabled: true
write-logs: true
debug-level: 0
live-reload-enabled
- Enable live reloads
hot-reload-enabled
- Enable hot reload
write-logs
- Write logs
debug-level
- Set debug level
Developer mode
Developer mode is disabled automatically when you run your application inside a docker container.
To force disable developer mode use the flag --mode prod
You can also enable developer mode inside a docker container with the flag --mode dev
Live reloads
GopherWeb supports live reloads in developer mode. You need to correctly configure the GoLand configuration and build.gconf
.
build.gconf
#v=1
build: build -o gopher_server src/main.go
run: run src/main.go
The run
parameter should run your application using standart go
utility.
GoLand configuration
You need to run main()
in github.com/Galagoshin/GopherWeb/develop/main.go
Hot reload
GopherWeb supports hot reload. If you change any configuration file or content in the views
directory, the web server will be restarted with the new settings.
Routing
Example route
server.Route("/api/info", []requests.Method{requests.GET, requests.POST}, info)
func info(request *requests.Request) (requests.Response, error) {
if request.Method == requests.POST {
//Create info
return requests.Response{
Body: []byte("created info."),
StatusCode: http.StatusCreated,
}, nil
}else if request.Method == requests.GET{
return requests.Response{
Body: []byte("any info."),
StatusCode: 200,
}, nil
}else{
return requests.Response{
Body: []byte("method not allowed."),
StatusCode: http.StatusMethodNotAllowed,
}, nil
}
}
Parametric routes
Example usage
server.Route("/api/user/{id: [0-9]+}", []requests.Method{requests.GET}, user)
func user(request *requests.Request) (requests.Response, error) {
user_id := request.Data.Get("id")
return requests.Response{
Body: []byte(fmt.Sprintf("getting user id %s", user_id)),
}, nil
}
Example without parameter
server.Route("/api/{[A-Za-z0-9]+}/get", []requests.Method{requests.GET}, hander)
Static files
All static files are located in the static/
directory and are available via the /static
route.
Render core
Standart partials
index.html
file in views/
directory.
<head>
<title><%=title%></title>
</head>
Usage in application.
func any_handler(request *requests.Request) (requests.Response, error) {
response := requests.Response{
Body: []byte(render.GetView("index").Render(map[string]string{
"title": "Page title",
})),
StatusCode: 200,
}
return response, nil
}
HTML escaped partials
index.html
file in views/
directory.
<head>
<title><%!title%></title>
</head>
Usage in application.
func any_handler(request *requests.Request) (requests.Response, error) {
response := requests.Response{
Body: []byte(render.GetView("index").Render(map[string]string{
"title": "<script>alert(1);</script>",
})),
StatusCode: 200,
}
return response, nil
}
Includes
In this example, the content of views/path/to/file.html
is set to the current html file.
<head>
<title><%& path/to/file %></title>
</head>
Plugins
GopherWeb supports plugins in .so
files.
With plugins, you can comfortably support multi-version REST API architectures by simply uploading each new version to the plugins
directory. Plugins can also be used for templates. For example, for a unified authorization system.
Example plugin
Plugin code
package main
var Name = "PluginName" //Required field
var Version = "1.0" //Optional
func GetUser() int {
return 1
}
func OnEnable(){ //Optional
//TODO
}
func OnDisable() { //Optional
//TODO
}
Plugin compilation
go build -buildmode=plugin -o plug_1.0.so
Then move plug_1.0.so
to the plugins
directory.
Usage in GopherWeb
plugin, exists := plugins.GetPlugin("PluginName", "1.0") //version can be empty to get latest version
if exists {
symbol, err := plugin.Lookup("GetUser")
if err != nil {
logger.Panic(err)
}
GetUser := symbol.(func() int)
return GetUser() //returns 1
}else{
logger.Error("Plugin is not enabled.")
}