Skip to content

Instantly share code, notes, and snippets.

View karlosmid's full-sized avatar

Karlo Smid karlosmid

View GitHub Profile
@karlosmid
karlosmid / mix.exs
Created February 7, 2021 16:28
exkeycdn mix project file
defmodule ExKeyCDN.MixProject do
use Mix.Project
def project do
[
app: :exkeycdn,
version: "0.0.2",
elixir: "~> 1.11",
start_permanent: Mix.env() == :prod,
deps: deps(),
<!DOCTYPE html>
<!--
! Excerpted from "Programming Elm",
! published by The Pragmatic Bookshelf.
! Copyrights apply to this code. It may not be used to create training material,
! courses, books, articles, and the like. Contact us if you are in doubt.
! We make no guarantees that this code is fit for any purpose.
! Visit http://www.pragmaticprogrammer.com/titles/jfelm for more book information.
-->
<html lang="en">
@karlosmid
karlosmid / picshare_websocket_connect.elm
Created February 4, 2021 12:40
Actual connection and subscription to websocket server.
module Picshare exposing (main)
-- 1. use WebSocket module
import WebSocket
import Browser
import Html exposing (..)
import Html.Events exposing (onClick, onInput, onSubmit)
import Html.Attributes exposing (class, src, placeholder, type_, disabled, value)
import Json.Decode exposing (Decoder, bool, int, list, string, succeed)
import Json.Decode.Pipeline exposing (hardcoded, required)
import Http
@karlosmid
karlosmid / index.html
Created January 28, 2021 13:25
Elm application index file
<!DOCTYPE html>
<!--
! Excerpted from "Programming Elm",
! published by The Pragmatic Bookshelf.
! Copyrights apply to this code. It may not be used to create training material,
! courses, books, articles, and the like. Contact us if you are in doubt.
! We make no guarantees that this code is fit for any purpose.
! Visit http://www.pragmaticprogrammer.com/titles/jfelm for more book information.
-->
<html lang="en">
@karlosmid
karlosmid / websocket.elm
Created January 28, 2021 13:24
Connect elm application with Websocket server
port module WebSocket exposing (listen, receive)
port listen : String -> Cmd msg
port receive : (String -> msg) -> Sub msg
@karlosmid
karlosmid / secure_preload.ex
Last active January 24, 2021 18:22
Ecto Query preload with a query.
@spec author_query() :: Ecto.Query.t()
def author_query() do
from user in User,
select: [:name, :email]
end
@spec query_active_charter_sessions(integer(), NaiveDateTime.t()) :: Ecto.Query.t()
def query_active_charter_sessions(charter_id, active_threshold) do
from(s in Session,
join: c in Charter,
on: c.id == s.charter_id,
@karlosmid
karlosmid / query_with_sensitive_info.ex
Created January 24, 2021 10:27
Query that returns sensitive information
@spec query_active_charter_sessions(integer(), NaiveDateTime.t()) :: Ecto.Query.t()
def query_active_charter_sessions(charter_id, active_threshold) do
from(s in Session,
join: c in Charter,
on: c.id == s.charter_id,
where: c.id == ^charter_id,
where: ^active_threshold <= s.inserted_at,
preload: :author
)
end
@karlosmid
karlosmid / secure_preload.ex
Created January 24, 2021 10:16
How To Securely Use Ecto Preload Function
@timestamps_opts [type: :utc_datetime]
schema "sessions" do
belongs_to(:charter, Charter)
has_many(:note, Note)
has_one(:report, Report)
belongs_to(:author, User)
timestamps()
end
@karlosmid
karlosmid / picshare_photo_feed.elm
Created January 19, 2021 13:45
Picshare loads Photo feed on init
module Picshare exposing (main)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick, onInput, onSubmit)
import Html.Attributes exposing (class, src, placeholder, type_, disabled, value)
import Json.Decode exposing (Decoder, bool, int, list, string, succeed)
import Json.Decode.Pipeline exposing (hardcoded, required)
import Http
type alias Id =
Int
@karlosmid
karlosmid / picshare_initial_get.elm
Created January 12, 2021 12:27
Picshare initial model is Photo from the Internet
module Picshare exposing (main)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick, onInput, onSubmit)
import Html.Attributes exposing (class, src, placeholder, type_, disabled, value)
import Json.Decode exposing (Decoder, bool, int, list, string, succeed)
import Json.Decode.Pipeline exposing (hardcoded, required)
import Http
type alias Id =
Int