Giter Site home page Giter Site logo

jan0sch / pfhais Goto Github PK

View Code? Open in Web Editor NEW
56.0 10.0 16.0 7.67 MB

Source code of the book Pure functional HTTP APIs in Scala including a chapter about upgrading to Scala 3.

Home Page: https://leanpub.com/pfhais

License: Creative Commons Attribution Share Alike 4.0 International

Scala 100.00%
scala functional-programming cats-effect http4s fs2 monocle tapir optics scala3

pfhais's Introduction

Pure functional HTTP APIs in Scala

Build Status codecov

This repository contains the source code for the book "Pure functional HTTP APIs in Scala" which is available on Leanpub: https://leanpub.com/pfhais

While Leanpub is the recommended source the book is also available at:

An online course based upon the book was done by Educative: https://www.educative.io/courses/pure-functional-http-apis-scala

Structure

The folder manuscript contains the book source code and other resources like images. The service implementations are placed in the folders impure and pure. Within the folder tapir you can find the pure implementation which uses the tapir library for a typed API design.

Build Tooling

All project modules can be compiled, run and tested via sbt. Just open a terminal in the desired folder (e.g. pure) and start the sbt shell.

If you are using the IntelliJ IDEA development environment then you will need to install the Scala plugin for it. Afterwards you should be able to create IDEA projects by using the "Import Project" feature and point it to the desired folder (e.g. tapir).

Copyleft Notice

This book uses the Creative Commons Attribution ShareAlike 4.0 International (CC BY-SA 4.0) license. The code snippets in this book are licensed under CC0 which means you can use them without restriction. Excerpts from libraries maintain their license.

pfhais's People

Contributors

jan0sch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pfhais's Issues

PostgreSQL setup commands

Hello @jan0sch , I am really enjoying your Pure functional HTTP APIs in Scala book.

When it comes to setting up PostgreSQL so that I can run the code, it would be really nice if there were some instructions, e.g. like these in doobie, or maybe even a script.

The reason I ask is because when I try to run impure, flyway falls over with org.postgresql.util.PSQLException: ERROR: permission denied for schema public:

[info] running com.wegtam.books.pfhais.impure.Impure 
2022-12-04 18:08:13,862 WARN  Database - Flyway upgrade recommended: PostgreSQL 15.1 is newer than this version of Flyway and support has not been tested. The latest supported version of PostgreSQL is 12.
[error] (run-main-0) org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException: 
[error] Migration  failed
[error] -----------------
[error] SQL State  : 42501
[error] Error Code : 0
[error] Message    : ERROR: permission denied for schema public
[error]   Position: 14
[error] Location   :  ()
[error] Line       : 1
[error] Statement  : CREATE TABLE "public"."flyway_schema_history" (
[error]     "installed_rank" INT NOT NULL,
[error]     "version" VARCHAR(50),
[error]     "description" VARCHAR(200) NOT NULL,
[error]     "type" VARCHAR(20) NOT NULL,
[error]     "script" VARCHAR(1000) NOT NULL,
[error]     "checksum" INTEGER,
[error]     "installed_by" VARCHAR(100) NOT NULL,
[error]     "installed_on" TIMESTAMP NOT NULL DEFAULT now(),
[error]     "execution_time" INTEGER NOT NULL,
[error]     "success" BOOLEAN NOT NULL
[error] )
[error] org.flywaydb.core.internal.sqlscript.FlywaySqlScriptException: 
[error] Migration  failed
[error] -----------------
[error] SQL State  : 42501
[error] Error Code : 0
[error] Message    : ERROR: permission denied for schema public
[error]   Position: 14
[error] Location   :  ()
[error] Line       : 1
[error] Statement  : CREATE TABLE "public"."flyway_schema_history" (
[error]     "installed_rank" INT NOT NULL,
[error]     "version" VARCHAR(50),
[error]     "description" VARCHAR(200) NOT NULL,
[error]     "type" VARCHAR(20) NOT NULL,
[error]     "script" VARCHAR(1000) NOT NULL,
[error]     "checksum" INTEGER,
[error]     "installed_by" VARCHAR(100) NOT NULL,
[error]     "installed_on" TIMESTAMP NOT NULL DEFAULT now(),
[error]     "execution_time" INTEGER NOT NULL,
[error]     "success" BOOLEAN NOT NULL
[error] )
[error] 
[error]         at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.handleException(DefaultSqlScriptExecutor.java:274)
[error]         at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.executeStatement(DefaultSqlScriptExecutor.java:220)
[error]         at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.execute(DefaultSqlScriptExecutor.java:127)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory$1$1.call(JdbcTableSchemaHistory.java:113)
[error]         at org.flywaydb.core.internal.jdbc.TransactionTemplate.execute(TransactionTemplate.java:74)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory$1.call(JdbcTableSchemaHistory.java:106)
[error]         at org.flywaydb.core.internal.database.postgresql.PostgreSQLAdvisoryLockTemplate.execute(PostgreSQLAdvisoryLockTemplate.java:71)
[error]         at org.flywaydb.core.internal.database.postgresql.PostgreSQLConnection.lock(PostgreSQLConnection.java:94)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.create(JdbcTableSchemaHistory.java:97)
[error]         at org.flywaydb.core.Flyway$1.execute(Flyway.java:184)
[error]         at org.flywaydb.core.Flyway$1.execute(Flyway.java:149)
[error]         at org.flywaydb.core.Flyway.execute(Flyway.java:511)
[error]         at org.flywaydb.core.Flyway.migrate(Flyway.java:149)
[error]         at com.wegtam.books.pfhais.impure.Impure$.main(Impure.scala:48)
[error]         at com.wegtam.books.pfhais.impure.Impure.main(Impure.scala)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error] Caused by: org.postgresql.util.PSQLException: ERROR: permission denied for schema public
[error]   Position: 14
[error]         at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497)
[error]         at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2233)
[error]         at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310)
[error]         at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446)
[error]         at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370)
[error]         at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:311)
[error]         at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:297)
[error]         at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:274)
[error]         at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:269)
[error]         at org.flywaydb.core.internal.jdbc.JdbcTemplate.executeStatement(JdbcTemplate.java:244)
[error]         at org.flywaydb.core.internal.sqlscript.ParsedSqlStatement.execute(ParsedSqlStatement.java:111)
[error]         at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.executeStatement(DefaultSqlScriptExecutor.java:208)
[error]         at org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor.execute(DefaultSqlScriptExecutor.java:127)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory$1$1.call(JdbcTableSchemaHistory.java:113)
[error]         at org.flywaydb.core.internal.jdbc.TransactionTemplate.execute(TransactionTemplate.java:74)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory$1.call(JdbcTableSchemaHistory.java:106)
[error]         at org.flywaydb.core.internal.database.postgresql.PostgreSQLAdvisoryLockTemplate.execute(PostgreSQLAdvisoryLockTemplate.java:71)
[error]         at org.flywaydb.core.internal.database.postgresql.PostgreSQLConnection.lock(PostgreSQLConnection.java:94)
[error]         at org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory.create(JdbcTableSchemaHistory.java:97)
[error]         at org.flywaydb.core.Flyway$1.execute(Flyway.java:184)
[error]         at org.flywaydb.core.Flyway$1.execute(Flyway.java:149)
[error]         at org.flywaydb.core.Flyway.execute(Flyway.java:511)
[error]         at org.flywaydb.core.Flyway.migrate(Flyway.java:149)
[error]         at com.wegtam.books.pfhais.impure.Impure$.main(Impure.scala:48)
[error]         at com.wegtam.books.pfhais.impure.Impure.main(Impure.scala)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[error] stack trace is suppressed; run last Compile / bgRun for the full output

