Skip to content

Commit

Permalink
implemented checking robots' user-agents by regexp. added yandex-metr…
Browse files Browse the repository at this point in the history
…ica javascript initialization in production.
  • Loading branch information
bvn13 committed Nov 27, 2017
1 parent 40bbb57 commit cc06ad8
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 8 deletions.
4 changes: 2 additions & 2 deletions releases/myblog.service
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[Unit]
Description=myapp
Description=MyBlog
After=syslog.target

[Service]
User=bvn13
#ExecStart=/home/bvn13/dev/blog/myblog.sh
ExecStart=/usr/bin/java -jar /home/bvn13/dev/blog/SpringBlog-2.6.1.jar
ExecStart=/usr/bin/java -jar /home/bvn13/dev/blog/SpringBlog-2.6.1.jar --spring.config.location=application-production.yml
SuccessExitStatus=143

[Install]
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/raysmond/blog/forms/SeoRobotAgentForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class SeoRobotAgentForm {
@NotNull
private String userAgent = "";

@NotNull
private Boolean isRegexp = false;
}
3 changes: 3 additions & 0 deletions src/main/java/com/raysmond/blog/models/SeoRobotAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class SeoRobotAgent extends BaseModel {
@Column(nullable = false)
private String userAgent;

@Column(nullable = false, columnDefinition = "boolean default false")
private Boolean isRegexp;

public SeoRobotAgent() {

}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/raysmond/blog/services/VisitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ public Long getUniqueVisitsCount(Post post) {
"SELECT COUNT(DISTINCT v.clientIp) " +
"FROM visits AS v " +
"LEFT JOIN seo_robots_agents AS ra " +
"ON LOWER(v.userAgent) LIKE concat('%', LOWER(ra.userAgent), '%') " +
//"ON LOWER(v.userAgent) LIKE concat('%', LOWER(ra.userAgent), '%') " +
"ON CASE WHEN ra.isregexp = TRUE THEN " +
"LOWER(v.userAgent) SIMILAR TO concat(LOWER(ra.userAgent)) " +
"ELSE " +
"LOWER(v.userAgent) LIKE concat('%', LOWER(ra.userAgent), '%') " +
"END " +
"WHERE v.post_id = :post_id AND v.isAdmin = FALSE " +
"AND ra.id IS NULL ");
query.setLong("post_id", post.getId());
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/raysmond/blog/support/web/ViewHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public String getApplicationEnv() {
return applicationEnv;
}

public Boolean isProductionMode() {
return this.applicationEnv.equalsIgnoreCase("production");
}

public void setApplicationEnv(String applicationEnv) {
this.applicationEnv = applicationEnv;
}
Expand Down Expand Up @@ -119,4 +123,14 @@ public String getPostUrl(Post post) {
post.getPermalink().isEmpty() ? post.getId() : post.getPermalink()
);
}

public String getAbsoluteUrl(String url) {
if (url.isEmpty()) {
return "";
}
return String.format("%s/%s",
this.appSetting.getMainUri().endsWith("/") ? this.appSetting.getMainUri().substring(0, this.appSetting.getMainUri().length()-1) : this.appSetting.getMainUri(),
url.startsWith("/") ? url.substring(1) : url
);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ spring:

hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
hbm2ddl.auto: validate
hbm2ddl.auto: update
show_sql: true
use_sql_comments: true
format_sql: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,27 @@ form.post-form(method='POST', action='/admin/robotsAgents/#{form.getId()}/edit')
| User-Agent:
input.form-control(name="userAgent", value="#{form.getUserAgent()}")
.item-row
input.btn.btn-primary(type='submit', value='Save')
label
| Is RegExp:
input(name="isRegexp", type="hidden", value="#{form.getIsRegexp() == true ? 'true' : 'false'}")
input(id="isRegexp", type="checkbox")
.item-row
input.btn.btn-primary(type='submit', value='Save')


script
:javascript
$(document).ready(function() {

var isChecked = $("input[name=isRegexp]").val();
if (isChecked === 'true') {
$("input#isRegexp").attr('checked', 'checked');
} else {
$("input#isRegexp").removeAttr('checked');
}

$("input#isRegexp").click(function(){
$("input[name=isRegexp]").val(""+$(this).is(':checked'));
});

});
2 changes: 2 additions & 0 deletions src/main/resources/templates/admin/robotsAgents/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ block content
tr
th ID
th User-Agent
th Is Regexp
th Operations

tbody
for record in records
tr
td #{record.getId()}
td #{record.getUserAgent()}
td #{record.getIsRegexp().toString()}
td.operations
a.btn.btn-xs.btn-primary(href="/admin/robotsAgents/#{record.getId()}/edit")
i.fa.fa-edit
Expand Down
Empty file.
3 changes: 2 additions & 1 deletion src/main/resources/templates/layout/app.jade
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ html(lang="en")

include footer

if viewHelper.getApplicationEnv() == "production"
if viewHelper.isProductionMode()
include ../fragments/ga
include ../fragments/ya-metrics
4 changes: 2 additions & 2 deletions src/main/resources/templates/layout/head.jade
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ if seoData != null

meta( property="og:type", content="#{seoData.getOgType().getName()}" )
meta( property="og:locale", content="#{seoData.getOgLocale().getName()}" )
meta( property="og:image", content="#{seoData.getOgImage()}" )
meta( property="og:video", content="#{seoData.getOgVideo()}" )
meta( property="og:image", content="#{viewHelper.getAbsoluteUrl(seoData.getOgImage())}" )
meta( property="og:video", content="#{viewHelper.getAbsoluteUrl(seoData.getOgVideo())}" )

else
meta( property="og:title", content="#{App.getSiteName()}" )
Expand Down

0 comments on commit cc06ad8

Please sign in to comment.