func main() { router := jas.NewRouter(new(Hello), new(World)) router.BasePath = "/v1/" fmt.Println(router.HandledPaths(true)) router2 := jas.NewRouter(new(World)) router2.BasePath = "/v2/" //output: `GET /v1/hello` http.Handle(router.BasePath, router) http.Handle(router2.BasePath, router2) fmt.Println(router2.HandledPaths(true)) http.ListenAndServe(":8080", nil) }
func main() { flag.Parse() //MODEL sporocilo.RegisterDb() sporocilo.UstvariTabeloSporocila() //REST router := jas.NewRouter( new(items.Items), // last 25 items with :offset new(items.ItemId), // item operations [like, unlike, read, unread] ) router.BasePath = "/v1/" router.EnableGzip = true fmt.Println(router.HandledPaths(true)) http.Handle(router.BasePath, router) rest.RegisterWS() http.HandleFunc("/feed", rest.Atom) // Listen fmt.Println("Listening on", *host_ip) if err := http.ListenAndServe(*host_ip, nil); err != nil { log.Fatal("ListenAndServe:", err) } }
func main() { router := jas.NewRouter(new(Hello)) router.BasePath = "/v1/" fmt.Println(router.HandledPaths(true)) http.Handle(router.BasePath, router) http.ListenAndServe(":8080", nil) }
func main() { router := jas.NewRouter(new(Hello)) router.BasePath = "/v1/" fmt.Println(router.HandledPaths(true)) //output: `GET /v1/hello` http.Handle(router.BasePath, router) // I changed the port because I 8080 is in use http.ListenAndServe(":8081", nil) }
// RegisterAPI invokes http.Handle() with a JAS router using the // default net/http server. It will respond to any URL "<prefix>/api". func RegisterAPI(prefix string) { // Initialize a JAS router with appropriate attributes. router := jas.NewRouter(new(Api)) router.BasePath = prefix // Disable automatic internal error logging. router.InternalErrorLogger = nil l.Debug("API paths:\n", router.HandledPaths(true)) // Handle "<prefix>/api/". Note that it must begin and end with /. http.Handle(path.Join(prefix, "api")+"/", router) }
////////////////main func main() { router := jas.NewRouter(new(Hello), new(Radio), new(RadioId)) router.BasePath = "/" fmt.Println(router.HandledPaths(true)) //output: `GET /v1/hello` http.Handle(router.BasePath, router) /* radio_router := jas.NewRouter(new(Radio)) radio_router.BasePath = "/" // http.Handle("/radio",jas.NewRouter(new(Radio))) http.Handle(radio_router.BasePath,radio_router) */ http.ListenAndServe(":8080", nil) }
// RegisterAPI invokes http.Handle() with a JAS router using the // default net/http server. It will respond to any URL "<prefix>/api". func RegisterAPI(prefix string) { // Initialize a JAS router with appropriate attributes. router := jas.NewRouter(new(Api)) router.BasePath = path.Join("/", prefix) // Disable automatic internal error logging. router.InternalErrorLogger = nil l.Debug("API paths:\n", router.HandledPaths(true)) // Seed the random number generator with the current Unix // time. This is not random, but it should be Good Enough. rand.Seed(time.Now().Unix()) // Handle "<prefix>/api/". Note that it must begin and end with /. http.Handle(path.Join("/", prefix, "api")+"/", router) }
func main() { loadConfiguration() router := jas.NewRouter(new(routes.Machines)) // Router configuration router.EnableGzip = true router.HijackWrite = responseMessage models.SetupConnectionPool(globalConfiguration.Database.Host, globalConfiguration.Database.Port, globalConfiguration.Database.Password, globalConfiguration.Database.MaxConnections, globalConfiguration.Database.IdleTimeout) router.BasePath = "/v1/" router.DisableAutoUnmarshal = true log.Println(router.HandledPaths(true)) http.Handle(router.BasePath, router) log.Println("Starting Web Server On Port: ", globalConfiguration.Port) http.ListenAndServe(":"+strconv.Itoa(globalConfiguration.Port), nil) }
func webUI() { tpl, err := template.New("XML translation").Parse(html1) if err != nil { panic(err) } requestMain := func(w http.ResponseWriter, req *http.Request) { fmt.Println("main page") files, err := ListSubDir(".") err = tpl.ExecuteTemplate(w, "main", files) if err != nil { log.Fatal(err) } } requestFile := func(w http.ResponseWriter, req *http.Request) { fmt.Println("file page") files, err := ListSubDir(".") err = tpl.ExecuteTemplate(w, "file", files) if err != nil { log.Fatal(err) } } requestUpload := func(w http.ResponseWriter, req *http.Request) { fmt.Println("upload file") if "POST" != req.Method { files, err := ListSubDir(".") err = tpl.ExecuteTemplate(w, "main", files) if err != nil { log.Fatal(err) } } file, head, err := req.FormFile("file") if err != nil { fmt.Println(err) return } defer file.Close() fW, err := os.Create(head.Filename) if err != nil { fmt.Println("文件创建失败") return } defer fW.Close() _, err = io.Copy(fW, file) if err != nil { fmt.Println("文件保存失败") return } fmt.Println(head.Filename) tablekey := "mail_set" prefix := strings.Split(head.Filename, ".") if len(prefix) > 0 { if prefix[0] == "male" || prefix[0] == "female" { tablekey += "." + prefix[0] } } mailAry, err := tool.ReadFileMails(head.Filename, tablekey) if err != nil { mailAry = append(mailAry, fmt.Sprintf("%v", err)) } err = tpl.ExecuteTemplate(w, "tipary", mailAry) if err != nil { log.Fatal(err) } } requestMd5 := func(w http.ResponseWriter, req *http.Request) { fmt.Println("calc MD5") err := req.ParseForm() if err != nil { log.Fatal(err) } if len(req.Form["name"]) < 1 { io.WriteString(w, "参数错误!\n") return } name := template.HTMLEscapeString(req.Form.Get("name")) nameMd5 := ToMd5(name) err = tpl.ExecuteTemplate(w, "tip", nameMd5) if err != nil { log.Fatal(err) } } requestSendMail := func(w http.ResponseWriter, req *http.Request) { fmt.Println("send mail") err := req.ParseForm() if err != nil { log.Fatal(err) } keyLen := len(req.Form["key"]) //numLen := len(req.Form["nums"]) titleLen := len(req.Form["title"]) contentLen := len(req.Form["content"]) if keyLen < 1 || titleLen < 1 || contentLen < 1 { io.WriteString(w, "参数错误!\n") return } nums := SEND_MAIL_MIN_NUMS user := template.HTMLEscapeString(req.Form.Get("user")) passwd := template.HTMLEscapeString(req.Form.Get("passwd")) host := template.HTMLEscapeString(req.Form.Get("host")) recvs := template.HTMLEscapeString(req.Form.Get("recvs")) key := template.HTMLEscapeString(req.Form.Get("key")) numstr := template.HTMLEscapeString(req.Form.Get("nums")) title := template.HTMLEscapeString(req.Form.Get("title")) content := template.HTMLEscapeString(req.Form.Get("content")) if key != SEND_MAIL_KEY { io.WriteString(w, "暂无权限!\n") return } tmpNums, err := strconv.Atoi(numstr) if err == nil && tmpNums >= SEND_MAIL_MIN_NUMS && tmpNums <= SEND_MAIL_MAX_NUMS { nums = tmpNums } resTip := string("已发送") if user != "" && passwd != "" && host != "" && recvs != "" { recvsAry := strings.Split(recvs, ";") _ = tool.SendMailCustomRequest(host, user, passwd, title, content, recvsAry, nums) } else { //tool.TestSendMailRequest(title, content, nums) //resTip = string("不支持默认参数发送") //tool.ReleaseSendMailRequest(title, content, "mail_test", nums) tool.TimerSendMailRequest(title, content, "mail_set", nums) } err = tpl.ExecuteTemplate(w, "tip", resTip) if err != nil { log.Fatal(err) } } /* requestCpu := func(w http.ResponseWriter, req *http.Request) { dir, err := ioutil.ReadDir(".") if err != nil { io.WriteString(w, "非法目录\n") } start := time.Now() startSec := start.UnixNano() / 1000000 wg := new(sync.WaitGroup) for _, fi := range dir { if !fi.IsDir() && strings.HasPrefix(fi.Name(), "fanyi_") { doTrans("", fi.Name(), false, wg) } } wg.Wait() end := time.Now() endSec := end.UnixNano() / 1000000 result := fmt.Sprintf("翻译文件消耗%d毫秒\n", int(endSec-startSec)) err = tpl.ExecuteTemplate(w, "tip", result) if err != nil { log.Fatal(err) } } */ /* http.HandleFunc("/", goHandler) http.HandleFunc("/heap", heapHandler) http.HandleFunc("/thread", threadHandler) http.HandleFunc("/block", blockHandler) */ http.HandleFunc("/main", requestMain) http.HandleFunc("/upload", requestUpload) http.HandleFunc("/md5", requestMd5) http.HandleFunc("/send", requestSendMail) http.HandleFunc("/file", requestFile) http.HandleFunc("/redis", redisHandle) // dir := "./" // http.Handle("/", http.FileServer(http.Dir(dir))) router := jas.NewRouter(new(Hello), new(Weixin), new(Baidu)) router.BasePath = "/" // fmt.Println(router.HandledPaths(true)) //output: `GET /v1/hello` http.Handle(router.BasePath, router) err = http.ListenAndServe(":80", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }