API機能性
このコミットが含まれているのは:
コミット
a7ebc09f97
|
@ -1,5 +1,6 @@
|
|||
# 2.1.0(未公開)
|
||||
* ローカライズは関数化
|
||||
* API機能性
|
||||
|
||||
# 2.0.2
|
||||
* Makefileでの「make install」部分を修正
|
||||
|
|
81
srv.go
81
srv.go
|
@ -4,21 +4,79 @@ import (
|
|||
"text/template"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
Tit string
|
||||
Err string
|
||||
Url string
|
||||
Dom string
|
||||
Lan string
|
||||
Ver string
|
||||
}
|
||||
type (
|
||||
Page struct {
|
||||
Tit string
|
||||
Err string
|
||||
Url string
|
||||
Dom string
|
||||
Lan string
|
||||
Ver string
|
||||
}
|
||||
Api struct {
|
||||
Cod int `json:"code"`
|
||||
Err string `json:"error"`
|
||||
Url string `json:"url"`
|
||||
Mot string `json:"origin"`
|
||||
New bool `json:"isnew"`
|
||||
}
|
||||
)
|
||||
|
||||
func serv (cnf Config, port int) {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
|
||||
|
||||
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(200)
|
||||
buf, _ := json.MarshalIndent(&Api{Cod: 200}, "", " ")
|
||||
_, _ = w.Write(buf)
|
||||
})
|
||||
|
||||
http.HandleFunc("/api/lolify", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(200)
|
||||
uri := r.URL.Path
|
||||
fmt.Println(uri)
|
||||
res := &Api{Cod: 500, Err: "未対応"}
|
||||
if r.Method == "POST" {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
res = &Api{Cod: 500, Err: "失敗"}
|
||||
} else {
|
||||
if r.PostForm.Get("url") != "" {
|
||||
addurl := r.PostForm.Get("url")
|
||||
chkprx := checkprefix(addurl)
|
||||
chklim := checkcharlim(addurl)
|
||||
if !chkprx {
|
||||
res = &Api{Cod: 400, Err: getloc("errfusei", "ja")}
|
||||
}
|
||||
if !chklim {
|
||||
res = &Api{Cod: 400, Err: getloc("errcharlim", "ja")}
|
||||
}
|
||||
|
||||
if chklim && chkprx {
|
||||
chkfn, key := geturl(addurl, cnf.linkpath, true)
|
||||
if chkfn != "" {
|
||||
res = &Api{Cod: 200, Url: cnf.domain + "/" + key, Mot: addurl, New: false}
|
||||
} else {
|
||||
res = &Api{Cod: 200, Url: cnf.domain + "/" + insertjson(addurl, cnf.linkpath), Mot: addurl, New: true}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res = &Api{Cod: 400, Err: getloc("errurlent", "ja")}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buf, _ := json.MarshalIndent(res, "", " ")
|
||||
_, _ = w.Write(buf)
|
||||
})
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := &Page{Ver: version}
|
||||
cookie, err := r.Cookie("lang")
|
||||
|
@ -62,8 +120,7 @@ func serv (cnf Config, port int) {
|
|||
http.Redirect(w, r, addurl, http.StatusSeeOther)
|
||||
return
|
||||
} else {
|
||||
res := insertjson(addurl, cnf.linkpath)
|
||||
data.Url = res
|
||||
data.Url = insertjson(addurl, cnf.linkpath)
|
||||
data.Dom = cnf.domain
|
||||
data.Tit = getloc("tansyukuzumi", data.Lan)
|
||||
ftmpl[0] = cnf.webpath + "/view/submitted.html"
|
||||
|
@ -86,7 +143,7 @@ func serv (cnf Config, port int) {
|
|||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
} else { // r.Method == "GET"
|
||||
if uri == "/" && qnewurl == "" {
|
||||
ftmpl[0] = cnf.webpath + "/view/index.html"
|
||||
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
|
@ -113,7 +170,7 @@ func serv (cnf Config, port int) {
|
|||
ftmpl[0] = cnf.webpath + "/view/404.html"
|
||||
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
}
|
||||
}
|
||||
} // r.Method
|
||||
|
||||
tmpl.Execute(w, data)
|
||||
data = nil
|
||||
|
|
読み込み中…
新しいイシューから参照