I tried a few quick fixes that didn't work and so it would be really nice if setting up the db were a complete piece of cake. Do you happen to have some commands handy that will do that?

Intellij setup question

Hi and thanks a lot for sharing your thoughts and work!

Just in case you are using Intellij Idea as IDE: do you have any special setup in place to make the project compile? I keep getting a "No implicits found for parameters..." in TranslationTest.scala, for example at

RefType.applyRef[LanguageCode](l)

"sbt clean test" does not have an issue, though. I do not have too much experience with sbt, unfortunately, but I can see quite a few compiler options in the build setup...

Any hint highly appreciated (and thanks for writing that book, highly inspiring ;))

Test Cases Compilation Error

Error:(20, 16) BaseSpec is already defined as class BaseSpec
abstract class BaseSpec extends WordSpec with MustMatchers with ScalaCheckPropertyChecks {}
2.
Error:(20, 16) Companions 'class BaseSpec' and 'object BaseSpec' must be defined in same file:
Found in /Users/imransarwar/git/pfhais/impure/src/test/scala/com/wegtam/books/pfhais/BaseSpec.scala and /Users/imransarwar/git/pfhais/impure/src/it/scala/com/wegtam/books/pfhais/impure/BaseSpec.scala
abstract class BaseSpec extends WordSpec with MustMatchers with ScalaCheckPropertyChecks {}
3.
Error:(23, 8) TypeGenerators is already defined as object TypeGenerators
object TypeGenerators {

Run benchmarks after 2.13 is ready

After the cross compilation with 2.13 is enabled und functional (#2) run the benchmarks.

  • Run benchmarks on Scala 2.13 too
  • Report observations regarding Future (2.12 vs 2.13) to V. Klang ;-)

Footnotes

Footnotes 11 and 12 are given twice and footnotes 38,39 and 40 don't seem to be referenced.

Scala 2.13

Missing dependencies for Scala 2.13

Just in case all dependencies get updated we will move on to Scala 2.13 or at least cross build between 2.12 and 2.13.

Impure

  • Cats
  • Circe
  • refined

Pure

  • Cats
  • Circe
  • Doobie
  • http4s
  • kittens
  • pureconfig
  • refined

Application Request Not Working

Once application started, I send the POST request as below but it shows malformed request

imransarwar➜~/git» curl -v -H "Content-Type: application/json" -X POST http://localhost:49152/products -d '{"id":"One", "names": [{"lang": "en", "name": "English"}]}'
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 49152 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 49152 (#0)
> POST /products HTTP/1.1
> Host: localhost:49152
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 58
> 
* upload completely sent off: 58 out of 58 bytes
< HTTP/1.1 400 Bad Request
< Server: akka-http/10.1.10
< Date: Mon, 16 Nov 2020 23:23:30 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 63
< 
The request content was malformed:
* Connection #0 to host localhost left intact
DecodingFailure at .id: UUID* Closing connection 0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.