srv.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "text/template"
  4. "fmt"
  5. "net/http"
  6. "encoding/json"
  7. "strings"
  8. )
  9. type (
  10. Page struct {
  11. Tit string
  12. Err string
  13. Url string
  14. Dom string
  15. Lan string
  16. Ver string
  17. Ves string
  18. }
  19. Api struct {
  20. Cod int `json:"code"`
  21. Err string `json:"error"`
  22. Url string `json:"url"`
  23. Mot string `json:"origin"`
  24. New bool `json:"isnew"`
  25. }
  26. Stat struct {
  27. Url string `json:"url"`
  28. Ver string `json:"version"`
  29. }
  30. )
  31. func serv (cnf Config, port int) {
  32. http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
  33. ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
  34. http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
  35. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  36. w.WriteHeader(200)
  37. buf, _ := json.MarshalIndent(&Stat{Url: cnf.domain, Ver: version}, "", " ")
  38. _, _ = w.Write(buf)
  39. })
  40. http.HandleFunc("/api/lolify", func(w http.ResponseWriter, r *http.Request) {
  41. w.Header().Set("Content-Type", "application/json; charset=UTF-8")
  42. w.WriteHeader(200)
  43. res := &Api{Cod: 500, Err: "未対応"}
  44. if r.Method == "POST" {
  45. err := r.ParseForm()
  46. if err != nil {
  47. fmt.Println(err)
  48. res.Err = "失敗"
  49. } else {
  50. if r.PostForm.Get("url") != "" {
  51. addurl := r.PostForm.Get("url")
  52. chkprx := checkprefix(addurl)
  53. chklim := checkcharlim(addurl)
  54. if !chkprx {
  55. res = &Api{Cod: 400, Err: getloc("errfusei", "ja")}
  56. }
  57. if !chklim {
  58. res = &Api{Cod: 400, Err: getloc("errcharlim", "ja")}
  59. }
  60. if chklim && chkprx {
  61. chkfn, key := geturl(addurl, cnf.linkpath, true)
  62. if chkfn != "" {
  63. res = &Api{Cod: 200, Url: cnf.domain + "/" + key, Mot: addurl, New: false}
  64. } else {
  65. res = &Api{Cod: 200, Url: cnf.domain + "/" + insertjson(addurl, cnf.linkpath), Mot: addurl, New: true}
  66. }
  67. }
  68. } else {
  69. res = &Api{Cod: 400, Err: getloc("errurlent", "ja")}
  70. }
  71. }
  72. }
  73. buf, _ := json.MarshalIndent(res, "", " ")
  74. _, _ = w.Write(buf)
  75. })
  76. http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  77. data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
  78. uri := r.URL.Path
  79. cookie, err := r.Cookie("lang")
  80. if err != nil {
  81. data.Lan = "ja"
  82. } else {
  83. data.Lan = cookie.Value
  84. }
  85. // デフォルトページ=未検出
  86. data.Tit = getloc("mikensyutu", data.Lan)
  87. data.Err = getloc("errurlnai", data.Lan)
  88. ftmpl[0] = cnf.webpath + "/view/404.html"
  89. tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
  90. if r.Method == "POST" {
  91. err := r.ParseForm()
  92. if err != nil { fmt.Println(err) }
  93. if r.PostForm.Get("sosin") != "" {
  94. if r.PostForm.Get("newadd") != "" {
  95. addurl := r.PostForm.Get("newadd")
  96. chkprx := checkprefix(addurl)
  97. chklim := checkcharlim(addurl)
  98. if !chkprx || !chklim {
  99. data.Tit = getloc("fuseiurl", data.Lan)
  100. if !chkprx {
  101. data.Err = getloc("errfusei", data.Lan)
  102. } else if !chklim {
  103. data.Err = getloc("errcharlim", data.Lan)
  104. }
  105. } else {
  106. chkfn, _ := geturl(addurl, cnf.linkpath, true)
  107. if chkfn != "" {
  108. http.Redirect(w, r, addurl, http.StatusSeeOther)
  109. return
  110. } else {
  111. data.Url = insertjson(addurl, cnf.linkpath)
  112. data.Dom = cnf.domain
  113. data.Tit = getloc("tansyukuzumi", data.Lan)
  114. ftmpl[0] = cnf.webpath + "/view/submitted.html"
  115. }
  116. }
  117. } else {
  118. data.Err = getloc("errurlent", data.Lan)
  119. }
  120. } else if r.PostForm.Get("langchange") != "" {
  121. cookie, err := r.Cookie("lang")
  122. if err != nil || cookie.Value == "ja" {
  123. http.SetCookie(w, &http.Cookie{Name: "lang", Value: "en", MaxAge: 31536000, Path: "/"})
  124. } else {
  125. http.SetCookie(w, &http.Cookie{Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
  126. }
  127. http.Redirect(w, r, "/", http.StatusSeeOther)
  128. return
  129. }
  130. } else { // r.Method == "GET"
  131. if uri == "/" {
  132. data.Tit = getloc("top", data.Lan)
  133. ftmpl[0] = cnf.webpath + "/view/index.html"
  134. } else {
  135. red, _ := geturl(uri[1:], cnf.linkpath, false)
  136. if red != "" {
  137. http.Redirect(w, r, red, http.StatusSeeOther)
  138. return
  139. }
  140. }
  141. } // r.Method
  142. tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
  143. tmpl.Execute(w, data)
  144. data = nil
  145. })
  146. fmt.Println(fmt.Sprint("http://127.0.0.1:", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"))
  147. http.ListenAndServe(fmt.Sprint(":", port), nil)
  148. }