Skip to content

Instantly share code, notes, and snippets.

View collinvandyck's full-sized avatar
🏠
Working from home

Collin Van Dyck collinvandyck

🏠
Working from home
View GitHub Profile
type User struct {
Id string `json:"id"`
Email string `json:"email,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name,omitempty"`
Admin bool `json:"admin"`
Active bool `json:"-"`
}
public interface UserDAO {
@SqlQuery("SELECT u.id, u.email, u.password_hash, u.apikey, u.created_at, u.updated_at, u.name, u.id, u.admin, u.active from users u where u.apikey = :apiKey")
@RegisterMapper(UserMapper.class)
User getUserByApiKey(@Bind("apiKey") String apiKey);
static class UserMapper implements ResultSetMapper<User> {
@Override
public User map(int i, ResultSet rs, StatementContext statementContext) throws SQLException {
return new User(
type Credentials string
func Authenticate(w http.ResponseWriter, r *http.Request) {
credentials, err := newCredentials(r)
if err != nil {
http.Error(w, err.Error(), BadRequest)
return
}
user, err := GetUserByApiKey(db, credentials)
if err != nil {
func main() {
http.HandleFunc("/authenticate", Authenticate)
http.ListenAndServe(":8080", nil)
}
@Path("authenticate")
@Produces(MediaType.APPLICATION_JSON)
public class AuthResource {
private final UserDAO userDAO;
public AuthResource(UserDAO userDAO) {
this.userDAO = userDAO;
}
@GET
type Config struct {
Db struct {
Name string
User string
Password string
}
}
func ReadConfig() Config {
if len(os.Args) != 2 {
{
"Db": {
"Name": "authdb",
"User": "postgres",
"Password": "postgres"
}
}
database:
driverClass: org.postgresql.Driver
user: postgres
password: postgres
url: jdbc:postgresql://localhost/authdb
properties:
charSet: UTF-8
http:
requestLog:
console:
public class AuthConfiguration extends Configuration {
@Valid
@NotNull
@JsonProperty
private DatabaseConfiguration database = new DatabaseConfiguration();
public DatabaseConfiguration getDatabaseConfiguration() {
return database;
}
@Path("authenticate")
@Produces(MediaType.APPLICATION_JSON)
public class AuthResource {
private final UserDAO userDAO;
public AuthResource(UserDAO userDAO) {
this.userDAO = userDAO;
}
@GET