Skip to content

Commit

Permalink
chore: improve for index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 30, 2023
1 parent 1cb14f3 commit 9c88687
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 35 deletions.
1 change: 1 addition & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ robots = [
"facebot",
"Applebot",
"Spider",
"Robot",
"robot",
]

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ go 1.21

require (
github.com/BurntSushi/toml v1.3.2
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible
github.com/teambition/gear v1.27.3
)

require (
github.com/go-http-utils/cookie v1.3.1 // indirect
github.com/go-http-utils/negotiator v1.0.0 // indirect
github.com/teambition/trie-mux v1.5.2 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
)
25 changes: 11 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible h1:6JF1bjhT0WN2srEmijfOFtVWwV91KZ6dJY1/JbdtGrI=
github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible h1:Sg/2xHwDrioHpxTN6WMiwbXTpUEinBpHsN7mG21Rc2k=
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible h1:so4m5rRA32Tc5GgKg/5gKUu0CRsYmVO3ThMP6T3CwLc=
github.com/aliyun/aliyun-oss-go-sdk v3.0.1+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dimfeld/httptreemux v5.0.1+incompatible/go.mod h1:rbUlSV+CCpv/SuqUTP/8Bk2O3LyUV436/yaRGkhP6Z0=
github.com/go-http-utils/cookie v1.3.1 h1:GCdTeqVV5vDcjP7LrgYpH8pbt3dOYKS+Wrs7Jo3/k/w=
github.com/go-http-utils/cookie v1.3.1/go.mod h1:ATl4rfG3bEemjiVa+8WIfgNcBUWdYBTasfXKjJ3Avt8=
Expand All @@ -17,22 +16,20 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/teambition/gear v1.27.3 h1:iWUOJYdBwxU+SZP5aZ2ZYR5FnRGmdgrMbbSpOCZo0go=
github.com/teambition/gear v1.27.3/go.mod h1:d3Nmr6rRPnH5lYSK33W9IDhsaxp/8n14vRrUZu9dP9c=
github.com/teambition/trie-mux v1.5.2 h1:ALTagFwKZXkn1vfSRlODlmoZg+NMeWAm4dyBPQI6a8w=
github.com/teambition/trie-mux v1.5.2/go.mod h1:0Woh4KOHSN9bkJ66eWmLs8ltrEKw+fnZbFaHFfbMrtc=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 20 additions & 14 deletions src/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net/http"
"net/http/httputil"
"net/url"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -39,6 +38,7 @@ func NewApp() *gear.App {
app.Set(gear.SetCompress, gear.ThresholdCompress(128))
app.Set(gear.SetGraceTimeout, time.Duration(conf.Config.Server.GracefulShutdown)*time.Second)
app.Set(gear.SetEnv, conf.Config.Env)
app.Set(gear.SetServerName, "Yiwen")

app.UseHandler(logging.AccessLogger)
groups, err := LoadFiles(conf.Config.GlobalSignal)
Expand Down Expand Up @@ -89,13 +89,14 @@ func (gs Groups) Serve(ctx *gear.Context) error {
return ctx.End(status)
}

ua := ctx.GetHeader(gear.HeaderUserAgent)
if ctx.Method == http.MethodGet && slices.ContainsFunc(conf.Config.SSR.Robots, func(bot string) bool {
return strings.Contains(ua, bot)
}) {
ssrProxy.ServeHTTP(ctx.Res, ctx.Req)
return nil
}
// SSR is dispatched by gateway
// ua := ctx.GetHeader(gear.HeaderUserAgent)
// if ctx.Method == http.MethodGet && slices.ContainsFunc(conf.Config.SSR.Robots, func(bot string) bool {
// return strings.Contains(ua, bot)
// }) {
// ssrProxy.ServeHTTP(ctx.Res, ctx.Req)
// return nil
// }

isWechat := strings.Contains(ctx.GetHeader(gear.HeaderUserAgent), Wechat_UA)
// https://www.yiwen.pub/pub/ck1sasaglcahc6fks810?language=zho&by=ke82hfgs3ni
Expand All @@ -111,10 +112,10 @@ func (gs Groups) Serve(ctx *gear.Context) error {

name, file := gs.lookupFile(ctx.Path)
if name != "" {
ctx.SetHeader(gear.HeaderCacheControl, "public, max-age=604800, must-revalidate")
ctx.SetHeader(gear.HeaderCacheControl, "public, max-age=604800")

if name == "index.html" {
ctx.SetHeader(gear.HeaderCacheControl, "no-cache, no-store")
ctx.SetHeader(gear.HeaderCacheControl, "no-cache")
lang := handleContext(ctx)
app := "web"
if isWechat {
Expand Down Expand Up @@ -178,10 +179,10 @@ func GetVersion() map[string]string {
}
}

func handleContext(ctx *gear.Context) (lang string) {
func handleContext(ctx *gear.Context) string {
logging.SetTo(ctx, "referer", ctx.GetHeader(gear.HeaderReferer))
// user preferred language
lang = ctx.Query("lang")
lang := ctx.Query("lang")
if lang == "" {
lang = ctx.GetHeader("x-language")
}
Expand All @@ -196,7 +197,12 @@ func handleContext(ctx *gear.Context) (lang string) {
}
}

lang = Lang639_3(lang)
langs := Lang639(lang)
if len(langs) == 0 {
langs = Lang639("en")
}

lang = langs[1]
logging.SetTo(ctx, "lang", lang)

// user preferred currency
Expand Down Expand Up @@ -241,7 +247,7 @@ func handleContext(ctx *gear.Context) (lang string) {
})
}

return
return langs[0]
}

func buildProxy(ssrHost string) http.Handler {
Expand Down
6 changes: 3 additions & 3 deletions src/app/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ func init() {
}
}

func Lang639_3(lang string) string {
func Lang639(lang string) []string {
lang = strings.ToLower(lang)

for _, vv := range Languages {
if vv[0] == lang || vv[1] == lang || strings.ToLower(vv[2]) == lang || strings.ToLower(vv[3]) == lang {
return vv[1]
return vv
}
}

return ""
return []string{}
}

var languages = `[["ab","abk","Abkhazian","аҧсуа бызшәа\u200e (Aṗsua byzšwa), аҧсшәа\u200e (Aṗsšwa)"],["aa","aar","Afar","Qafar"],["af","afr","Afrikaans","Afrikaans"],["ak","aka","Akan","Akan"],["sq","sqi","Albanian","shqip"],["am","amh","Amharic","አማርኛ"],["ar","ara","Arabic","العربية"],["an","arg","Aragonese","Aragonés"],["hy","hye","Armenian","հայերեն"],["as","asm","Assamese","অসমীয়া"],["av","ava","Avaric","авар мацӏ\u200e (Awar mac̣), магӏарул мацӏ\u200e (Maʿarul mac̣)"],["az","aze","Azerbaijani","azərbaycan"],["bm","bam","Bambara","bamanakan"],["ba","bak","Bashkir","башҡорт теле\u200e (Başķort tele), башҡортса\u200e (Başķortsa)"],["eu","eus","Basque","euskara"],["be","bel","Belarusian","беларуская"],["bn","ben","Bengali","বাংলা"],["bi","bis","Bislama","Bislama"],["bs","bos","Bosnian","bosanski"],["br","bre","Breton","brezhoneg"],["bg","bul","Bulgarian","български"],["my","mya","Burmese","မြန်မာ"],["ca","cat","Catalan","català"],["ch","cha","Chamorro","Chamorru"],["ce","che","Chechen","нохчийн"],["zh","zho","Chinese","中文"],["cu","chu","Church Slavic","церковнослове́нскїй"],["cv","chv","Chuvash","Чӑвашла"],["kw","cor","Cornish","kernewek"],["co","cos","Corsican","Corsu"],["hr","hrv","Croatian","hrvatski"],["cs","ces","Czech","čeština"],["da","dan","Danish","dansk"],["dv","div","Dhivehi","ދިވެހިބަސް"],["nl","nld","Dutch","Nederlands"],["dz","dzo","Dzongkha","རྫོང་ཁ"],["en","eng","English","English"],["eo","epo","Esperanto","esperanto"],["et","est","Estonian","eesti"],["ee","ewe","Ewe","Eʋegbe"],["fo","fao","Faroese","føroyskt"],["fi","fin","Finnish","suomi"],["fr","fra","French","français"],["ff","ful","Fulah","Pulaar"],["gl","glg","Galician","galego"],["lg","lug","Ganda","Luganda"],["ka","kat","Georgian","ქართული"],["de","deu","German","Deutsch"],["gu","guj","Gujarati","ગુજરાતી"],["ht","hat","Haitian","Ayisyen, Kreyòl"],["ha","hau","Hausa","Hausa"],["he","heb","Hebrew","עברית"],["hi","hin","Hindi","हिन्दी"],["hu","hun","Hungarian","magyar"],["is","isl","Icelandic","íslenska"],["ig","ibo","Igbo","Igbo"],["id","ind","Indonesian","Indonesia"],["ia","ina","Interlingua","interlingua"],["iu","iku","Inuktitut","ᐃᓄᒃᑎᑐᑦ"],["ga","gle","Irish","Gaeilge"],["it","ita","Italian","italiano"],["ja","jpn","Japanese","日本語"],["jv","jav","Javanese","Basa Jawa"],["kl","kal","Kalaallisut","kalaallisut"],["kn","kan","Kannada","ಕನ್ನಡ"],["ks","kas","Kashmiri","کٲشُر"],["kk","kaz","Kazakh","қазақ тілі"],["km","khm","Khmer","ខ្មែរ"],["ki","kik","Kikuyu","Gikuyu"],["rw","kin","Kinyarwanda","Kinyarwanda"],["ky","kir","Kirghiz","кыргызча"],["ko","kor","Korean","한국어"],["kj","kua","Kuanyama","Oshikwanyama"],["ku","kur","Kurdish","kurdî"],["lo","lao","Lao","ລາວ"],["lv","lav","Latvian","latviešu"],["li","lim","Limburgan","Limbourgeois, Limburgs"],["ln","lin","Lingala","lingála"],["lt","lit","Lithuanian","lietuvių"],["lu","lub","Luba-Katanga","Tshiluba"],["lb","ltz","Luxembourgish","Lëtzebuergesch"],["mk","mkd","Macedonian","македонски"],["mg","mlg","Malagasy","Malagasy"],["ms","msa","Malay","Melayu"],["ml","mal","Malayalam","മലയാളം"],["mt","mlt","Maltese","Malti"],["gv","glv","Manx","Gaelg"],["mi","mri","Maori","te reo Maori"],["mr","mar","Marathi","मराठी"],["el","ell","Modern Greek","Ελληνικά"],["mn","mon","Mongolian","монгол"],["nv","nav","Navajo","Diné"],["ne","nep","Nepali","नेपाली"],["nd","nde","North Ndebele","isiNdebele"],["se","sme","Northern Sami","davvisámegiella"],["no","nor","Norwegian","Norsk"],["nn","nno","Norwegian Nynorsk","nynorsk"],["ny","nya","Nyanja","Chichewa, chiCheŵa\u200e (Chichewa), chiNyanja"],["oc","oci","Occitan","occitan"],["or","ori","Oriya","ଓଡ଼ିଆ"],["om","orm","Oromo","Oromoo"],["os","oss","Ossetian","ирон"],["pa","pan","Panjabi","ਪੰਜਾਬੀ"],["fa","fas","Persian","فارسی"],["pl","pol","Polish","polski"],["pt","por","Portuguese","português"],["ps","pus","Pushto","پښتو"],["qu","que","Quechua","Runasimi"],["ro","ron","Romanian","română"],["rm","roh","Romansh","rumantsch"],["rn","run","Rundi","Ikirundi"],["ru","rus","Russian","русский"],["sm","smo","Samoan","Gagana Samoa"],["sg","sag","Sango","Sängö"],["sa","san","Sanskrit","संस्कृत भाषा"],["gd","gla","Scottish Gaelic","Gàidhlig"],["sr","srp","Serbian","српски"],["sn","sna","Shona","chiShona"],["ii","iii","Sichuan Yi","ꆈꌠꉙ"],["sd","snd","Sindhi","سنڌي"],["si","sin","Sinhala","සිංහල"],["sk","slk","Slovak","slovenčina"],["sl","slv","Slovenian","slovenščina"],["so","som","Somali","Soomaali"],["nr","nbl","South Ndebele","isiNdebele"],["st","sot","Southern Sotho","Sesotho"],["es","spa","Spanish","español"],["su","sun","Sundanese","Sunda"],["sw","swa","Swahili","Kiswahili"],["ss","ssw","Swati","siSwati"],["sv","swe","Swedish","svenska"],["tl","tgl","Tagalog","Tagalog"],["ty","tah","Tahitian","Reo Tahiti"],["tg","tgk","Tajik","тоҷикӣ"],["ta","tam","Tamil","தமிழ்"],["tt","tat","Tatar","татар"],["te","tel","Telugu","తెలుగు"],["th","tha","Thai","ไทย"],["bo","bod","Tibetan","བོད་སྐད་"],["ti","tir","Tigrinya","ትግርኛ"],["to","ton","Tonga","lea fakatonga"],["ts","tso","Tsonga","Xitsonga"],["tn","tsn","Tswana","Setswana"],["tr","tur","Turkish","Türkçe"],["tk","tuk","Turkmen","Türkmen dili"],["ug","uig","Uighur","ئۇيغۇرچە"],["uk","ukr","Ukrainian","українська"],["ur","urd","Urdu","اردو"],["uz","uzb","Uzbek","o‘zbek"],["ve","ven","Venda","Tshivenḓa"],["vi","vie","Vietnamese","Tiếng Việt"],["cy","cym","Welsh","Cymraeg"],["fy","fry","Western Frisian","Frysk"],["wo","wol","Wolof","Wolof"],["xh","xho","Xhosa","isiXhosa"],["yi","yid","Yiddish","ייִדיש"],["yo","yor","Yoruba","Èdè Yorùbá"],["zu","zul","Zulu","isiZulu"]]`

0 comments on commit 9c88687

Please sign in to comment.