> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Bắt đầu nhanh

> Xây dựng một connector tối giản từ đầu đến cuối: module, manifest, adapter, phần kết nối và image

Hướng dẫn này thêm một connector tên `example` vào danh mục `vulnerabilities`. Thay `vulnerabilities` và `example` bằng danh mục và slug bạn muốn.

## Điều kiện tiên quyết

* Go 1.26+
* [Task](https://taskfile.dev) 3.x
* Docker 20.10+ (để build và chạy image)

## Tạo module

Thư mục có dạng `<category>/<slug>`.

```bash theme={null}
mkdir -p vulnerabilities/example
cd vulnerabilities/example
```

Tạo `go.mod` yêu cầu SDK, sau đó trỏ nó về bản sao local bằng một chỉ thị `replace`:

```go theme={null}
module github.com/oasm-platform/oasm-connectors/vulnerabilities/example

go 1.26

require github.com/oasm-platform/oasm-connectors/sdk v0.0.0

replace github.com/oasm-platform/oasm-connectors/sdk => ../../sdk
```

```bash theme={null}
GOWORK=off go mod tidy
```

## Viết `manifest.yaml`

```yaml theme={null}
name: Example
slug: example
version: 1.0.0
image: ghcr.io/oasm-platform/connector-example:1.0.0
author: 'you'
pricingTier: [free]
shortDescription: 'Example vulnerability scanner'
description: 'Scans a target for example findings.'
capabilities: [vulnerabilities]
inputsSchema:
  type: object
  required: [target]
  properties:
    target:
      type: string
      format: uri
      title: Scan target
      description: 'Target URL or hostname to scan.'
  additionalProperties: false
configSchema:
  type: object
  additionalProperties: false
  properties:
    severity:
      type: string
      enum: [info, low, medium, high, critical]
      title: Minimum severity
      default: high
```

Xem [tham chiếu manifest](https://docs.oasm.io/vi/connectors/manifest.md) để biết mọi trường.

## Viết adapter

`adapter.go` — phần code duy nhất dành riêng cho công cụ.

```go theme={null}
package main

import (
    "context"
    "fmt"
    "os"
    "strings"

    "github.com/oasm-platform/oasm-connectors/sdk/connector"
)

type ExampleAdapter struct{}

// Validate rejects inputs the tool cannot run.
func (a *ExampleAdapter) Validate(_ context.Context, inputs map[string]any) error {
    target, _ := inputs["target"].(string)
    if strings.TrimSpace(target) == "" {
        return fmt.Errorf("fatal: target is required")
    }
    return nil
}

// Execute runs the tool and streams findings to out.
func (a *ExampleAdapter) Execute(ctx context.Context, inputs map[string]any, out chan<- connector.Finding) error {
    target, _ := inputs["target"].(string)
    severity := os.Getenv("OASM_CONFIG") // parse in real connectors

    _ = severity
    for _, raw := range runTool(ctx, target) {
        f := connector.Finding{
            Name:     raw.Name,
            Severity: normalizeSeverity(raw.Severity),
            Host:     target,
        }
        select {
        case out <- f:
        case <-ctx.Done():
            return ctx.Err()
        }
    }
    return nil
}

func normalizeSeverity(s string) string {
    switch strings.ToLower(strings.TrimSpace(s)) {
    case "info", "low", "medium", "high", "critical":
        return strings.ToLower(strings.TrimSpace(s))
    default:
        return "info"
    }
}
```

Xem [tham chiếu Adapter](https://docs.oasm.io/vi/connectors/adapter.md) và [tham chiếu Finding](https://docs.oasm.io/vi/connectors/finding.md).

## Kết nối trong `main.go`

`main.go` chỉ khởi tạo adapter và chạy runtime. Coi context bị hủy là một lần thoát bình thường.

```go theme={null}
package main

import (
    "context"
    "log"

    sdkconn "github.com/oasm-platform/oasm-connectors/sdk/connector"
    "github.com/oasm-platform/oasm-connectors/sdk/runtime"
)

func main() {
    conn := sdkconn.New(&ExampleAdapter{})
    rt := runtime.New(conn)

    log.Println("example connector starting...")
    if err := rt.Run(context.Background()); err != nil && err != context.Canceled {
        log.Fatalf("connector failed: %v", err)
    }
    log.Println("example connector stopped")
}
```

> [!NOTE]
>
> Mọi thứ giữa `runtime.New` và `Run` — kết nối, đăng ký, stream — là việc của SDK. Bạn không cần cấu hình gì.

## Viết `Dockerfile`

Build context là **thư mục gốc của repo**, nên hãy copy `sdk/` và thư mục connector một cách tường minh. Copy cả hai cặp `go.mod`/`go.sum` trước để các layer dependency được cache.

```dockerfile theme={null}
FROM golang:1.26-alpine AS builder
WORKDIR /src
COPY sdk/go.mod sdk/go.sum sdk/
COPY vulnerabilities/example/go.mod vulnerabilities/example/go.sum vulnerabilities/example/
RUN cd vulnerabilities/example && GOWORK=off go mod download
COPY sdk/ sdk/
COPY vulnerabilities/example/ vulnerabilities/example/
RUN cd vulnerabilities/example && CGO_ENABLED=0 GOWORK=off go build -trimpath -o /out/connector .

FROM alpine:3.20
COPY --from=builder /out/connector /usr/local/bin/connector
RUN adduser -D -u 10001 connector
USER 10001
ENTRYPOINT ["/usr/local/bin/connector"]
```

Image không được expose port hay yêu cầu shell.

## Tạo lại và kiểm thử

```bash theme={null}
task manifest   # regenerate manifest.json
task test       # per-module tests
task vet        # per-module go vet
```

## Bước tiếp theo

> [card] [Build & kiểm thử](https://docs.oasm.io/vi/connectors/build-and-test.md)
>
> Các lệnh Task, quy tắc kiểm thử và phát hành.

> [card] [Input & config](https://docs.oasm.io/vi/connectors/inputs-and-config.md)
>
> Đọc config profile đúng cách.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.