uAdmin Tutorial Part 11 - Accessing an HTML fileΒΆ
In this part, we will talk about establishing a connection to the HTTP Handler, setting the URL path name, and executing an HTML file.
Go to handler.go inside the handlers folder with the following codes below:
package handlers
import (
"net/http"
"strings"
)
// HTTPHandler !
func HTTPHandler(w http.ResponseWriter, r *http.Request) {
// r.URL.Path creates a new path called /http_handler
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/http_handler")
}
Establish a connection in the main.go to the handlers by using http.HandleFunc. It should be placed after the uadmin.Register and before the StartServer.
import (
"net/http"
// Specify the username that you used inside github.com folder
"github.com/username/todo/api"
"github.com/username/todo/models"
// Import this library
"github.com/username/todo/handlers"
"github.com/uadmin/uadmin"
)
func main() {
// Some codes
// HTTP UI Handler
http.HandleFunc("/http_handler/", handlers.HTTPHandler)
}
Create a file named todo_handler.go inside the handlers folder with the following codes below:
package handlers
import (
"html/template"
"net/http"
"strings"
"github.com/uadmin/uadmin"
)
// TodoHandler !
func TodoHandler(w http.ResponseWriter, r *http.Request) {
// r.URL.Path creates a new path called /todo_html
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/todo")
// TodoList field inside the Context that will be used in Golang
// HTML template
type Context struct {
TodoList []map[string]interface{}
}
// Assigns Context struct to the c variable
c := Context{}
// Pass TodoList data object to the specified HTML path
uadmin.HTMLContext(w, c, "views/todo.html")
}
Finally, add this piece of code in the handler.go shown below. This will establish a communication between the HTTPHandler and the TodoHandler.
// HTTPHandler !
func HTTPHandler(w http.ResponseWriter, r *http.Request) {
// r.URL.Path creates a new path called /http_handler
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/http_handler")
if strings.HasPrefix(r.URL.Path, "/todo") {
TodoHandler(w, r)
return
}
}
Now run your application, go to http_handler/todo path and see what happens.

In the next part, we will discuss about fetching the records in the API and migrating the data from API to HTML that will display the records using Go template.