http.FileServer doesn work with my custom router

package router

import (

"errors"

"net/http"

"regexp"

)

type route struct {

Path string

Method string

Handler Handler

}

type Router struct {

routes []route

}

type Handler func(http.ResponseWriter, *http.Request)

//add method, path, handler func in to slice of route in struct Router

func (r *Router) AddRoute(method, path string, handler Handler) {

r.routes = append(r.routes, route{Path: path, Method: method, Handler: handler})

}

// methods: Get, Post, Put, Delete of router created for use in other package

func (r *Router) Get(path string, handler Handler) {

r.AddRoute("GET", path, handler)

}

func (r *Router) Post(path string, handler Handler) {

r.AddRoute("POST", path, handler)

}

func (r *Router) Put(path string, handler Handler) {

r.AddRoute("PUT", path, handler)

}

func (r *Router) Delete(path string, handler Handler) {

r.AddRoute("DELETE", path, handler)

}

// from request of client we find handler function if it is exist in Router

func (r *Router) getHandler(path, method string) (Handler, error) {

for _, route := range r.routes {

regex := regexp.MustCompile(route.Path)

if regex.MatchString(path) && route.Method == method {

return route.Handler, nil

}

}

return nil, errors.New("not foundm")

}

// for implementing http.Handler interface

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {

path := req.URL.Path

method := req.Method

handler, err := r.getHandler(path, method)

if err != nil {

http.NotFound(w, req)

return

}

handler(w, req)

}

// if call this function you get type *Router, and use all method of Router type

func NewRouter() *Router {

return &Router{}

}

code above my custom router

package main

import (
	router "asciiWeb/back"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"os/exec"
	"strings"
	"text/template"
)

var tmp *template.Template

func init() {
	tmp = template.Must(template.ParseGlob("front/*.html"))
}

func main() {
	route := router.NewRouter()
	route.Get("/api/home", home)
	route.Get("/api/index", index)
	route.Post("/api/convert", convert)
	route.Post("/api/upload", upload)
	http.Handle("/api/", route)
	http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
	fmt.Println("http://localhost:8080")
	if err := http.ListenAndServe(":8080", nil); err != nil {
		return
	}
}

func home(w http.ResponseWriter, req *http.Request) {
	if err := tmp.ExecuteTemplate(w, "index.html", nil); err != nil {
		return
	}
}
func convert(w http.ResponseWriter, req *http.Request) {
	err := req.ParseForm()
	if err != nil {
		return
	}
	font := "--font=" + req.Form.Get("fs")
	text := req.Form.Get("name")
	res := ""
	for _, word := range strings.Fields(text) {
		cmd := exec.Command("./ascii", font, word)
		output, err := cmd.Output()
		if err != nil {
			fmt.Println("********", err)
			return
		}
		res += string(output) + "\n"
	}
	if err := tmp.ExecuteTemplate(w, "index.html", res); err != nil {
		return
	}
}

if i run this code, my css file doesn’t connect with html.
if i use default go’s mux everithing is okay. why my router doesn’t work correctly

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.