Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vkuznet/wt
Collection of WebTools (wt) for Go server
https://github.com/vkuznet/wt
Last synced: 30 days ago
JSON representation
Collection of WebTools (wt) for Go server
- Host: GitHub
- URL: https://github.com/vkuznet/wt
- Owner: vkuznet
- License: mit
- Created: 2022-01-08T15:27:15.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-11T19:06:57.000Z (about 3 years ago)
- Last Synced: 2024-10-30T06:27:36.103Z (3 months ago)
- Language: Go
- Size: 18.6 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### WebTools (wt)
Collection of useful set of WebTools (wt) for building Go HTTP server
applications. For example:```
package mainimport (
"flag"
"log"
"net/http""github.com/gorilla/mux"
wt "github.com/vkuznet/wt"
)func main() {
var verbose int
flag.IntVar(&verbose, "verbose", 0, "verbosity level")
flag.Parse()
// create new server configuration
config := wt.NewServerConfig()
// set appropriate flags, see more in wt.ServerConfiguration
config.Templates = "static/tmpl" // location of static template area
config.Base = "/" // our server base path
config.Verbose = 1 // verbosity level// start the server
wt.StartServer(config, srvRouter(config))
}// AuthFunc provides authentication for incoming HTTP request
func AuthFunc(h http.Header) error {
log.Println("authFunc")
return nil
}// AuthFunc provides validation for incoming HTTP request
func ValidateFunc(h http.Header) error {
log.Println("validateFunc")
return nil
}// create our HTTP router
func srvRouter(serverConfig wt.ServerConfiguration) *mux.Router {
base := serverConfig.Base
router := mux.NewRouter()
router.StrictSlash(true) // to allow /route and /route/ end-points
router.HandleFunc(wt.BasePath(base, "/"), wt.HomeHandler).Methods("GET")// this is for displaying the QR code on /qr end point
// and static area which holds user's images
log.Println("server static area", serverConfig.StaticDir)
fileServer := http.StripPrefix("/static/", http.FileServer(http.Dir(serverConfig.StaticDir)))
router.PathPrefix(wt.BasePath(base, "/css/{file:[0-9a-zA-Z-\\.]+}")).Handler(fileServer)// define different middleware layers
router.Use(wt.LoggingMiddleware)
router.Use(wt.AuthMiddleware(AuthFunc))
router.Use(wt.ValidateMiddleware(ValidateFunc))
router.Use(wt.LimitMiddleware)
router.Use(wt.CorsMiddleware)return router
}
```The wt provides different middleware layers, and flexible configuration
to setup different servers, HTTP, HTTPs, HTTPs with let's encrypt, etc.