First Commit
This commit is contained in:
137
main.go
Normal file
137
main.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jbowtie/gokogiri/xml"
|
||||
"github.com/jbowtie/ratago/xslt"
|
||||
)
|
||||
|
||||
type TOCNode struct {
|
||||
Name string `json:"name"`
|
||||
File string `json:"file,omitempty"`
|
||||
Key string `json:"id"`
|
||||
Children []TOCNode `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
type CarInfo struct {
|
||||
SoftLink url.Values `json:"softlink"`
|
||||
ToC []TOCNode `json:"toc"`
|
||||
Values map[string]url.Values `json:"tocpath"`
|
||||
}
|
||||
|
||||
var indexList CarInfo
|
||||
|
||||
func main() {
|
||||
r := mux.NewRouter()
|
||||
r.PathPrefix("/static/").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("html/static"))))
|
||||
r.HandleFunc("/cars", HomeHandler)
|
||||
r.HandleFunc("/{car}", CarHandler)
|
||||
r.HandleFunc("/file/{page}", PageHandler)
|
||||
r.HandleFunc("/img/{file}", ImgHandler)
|
||||
r.PathPrefix("/").Handler(http.FileServer(http.Dir("html")))
|
||||
err := http.ListenAndServe(":8080", r)
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
func HomeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
body, _ := ioutil.ReadFile("Manuals/vehicles.json")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(body)
|
||||
|
||||
}
|
||||
|
||||
func CarHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
folder := vars["car"]
|
||||
indexList = CarInfo{}
|
||||
file, err := os.Open(path.Join("Manuals", folder+".toc"))
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
empty := []TOCNode{TOCNode{Name: "Not Available", Key: "1"}}
|
||||
json.NewEncoder(w).Encode(empty)
|
||||
return
|
||||
}
|
||||
json.NewDecoder(file).Decode(&indexList)
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
writer := gzip.NewWriter(w)
|
||||
json.NewEncoder(writer).Encode(indexList.ToC)
|
||||
writer.Close()
|
||||
|
||||
}
|
||||
|
||||
func PageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
img := vars["page"]
|
||||
style, _ := xml.ReadFile(path.Join("Manuals", "techauth.xsl"), xml.StrictParseOption)
|
||||
stylesheet, _ := xslt.ParseStylesheet(style, path.Join("Manuals", "techauth.xsl"))
|
||||
|
||||
input, err := xml.ReadFile(path.Join("Manuals", "files", img), xml.StrictParseOption)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
params := make(map[string]interface{})
|
||||
params["TOCPath"] = indexList.Values[img]["TOCPath"][0]
|
||||
params["modelyear"] = indexList.Values[img]["year"][0]
|
||||
params["modelname"] = indexList.Values[img]["platform"][0]
|
||||
params["ENG"] = indexList.Values[img]["ENG"][0]
|
||||
params["locale"] = indexList.Values[img]["lang"][0]
|
||||
params["vid"] = indexList.Values[img]["VID"][0]
|
||||
params["contentPath"] = "img/"
|
||||
output, err := stylesheet.Process(input, xslt.StylesheetOptions{false, params})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
if strings.Contains(output, "REPLACEME") {
|
||||
reg := regexp.MustCompile(`REPLACEME="(\d+?)"`)
|
||||
matches := reg.FindAllStringSubmatch(output, -1)
|
||||
for _, item := range matches {
|
||||
location := indexList.SoftLink[item[1]]
|
||||
if len(location) > 1 {
|
||||
string2 := FindToc(indexList.ToC, location[0])
|
||||
place, _ := json.Marshal(string2)
|
||||
output = strings.Replace(output, item[0], fmt.Sprintf(`v-on:click="app.switchToc('%s')"`, strings.Replace(string(place), `"`, `"`, -1)), -1)
|
||||
} else {
|
||||
output = strings.Replace(output, item[0], fmt.Sprintf(`v-on:click="app.index('%s')"`, location[0]), -1)
|
||||
}
|
||||
}
|
||||
}
|
||||
w.Write([]byte(output))
|
||||
}
|
||||
|
||||
func ImgHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
img := vars["file"]
|
||||
img = strings.Replace(img, ".tif", ".jpg", -1)
|
||||
http.ServeFile(w, r, path.Join("Manuals", "files", "img", img))
|
||||
}
|
||||
|
||||
func FindToc(toc []TOCNode, file string) []string {
|
||||
var list []string
|
||||
for _, item := range toc {
|
||||
if item.File == file {
|
||||
list = append([]string{item.Key}, list...)
|
||||
}
|
||||
list2 := FindToc(item.Children, file)
|
||||
if len(list2) > 0 {
|
||||
list = append([]string{item.Key}, list2...)
|
||||
}
|
||||
|
||||
}
|
||||
return list
|
||||
}
|
||||
Reference in New Issue
Block a user