lauantai 21. helmikuuta 2015

Setting a cookie with Golang

Couldn't find a good example about how to set a cookie to response with Golang using Gin.

So, here is what I did

r.LoadHTMLTemplates("templates/*")
r.GET("/en", func(c *gin.Context)
 
{
    expire
 
:=
 time
.Now
().AddDate
(
1
,
 
0
,
 
1
)
    cookie
 :=
 http.Cookie
{"cookieName", "cookieValue",
        "/", ".domain.com", expire, expire.Format(time.UnixDate), 
        41472000, false, false, "cookieName=cookieValue", 
        []string{"cookieName=cookieValue"}}
     http.SetCookie(context.Writer, &cookie)
     c.HTML(200, "index.html", gin.H{"title""Main website"})
})


From documentation (http://golang.org/src/net/http/cookie.go)

type Cookie struct { 
    Name string 
    Value string 
    Path string 
    Domain string 
    Expires time.Time 
    RawExpires string 
    // MaxAge=0 means no 'Max-Age' attribute specified. 
    // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0' 
    // MaxAge>0 means Max-Age attribute present and given in seconds 
    MaxAge int 
    Secure bool 
    HttpOnly bool 
    Raw string 
    Unparsed []string // Raw text of unparsed attribute-value pairs 
}

lauantai 21. huhtikuuta 2012

Jetty 8 and memcached sessions

Today I changed Uutispuro into using memcached as a session store.


Basically I followed the instructions from https://github.com/yyuu/jetty-nosql-memcached with minor tweaks.

etc/jetty.xml



  <Set name="sessionIdManager">
    <New id="memcachedSessionIdManager" class="org.eclipse.jetty.nosql.memcached.MemcachedSessionIdManager">
      <Arg><Ref id="Server" /></Arg>
      <Set name="serverString">localhost:11211</Set>
      <Set name="keyPrefix">session:</Set>
    </New>
  </Set>
  <Call name="setAttribute">
    <Arg>memcachedSessionIdManager</Arg>
    <Arg><Ref id="memcachedSessionIdManager" /></Arg>
  </Call>




contexts/uutiset.xml




  <Ref name="Server" id="Server">
    <Call id="sessionIdManager" name="getAttribute">
      <Arg>memcachedSessionIdManager</Arg>
    </Call>
  </Ref>
  <Set name="sessionHandler">
    <New class="org.eclipse.jetty.server.session.SessionHandler">
      <Arg>
        <New id="memcachedSessionManager" class="org.eclipse.jetty.nosql.memcached.MemcachedSessionManager">
          <Set name="sessionIdManager">
            <Ref id="sessionIdManager" />
          </Set>
        </New>
      </Arg>
    </New>
  </Set>

At the moment I have two jetty's running and I can stop each one of them at any time and user stays logged in as nothing has happened.

sunnuntai 22. tammikuuta 2012

Jetty 8 and using ssl with jetty:run

<plugin>
  <groupid>org.mortbay.jetty</groupid>
  <artifactid>jetty-maven-plugin</artifactid>
  <version>8.0.4.v20111024</version>
  <configuration>
    <scanintervalseconds>5</scanintervalseconds>
    <webappconfig>
      <contextpath>/</contextpath>
    </webappconfig>
    <connectors>
      <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
        <port>8080</port>
      </connector>
      <connector implementation="org.eclipse.jetty.server.ssl.SslSocketConnector">
        <port>8443</port>
        <keystore>/etc/keystore/keystore</keystore>
        <keypassword>password</keypassword>
        <password>password</password>
      </connector>
   </connectors>
</configuration>
</plugin>

Add this to your pom.xml inside <build> and <plugins> tags.
Of course you also need to have keystore in place. See Jetty/Howto/Configure_SSL.

lauantai 21. tammikuuta 2012

Spring-security start project with mongodb

I started up making a custom signup && login to Uutispuro and it was working nicely. A Couple of years ago I looked at spring-security for this, but it seemed like an overblow. When Spring 3.1 was out, I happened to look at spring-security again. And I tried it. No turning back now :)

I made a small start project to github: springsecuritylogin.

There is no ssl-support of course, you need to add to your application server. Handling sessions with memcached is something I'd still like to add. Maybe tomorrow ...

The project adds users with a signup page. Default role is ROLE_USER. You can modify a user to be an "ROLE_ADMIN" user by hand, and the index pages verifies the current role used.

torstai 17. marraskuuta 2011

Spring, Jetty 7 and jndi

jetty-env.xml needs to be in WEB-INF


<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd">
<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<New class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg>jdbc/jndiname</Arg>
<Arg>
<New class="oracle.jdbc.pool.OracleDataSource">
<Set name="user">DBUSER</Set>
<Set name="password">DBPASSWORD</Set>
<Set name="URL">jdbc:oracle:thin:@URL</Set>
</New>
</Arg>
</New>
</Configure>


pom.xml:


<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
....


applicationContext:


<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/jndiname" />
</bean>

lauantai 14. toukokuuta 2011

How to find largest files on linux?

find . -type f | xargs ls -s | sort -rn | awk '{size=$1/1024; printf("%dMb %s\n", size,$2);}' | head -30

keskiviikko 2. maaliskuuta 2011

Googlebot, CookieLocaleResolver and default locale

Googlebot doesn't set a locale on request.

What does this mean? Well, in my case when I localized www.uutispuro.fi, googlebot only minded about english content. Even if I made different urls and sitemaps for webmaster tools.

Why such a behaviour? I'm using Spring's CookieLocalResolver, and ofcourse it had a default to en_US. This took a while for me to notice. Setting the default to "fi" does what I was expecting in the first place.


<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="fi"/>
</bean>