之前一直使用百度自帶的網址導航,但是最近發現不能添加類别目錄了。是以想找個差不多的導航網址,一直沒有找到。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLwkDO5IDO0EjM0ITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
要麼是各種彈廣告,要麼是不能自己添加網址。于是乎,就想自己做一個。
我也不知道有多少人跟我一樣喜歡用網址導航,這裡就介紹一下怎麼樣來用Go語言做一個極簡風格的網址導航。需要的可以直接拿走使用。
源代碼:https://gitee.com/fcsvr/navi
1.目錄結構
目錄下面主要的檔案就是css,img,js,index.html,navi.go
- css,img,js這三個目錄就是web程式設計常用的一些資源檔案
- navi.go是web服務主程式
- index.html是首頁面
2.navi.go
navi.go是Go伺服器主程式,代碼如下,其實就是很簡單的一段Go Web伺服器的代碼,很容易了解。
用Go來做伺服器就是很友善,不用配置什麼Nginx,Apache,幾行代碼直接運作。
package main
import (
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"regexp"
"time"
)
//定義路由函數數組
var mux map[string]func(http.ResponseWriter, *http.Request)
type Myhandler struct{}
type home struct {
Title string
}
const (
Js_Dir = "./js/"
Css_Dir = "./css/"
Img_Dir = "./img/"
)
func main() {
//定義http服務結構
server := http.Server{
Addr: ":8002",
Handler: &Myhandler{},
ReadTimeout: 100 * time.Second,
}
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/"] = index
mux["/js"] = jsFile
mux["/css"] = cssFile
mux["/img"] = imgFile
fmt.Println("Hello, this is fancygo navi!")
go server.ListenAndServe()
//設定sigint信号
close := make(chan os.Signal, 1)
signal.Notify(close, os.Interrupt, os.Kill)
<-close
fmt.Println("Bye, fancygo webgame close")
}
func (*Myhandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h, ok := mux[r.URL.String()]; ok {
h(w, r)
return
}
if ok, _ := regexp.MatchString("/css/", r.URL.String()); ok {
http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))).ServeHTTP(w, r)
} else if ok, _ := regexp.MatchString("/js/", r.URL.String()); ok {
http.StripPrefix("/js/", http.FileServer(http.Dir("./js/"))).ServeHTTP(w, r)
} else if ok, _ := regexp.MatchString("/img/", r.URL.String()); ok {
http.StripPrefix("/img/", http.FileServer(http.Dir("./img/"))).ServeHTTP(w, r)
}
}
func index(w http.ResponseWriter, r *http.Request) {
title := home{Title: "fancygo navi"}
t, _ := template.ParseFiles("index.html")
t.Execute(w, title)
}
func jsFile(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/js/", http.FileServer(http.Dir("./js/"))).ServeHTTP(w, r)
}
func cssFile(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/css/", http.FileServer(http.Dir("./css/"))).ServeHTTP(w, r)
}
func imgFile(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/img/", http.FileServer(http.Dir("./img/"))).ServeHTTP(w, r)
}
3.index.html
index.html裡面寫死了各種網站的網址,可以根據自己的需要修改
本來也想過再也個配置工具,用來配網址名稱,再寫到資料庫中。。。
後來發現自己用的話還不如就直接修改index.html源檔案,友善簡潔
上一段源代碼
<body
<div class="container" id="container"
<aside class="left-bar" id="leftBar"
<div class="title"
<pFancyGo導航</p
</div
<nav class="nav"
<ul class="nav-item" id="navItem"
<li<a href="#normal" class="active"常用</a</li
<li<a href="#study"學習</a</li
<li<a href="#live"生活</a</li
<li<a href="#server"server</a</li
<li<a href="#linux"linux</a</li
<li<a href="#tool"工具</a</li
<li<a href="#go"go</a</li
<li<a href="#go-book"go書籍</a</li
<li<a href="#go-net"go網絡</a</li
<li<a href="#go-game"go遊戲</a</li
<li<a href="#devops"運維</a</li
<li<a href="#security"網絡安全</a</li
<li<a href="#crypto"數學和密碼</a</li
<li<a href="#programe"通用程式設計</a</li
<li<a href="#astronomy"天文</a</li
<li<a href="#recruit"招聘考試</a</li
</ul
</nav
</aside
這一段是配置網站的類配
<section class="main"
<div id="mainContent"
<!-- 常用 --
<div class="box"
<a href="#" name="normal"</a
<div class="sub-category"
<div class="iconword"常用</div
</div
<div
<a target="_blank" href="http://www.fancygo.net/"<div class="item"<div class="no-logo"FancyGo</div<div class="desc"FancyGo自己的部落格</ div</div</a
<a target="_blank" href="https://www.zhihu.com/"<div class="item"<div class="no-logo"知乎</div<div class="desc"知乎</div</div</a
<a target="_blank" href="https://www.baidu.com/"<div class="item"<div class="no-logo"百度</div<div class="desc"百度</div</div</a
<a target="_blank" href="https://www.qq.com/"<div class="item"<div class="no-logo"騰訊</div<div class="desc"騰訊</div</div</a
<a target="_blank" href="https://www.sina.com.cn/"<div class="item"<div class="no-logo"新浪</div<div class="desc"新浪</div</div</a
<a target="_blank" href="https://www.zhibo8.cc/"<div class="item"<div class="no-logo"直播吧</div<div class="desc"直播吧</div</div</a
</div
</div
然後下面都是為每個類别配置不同的網址
4.運作
有兩種運作的方式
- 直接再本地用浏覽器打開index.html檔案
- go build 編譯後,運作web伺服器後,就可以在外網打開了
5.大家可以如下連結預覽一下
http://fancygo.net:8002/