mosn

Форк
0
55 строк · 1.9 Кб
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
package main
19

20
import (
21
	"strconv"
22

23
	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
24
	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
25
)
26

27
// build main like below with proxy-wasm-go-sdk v0.0.14 which works with
28
// TinyGo 0.19.0 (which works with Go 1.16).
29
//
30
//	tinygo build -o main.wasm -scheduler=none -target=wasi --no-debug -wasm-abi=generic -tags 'abi_010' ./main.go
31
//
32
// Note: Old tooling is needed because mosn.io/proxy-wasm-go-host v2 is
33
// incompatible with current proxy-wasm SDKs.
34
func main() {
35
	proxywasm.SetNewHttpContext(newHttpContext)
36
}
37

38
type myHttpContext struct {
39
	// you must embed the default context so that you need not to re-implement all the methods by yourself
40
	proxywasm.DefaultHttpContext
41
	contextID uint32
42
}
43

44
func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
45
	return &myHttpContext{contextID: contextID}
46
}
47

48
func (ctx *myHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
49
	key := "Wasm-Context"
50
	err := proxywasm.SetHttpRequestHeader(key, strconv.Itoa(int(ctx.contextID)))
51
	if err != nil {
52
		proxywasm.LogCritical("failed to set request header: " + key)
53
	}
54
	return types.ActionContinue
55
}
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.