Skip to content

Commit

Permalink
Merge pull request #22 from rubixFunctions/code-clean
Browse files Browse the repository at this point in the history
add haskell bootstrapping
  • Loading branch information
ciaranRoche authored Apr 19, 2019
2 parents abdb65d + 625112d commit 5562443
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ RubiX supports the following:
- JavaScript : `--type js`
- Golang : `--type go`
- Python : `--type py`
- Haskell : `--type hs`

### Create a Function as a Container Image
The create function image locally:
Expand Down
2 changes: 2 additions & 0 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ func genServiceYaml(name string, image string){
createServiceYAML(name, image)
case "py":
createServiceYAML(name, image)
case "hs":
createServiceYAML(name, image)
default:
fmt.Println("Error parsing Schema, no service.yaml generated")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/function_types_golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func InitializeGoFunction(function *Function, schema *Schema){
return
}
} else if !isEmpty(function.AbsPath()) {
fmt.Println("Function can not be bootstrapped in a non empty direcctory: " + function.AbsPath())
fmt.Println("Function can not be bootstrapped in a non empty directory: " + function.AbsPath())
return
}

Expand Down
151 changes: 151 additions & 0 deletions cmd/function_types_haskell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package cmd

import (
"fmt"
"os"
"path/filepath"
)

func InitializeHaskellFunction(function *Function, schema *Schema){
if !exists(function.AbsPath()) {
err := os.MkdirAll(function.AbsPath(), os.ModePerm)
if err != nil {
fmt.Println(err)
return
}
} else if !isEmpty(function.AbsPath()) {
fmt.Println("Function can not be bootstrapped in a non empty directory: " + function.AbsPath())
return
}

createHsDockerfile(function)
createHsMain(function)
createHsPackageYaml(function)
createHsStackFile(function)
createLicense(function)
createSchema(schema, function)
fmt.Println(`Your Function is ready at` + function.AbsPath())

}

func createHsDockerfile(function *Function){
dockerTemplate := `FROM haskell:8.2.2 as builder
# Add dependiencies needed to builder
RUN apt-get update && apt-get install --yes \
xz-utils \
build-essential \
libtool \
libpcre3-dev \
libpcre3 \
make
# Copy local code to the container image.
WORKDIR /app
COPY . .
# Build and test our code, then build the “helloworld-haskell-exe” executable.
RUN stack setup
RUN stack build --copy-bins --ghc-options '-static -optl-static -optl-pthread -fPIC'
# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM fpco/haskell-scratch
# Copy the "helloworld-haskell-exe" executable from the builder stage to the production image.
WORKDIR /root/
COPY --from=builder /root/.local/bin/helloworld-haskell-exe .
# Service must listen to $PORT environment variable.
# This default value facilitates local development.
ENV PORT 8080
# Run the web service on container startup.
CMD ["./helloworld-haskell-exe"]`

createFile(function, dockerTemplate, "Dockerfile")

}

func createHsPackageYaml(function *Function){
packageTemplate := `name: helloworld-haskell
version: 0.1.0.0
dependencies:
- base >= 4.7 && < 5
- r3x-haskell-sdk
- wai
- text
- aeson
executables:
helloworld-haskell-exe:
main: Main.hs
source-dirs: app
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N`

createFile(function, packageTemplate, "package.yaml")

}

func createHsStackFile(function *Function){
stackTemplate := `flags: {}
packages:
- .
extra-deps:
- r3x-haskell-sdk-0.1.0.0@sha256:5411ed12947b6cc623bbf25997a2abaf26686e5bd97c18dce6a486ab07187df8
resolver: lts-10.7`

createFile(function, stackTemplate, "stack.yaml")

}

func createHsMain(function *Function){
mainTemplate := `{-# language OverloadedStrings #-}
{-# language DeriveAnyClass #-}
{-# language DeriveGeneric #-}
module Main where
import Rubix
import Data.Aeson (ToJSON, FromJSON)
import GHC.Generics (Generic)
import qualified Data.Text as DT
import qualified Network.Wai as NW
data Message = Message
{
message :: DT.Text
} deriving (Generic, ToJSON, FromJSON)
rubixMessage :: Message
rubixMessage = Message{message="Hello RubiX!!!"}
-- Define a response handler
rubixHandler :: Handler NW.Response
rubixHandler = do
let res = toResponse $ Json rubixMessage
return res
-- Start the server
main :: IO ()
main = execute rubixHandler
`
createMainFile(function, mainTemplate, "Main.hs")

}

// Creates a file
func createMainFile(function *Function, template, file string) {
data := make(map[string]interface{})
rootCmdScript, err := executeTemplate(template, data)
if err != nil {
fmt.Println(err)
}
var path = filepath.Join(function.AbsPath(), "/app")
err = writeStringToFile(filepath.Join(path, file), rootCmdScript)
if err != nil {
fmt.Println(err)
}
}
19 changes: 18 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ Init will not use an existing directory with contents.`, logo),
initString("Python")
InitializePyFunction(function, schema)
}
case "hs":
var function *Function
if len(args) == 0 {
fmt.Println("A Function needs a name")
} else if len(args) == 1 {
arg := args[0]
if arg[0] == '.'{
arg = filepath.Join(wd, arg)
}
function = NewFunction(arg)
function.license.Name = license
var schema *Schema
schema = NewSchema("r3x-"+arg, "hs", "json")
initString("Haskell")
InitializeHaskellFunction(function, schema)
}
default:
fmt.Println(warningTypeMessage)
}
Expand All @@ -114,7 +130,8 @@ var warningTypeMessage = `Function type required, use '-t' flag
Supported paradigms :
- JavaScript : '-t js'
- GoLang : '-t go'
- Python : '-t py'`
- Python : '-t py'
- Haskell : '-t hs'`

// Init Init Function
func init() {
Expand Down

0 comments on commit 5562443

Please sign in to comment.