Install Libraries
Make sure Git is installed on your machine and in your system’s PATH. Install the packages to your $GOPATH with the go tool from shell:
$ go get -u github.com/gorilla/mux
$ go get -u github.com/gorilla/handlers
Web APIs
In src folder, create new folder named apis. In this folder, create new web folder named demoapi. In demoapi folder, create new apis as below:
API 1
In demoapi folder, create new go file named api1.api.go as below:
package demoapi
import (
"fmt"
"net/http"
)
func Demo1API(response http.ResponseWriter, request *http.Request) {
fmt.Fprint(response, "Demo 1 API")
}
API 2
In demoapi folder, create new go file named api2.api.go as below:
package demoapi
import (
"fmt"
"net/http"
)
func Demo2API(response http.ResponseWriter, request *http.Request) {
fmt.Fprint(response, "Demo 2 API")
}
API 3
In demoapi folder, create new go file named api3.api.go as below:
package demoapi
import (
"fmt"
"net/http"
)
func Demo3API(response http.ResponseWriter, request *http.Request) {
fmt.Fprint(response, "Demo 3 API")
}
Middlewares
In src folder, create new folder named middlewares. In this folder, create new middlewares as below:
Browser Middleware
In middlewares folder, create new folder named browsermiddleware. In browsermiddleware folder, create new go file named browser.middleware.go as below:
package browsermiddleware
import (
"fmt"
"net"
"net/http"
)
func BrowserMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
fmt.Println("url: ", request.URL)
ip, port, _ := net.SplitHostPort(request.RemoteAddr)
fmt.Println("ip: " + ip)
fmt.Println("port: " + port)
handler.ServeHTTP(response, request)
})
}
Log Middleware
In middlewares folder, create new folder named logmiddleware. In logmiddleware folder, create new go file named log.middleware.go as below:
package logmiddleware
import (
"fmt"
"net/http"
"time"
)
func LogMiddleware(handler http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
today := time.Now()
fmt.Println("date: " + today.Format("01/02/2006 15:04:05"))
handler.ServeHTTP(response, request)
})
}
Structure of Project
Run Application
In src folder, create new file named main.go as below and use go run main.go command to run program:
package main
import (
"apis/demoapi"
"fmt"
"middlewares/browsermiddleware"
"middlewares/logmiddleware"
"net/http"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.Handle("/api/demo/demo1", browsermiddleware.BrowserMiddleware(http.HandlerFunc(demoapi.Demo1API))).Methods("GET")
router.Handle("/api/demo/demo2", logmiddleware.LogMiddleware(http.HandlerFunc(demoapi.Demo2API))).Methods("GET")
router.Handle("/api/demo/demo3", logmiddleware.LogMiddleware(browsermiddleware.BrowserMiddleware(http.HandlerFunc(demoapi.Demo3API)))).Methods("GET")
err := http.ListenAndServe(":3000", router)
if err != nil {
fmt.Println(err)
}
}
Test Web API
Test API 1
Use PostMan Tool test api 1 web api with url: http://localhost:3000/api/demo/demo1
Output in Console
url: /api/demo/demo1
ip: ::1
port: 38600
Test API 2
Use PostMan Tool test api 2 web api with url: http://localhost:3000/api/demo/demo2
Output in Console
date: 07/14/2019 16:25:39
Test API 3
Use PostMan Tool test api 3 web api with url: http://localhost:3000/api/demo/demo3
Output in Console
date: 07/14/2019 16:27:17
url: /api/demo/demo3
ip: ::1
port: 38615