12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package main
- import (
- "flag"
- "html/template"
- "log"
- "net/http"
- "time"
- )
- var host = flag.String("h", "localhost", "host")
- var port = flag.String("p", "80", "port")
- var url = flag.String("b", "", "API URL")
- var app Application
- var errorPage = template.Must(template.ParseFiles("./html/error.html"))
- // ServeIndex serves index page.
- func serveIndex(w http.ResponseWriter, r *http.Request) {
- t := template.Must(template.ParseFiles("./html/index.html"))
- t.Execute(w, app.Languages)
- }
- // DisplaySearchResults displays search results as HTML page.
- func displaySearchResults(w http.ResponseWriter, r *http.Request) {
- var info SearchInfo
- info.GetInfoFromRequest(r)
- if info.Phrase != "" {
- var searchResults SearchResults
- if info.LanguageCode == "" || app.checkLanguage(info.LanguageCode) {
- searchResults = app.search(app.GetTMs(info.LanguageCode), info.Phrase)
- info.ResultsServed = searchResults.TotalResults
- writeLog(info)
- } else {
- errorPage.Execute(w, "Language not valid!")
- return
- }
- if len(searchResults.Results) > 0 {
- t := template.Must(template.New("results.html").ParseFiles("./html/results.html"))
- t.Execute(w, searchResults)
- } else {
- errorPage.Execute(w, "Nothing found!")
- }
- } else {
- errorPage.Execute(w, "You need to enter search phrase!")
- }
- }
- // DisplayTMs displays TMs as HTML page.
- func displayTMs(w http.ResponseWriter, r *http.Request) {
- var info SearchInfo
- info.GetInfoFromRequest(r)
- var TMList []TM
- if info.LanguageCode == "" || app.checkLanguage(info.LanguageCode) {
- TMList = app.GetTMs(info.LanguageCode)
- info.ResultsServed = len(TMList)
- writeLog(info)
- } else {
- errorPage.Execute(w, "Language not valid!")
- return
- }
- if len(TMList) > 0 {
- t := template.Must(template.New("tms.html").ParseFiles("./html/tms.html"))
- t.Execute(w, TMList)
- } else {
- errorPage.Execute(w, "No TMs to display!")
- }
- }
- func main() {
- flag.Parse()
- app.setBaseURL(*url)
- if app.BaseURL == "" {
- log.Fatalln("Can't do anything without URL to API")
- }
- status, err := app.login()
- if !status || err != nil {
- log.Fatalf("Couldn't log in: %s", err)
- }
- app.loadLanguages()
- app.Delay = time.Duration(20 * time.Second)
- hostname := *host + ":" + *port
- http.HandleFunc("/", serveIndex)
- http.HandleFunc("/q", displaySearchResults)
- http.HandleFunc("/tms", displayTMs)
- http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
- log.Fatal(http.ListenAndServe(hostname, nil))
- }
|