将无服务器函数部署到DigitalOcean上的Golang

huangapple go评论84阅读模式
英文:

Deploy serverless functions to digitalocean in golang

问题

我正在尝试在Digital Ocean的无服务器函数中构建一个CRUD应用程序。我正在使用sqlite3进行测试,并将数据插入表中。当我尝试将其部署到生产环境时,出现了错误。以下是我的代码:

package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
)

func Main(args map[string]interface{}) map[string]interface{} {
	db, err := sql.Open("sqlite3", "./data.db")
	checkErr(err)

	// 插入数据
	stmt, err := db.Prepare("INSERT INTO userinfo(username, departname, created) values(?,?,?)")
	checkErr(err)

	res, err := stmt.Exec("rumi", "CSE", "2012-12-09")
	checkErr(err)

	id, err := res.LastInsertId()
	checkErr(err)

	fmt.Println(id)

	db.Close()

	msg := make(map[string]interface{})
	msg["body"] = id
	return msg
}

我遇到的错误如下:

➜  functions git:(master) ✗ doctl serverless deploy . --remote-build
Deploying '/home/rumi/go/src/github.com/manjurulhoque/digitalocean-cloud-functions/functions'
  to namespace 'fn-b799454253a-a40440-4639-937f-05102a48c06e'
  on host 'https://fa45as-sgp1-18b45c02afgc.doserverless.co'
Submitted action 'blog/createBlog' for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action 'sample/hello' for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action 'blog/db' for remote building and deployment in runtime go:default (id: edcc9eefce9f4aa58c9eefce9f2aa5e6)
Transcript of remote build session for action 'blog/db':
Output of failed build in /tmp/slices/builds/fn-b79956253a-a4080-465639-95637f-05102a48c06e/blog_db/2022-10-22T04-23-08.642Z/packages/blog/db
initializing modules
go: creating new go.mod: module exec
go: to add module requirements and sums:
        go mod tidy
building
db.go:6:2: no required module provides package github.com/mattn/go-sqlite3; to add it:
        go get github.com/mattn/go-sqlite3


Deployed functions ('doctl sbx fn get <funcName> --url' for URL):
  - blog/createBlog
  - sample/hello
Failures:
Error: While deploying action 'blog/db': './build.sh' exited with code 1

project.yml

environment: {}
parameters: {}
packages:
  - name: blog
    environment: {}
    parameters: {}
    annotations: {}
    functions:
      - name: db
        binary: false
        main: ''
        runtime: 'go:default'
        web: true
        parameters: {}
        environment: {}
        annotations: {}
        limits: {}

我没有找到任何好的资源来连接数据库。任何帮助将不胜感激。

英文:

I am trying to build a CRUD in digital ocean serverless functions. I am testing with sqlite3 and insert into the table. When I tried to deploy it to productions, I am getting errors. Here is my code:

package main

import (
    &quot;database/sql&quot;
    &quot;fmt&quot;
    _ &quot;github.com/mattn/go-sqlite3&quot;
)

func Main(args map[string]interface{}) map[string]interface{} {
db, err := sql.Open(&quot;sqlite3&quot;, &quot;./data.db&quot;)
checkErr(err)

// insert
stmt, err := db.Prepare(&quot;INSERT INTO userinfo(username, departname, created) values(?,?,?)&quot;)
checkErr(err)

res, err := stmt.Exec(&quot;rumi&quot;, &quot;CSE&quot;, &quot;2012-12-09&quot;)
checkErr(err)

id, err := res.LastInsertId()
checkErr(err)

fmt.Println(id)

db.Close()

msg := make(map[string]interface{})
msg[&quot;body&quot;] = id
return msg
}

Errors I am getting:

➜  functions git:(master) ✗ doctl serverless deploy . --remote-build
Deploying &#39;/home/rumi/go/src/github.com/manjurulhoque/digitalocean-cloud-functions/functions&#39;
  to namespace &#39;fn-b799454253a-a40440-4639-937f-05102a48c06e&#39;
  on host &#39;https://fa45as-sgp1-18b45c02afgc.doserverless.co&#39;
Submitted action &#39;blog/createBlog&#39; for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action &#39;sample/hello&#39; for remote building and deployment in runtime go:default (id: b3d5421ee5656bb44c4295421eebb44c642cf)
Submitted action &#39;blog/db&#39; for remote building and deployment in runtime go:default (id: edcc9eefce9f4aa58c9eefce9f2aa5e6)
Transcript of remote build session for action &#39;blog/db&#39;:
Output of failed build in /tmp/slices/builds/fn-b79956253a-a4080-465639-95637f-05102a48c06e/blog_db/2022-10-22T04-23-08.642Z/packages/blog/db
initializing modules
go: creating new go.mod: module exec
go: to add module requirements and sums:
        go mod tidy
building
db.go:6:2: no required module provides package github.com/mattn/go-sqlite3; to add it:
        go get github.com/mattn/go-sqlite3


Deployed functions (&#39;doctl sbx fn get &lt;funcName&gt; --url&#39; for URL):
  - blog/createBlog
  - sample/hello
Failures:
Error: While deploying action &#39;blog/db&#39;: &#39;./build.sh&#39; exited with code 1

project.yml

environment: {}
parameters: {}
packages:
  - name: blog
    environment: {}
    parameters: {}
    annotations: {}
    functions:
      - name: db
        binary: false
        main: &#39;&#39;
        runtime: &#39;go:default&#39;
        web: true
        parameters: {}
        environment: {}
        annotations: {}
        limits: {}

I didn't find any good resource though to connect to DB. Any help would be appreciated.

答案1

得分: 1

在 DO(Digital Ocean)中,每个函数本身都是一个应用程序。因此,您需要在每个函数目录中创建 go.modgo.sum 文件。

以下是我的一个项目结构示例:

将无服务器函数部署到DigitalOcean上的Golang

注意:我在如何设置用于 DO 的 Golang 应用程序的文章中进行了介绍。
https://medium.com/@manzurulhoque/use-package-in-digital-ocean-do-serverless-functions-using-golang-cb5200ab22ee

英文:

In DO, every function itself is an app. So you need to create go.mod and go.sum in each function directoy.

Below is one of my project structure

将无服务器函数部署到DigitalOcean上的Golang

Note: I wrote an article on how to setup golang app for DO
https://medium.com/@manzurulhoque/use-package-in-digital-ocean-do-serverless-functions-using-golang-cb5200ab22ee

huangapple
  • 本文由 发表于 2022年10月22日 12:28:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74161067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定